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"
)

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

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

		return dao.DeleteCollection(collection)
	}, func(db dbx.Builder) error {
		jsonData := `{
			"id": "az9g2m6y2eoyhwo",
			"created": "2025-02-15 01:35:42.844Z",
			"updated": "2025-02-15 01:37:34.943Z",
			"name": "finding_hashes",
			"type": "base",
			"system": false,
			"schema": [
				{
					"system": false,
					"id": "8jvj2ua5",
					"name": "hash",
					"type": "text",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"min": null,
						"max": null,
						"pattern": ""
					}
				},
				{
					"system": false,
					"id": "1fase53v",
					"name": "target",
					"type": "text",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"min": null,
						"max": null,
						"pattern": ""
					}
				},
				{
					"system": false,
					"id": "rsdhhnbl",
					"name": "finding_type",
					"type": "text",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"min": null,
						"max": null,
						"pattern": ""
					}
				},
				{
					"system": false,
					"id": "5uujhazp",
					"name": "severity",
					"type": "select",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"maxSelect": 1,
						"values": [
							"critical",
							"high",
							"medium",
							"low",
							"info"
						]
					}
				},
				{
					"system": false,
					"id": "i558ulb0",
					"name": "last_seen",
					"type": "date",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"min": "",
						"max": ""
					}
				},
				{
					"system": false,
					"id": "1njwaldb",
					"name": "scan_id",
					"type": "text",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"min": null,
						"max": null,
						"pattern": ""
					}
				},
				{
					"system": false,
					"id": "6oegeeaj",
					"name": "client_id",
					"type": "text",
					"required": true,
					"presentable": false,
					"unique": false,
					"options": {
						"min": null,
						"max": null,
						"pattern": ""
					}
				}
			],
			"indexes": [],
			"listRule": "@request.auth.id != '' && (@request.auth.role = 'admin' || @request.auth.view_findings = true)",
			"viewRule": "@request.auth.id != '' && (@request.auth.role = 'admin' || @request.auth.view_findings = true)",
			"createRule": "@request.auth.id != '' && (@request.auth.role = 'admin' || @request.auth.view_findings = true)",
			"updateRule": "@request.auth.id != '' && (@request.auth.role = 'admin' || @request.auth.view_findings = true)",
			"deleteRule": "@request.auth.id != '' && @request.auth.role = 'admin'",
			"options": {}
		}`

		collection := &models.Collection{}
		if err := json.Unmarshal([]byte(jsonData), &collection); err != nil {
			return err
		}

		return daos.New(db).SaveCollection(collection)
	})
}
