//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
//Copyright (C) 2024 HTWKalender support@htwkalender.de

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Affero General Public License for more details.

//You should have received a copy of the GNU Affero General Public License
//along with this program.  If not, see <https://www.gnu.org/licenses/>.

package migrations

import (
	"encoding/json"

	"github.com/pocketbase/dbx"
	"github.com/pocketbase/pocketbase/daos"
	m "github.com/pocketbase/pocketbase/migrations"
	"github.com/pocketbase/pocketbase/models/schema"
)

func init() {
	m.Register(func(db dbx.Builder) error {
		dao := daos.New(db)

		collection, err := dao.FindCollectionByNameOrId("7her4515qsmrxe8")
		if err != nil {
			return err
		}

		json.Unmarshal([]byte(`[]`), &collection.Indexes)

		// remove
		collection.Schema.RemoveField("7vsr9h6p")

		// remove
		collection.Schema.RemoveField("wwpokofe")

		// add
		new_start := &schema.SchemaField{}
		json.Unmarshal([]byte(`{
			"system": false,
			"id": "6hkjwgb4",
			"name": "start",
			"type": "date",
			"required": false,
			"presentable": false,
			"unique": false,
			"options": {
				"min": "",
				"max": ""
			}
		}`), new_start)
		collection.Schema.AddField(new_start)

		// add
		new_end := &schema.SchemaField{}
		json.Unmarshal([]byte(`{
			"system": false,
			"id": "szbefpjf",
			"name": "end",
			"type": "date",
			"required": false,
			"presentable": false,
			"unique": false,
			"options": {
				"min": "",
				"max": ""
			}
		}`), new_end)
		collection.Schema.AddField(new_end)

		// add
		new_Compulsory := &schema.SchemaField{}
		json.Unmarshal([]byte(`{
			"system": false,
			"id": "nlnnxu7x",
			"name": "Compulsory",
			"type": "text",
			"required": false,
			"presentable": false,
			"unique": false,
			"options": {
				"min": null,
				"max": null,
				"pattern": ""
			}
		}`), new_Compulsory)
		collection.Schema.AddField(new_Compulsory)

		return dao.SaveCollection(collection)
	}, func(db dbx.Builder) error {
		dao := daos.New(db)

		collection, err := dao.FindCollectionByNameOrId("7her4515qsmrxe8")
		if err != nil {
			return err
		}

		json.Unmarshal([]byte(`[
			"CREATE UNIQUE INDEX `+"`"+`idx_orp1NWL`+"`"+` ON `+"`"+`events`+"`"+` (\n  `+"`"+`Day`+"`"+`,\n  `+"`"+`Week`+"`"+`,\n  `+"`"+`Start`+"`"+`,\n  `+"`"+`End`+"`"+`,\n  `+"`"+`Name`+"`"+`,\n  `+"`"+`course`+"`"+`,\n  `+"`"+`Prof`+"`"+`,\n  `+"`"+`Rooms`+"`"+`,\n  `+"`"+`EventType`+"`"+`\n)"
		]`), &collection.Indexes)

		// add
		del_Start := &schema.SchemaField{}
		json.Unmarshal([]byte(`{
			"system": false,
			"id": "7vsr9h6p",
			"name": "Start",
			"type": "text",
			"required": false,
			"presentable": false,
			"unique": false,
			"options": {
				"min": null,
				"max": null,
				"pattern": ""
			}
		}`), del_Start)
		collection.Schema.AddField(del_Start)

		// add
		del_End := &schema.SchemaField{}
		json.Unmarshal([]byte(`{
			"system": false,
			"id": "wwpokofe",
			"name": "End",
			"type": "text",
			"required": false,
			"presentable": false,
			"unique": false,
			"options": {
				"min": null,
				"max": null,
				"pattern": ""
			}
		}`), del_End)
		collection.Schema.AddField(del_End)

		// remove
		collection.Schema.RemoveField("6hkjwgb4")

		// remove
		collection.Schema.RemoveField("szbefpjf")

		// remove
		collection.Schema.RemoveField("nlnnxu7x")

		return dao.SaveCollection(collection)
	})
}
