import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_cookie_configurations_enabled_categories" AS ENUM('necessary', 'functional', 'analytics', 'marketing');
  CREATE TYPE "public"."enum_cookie_configurations_styling_position" AS ENUM('bottom', 'top', 'middle');
  CREATE TYPE "public"."enum_cookie_configurations_styling_theme" AS ENUM('dark', 'light', 'auto');
  CREATE TYPE "public"."enum_cookie_inventory_category" AS ENUM('necessary', 'functional', 'analytics', 'marketing');
  CREATE TABLE "cookie_configurations_enabled_categories" (
  	"order" integer NOT NULL,
  	"parent_id" integer NOT NULL,
  	"value" "enum_cookie_configurations_enabled_categories",
  	"id" serial PRIMARY KEY NOT NULL
  );
  
  CREATE TABLE "cookie_configurations" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer NOT NULL,
  	"title" varchar DEFAULT 'Cookie-Einstellungen' NOT NULL,
  	"revision" numeric DEFAULT 1 NOT NULL,
  	"translations_de_banner_title" varchar DEFAULT 'Wir respektieren Ihre Privatsphäre',
  	"translations_de_banner_description" varchar DEFAULT 'Diese Website verwendet Cookies, um Ihnen die bestmögliche Erfahrung zu bieten. Sie können Ihre Einstellungen jederzeit anpassen.',
  	"translations_de_accept_all_button" varchar DEFAULT 'Alle akzeptieren',
  	"translations_de_accept_necessary_button" varchar DEFAULT 'Nur notwendige',
  	"translations_de_settings_button" varchar DEFAULT 'Einstellungen',
  	"translations_de_save_button" varchar DEFAULT 'Auswahl speichern',
  	"translations_de_privacy_policy_url" varchar DEFAULT '/datenschutz',
  	"translations_de_category_labels_necessary_title" varchar DEFAULT 'Notwendig',
  	"translations_de_category_labels_necessary_description" varchar DEFAULT 'Diese Cookies sind für die Grundfunktionen der Website erforderlich.',
  	"translations_de_category_labels_functional_title" varchar DEFAULT 'Funktional',
  	"translations_de_category_labels_functional_description" varchar DEFAULT 'Diese Cookies ermöglichen erweiterte Funktionen und Personalisierung.',
  	"translations_de_category_labels_analytics_title" varchar DEFAULT 'Statistik',
  	"translations_de_category_labels_analytics_description" varchar DEFAULT 'Diese Cookies helfen uns zu verstehen, wie Besucher mit der Website interagieren.',
  	"translations_de_category_labels_marketing_title" varchar DEFAULT 'Marketing',
  	"translations_de_category_labels_marketing_description" varchar DEFAULT 'Diese Cookies werden verwendet, um Werbung relevanter für Sie zu gestalten.',
  	"styling_position" "enum_cookie_configurations_styling_position" DEFAULT 'bottom',
  	"styling_theme" "enum_cookie_configurations_styling_theme" DEFAULT 'dark',
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE "cookie_inventory" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer NOT NULL,
  	"name" varchar NOT NULL,
  	"provider" varchar NOT NULL,
  	"category" "enum_cookie_inventory_category" NOT NULL,
  	"duration" varchar NOT NULL,
  	"description" varchar NOT NULL,
  	"is_active" boolean DEFAULT true,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE "consent_logs" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"consent_id" varchar NOT NULL,
  	"client_ref" varchar,
  	"tenant_id" integer NOT NULL,
  	"categories" jsonb NOT NULL,
  	"revision" numeric NOT NULL,
  	"user_agent" varchar,
  	"anonymized_ip" varchar,
  	"expires_at" timestamp(3) with time zone NOT NULL,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "cookie_configurations_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "cookie_inventory_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "consent_logs_id" integer;
  ALTER TABLE "cookie_configurations_enabled_categories" ADD CONSTRAINT "cookie_configurations_enabled_categories_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."cookie_configurations"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "cookie_configurations" ADD CONSTRAINT "cookie_configurations_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "cookie_inventory" ADD CONSTRAINT "cookie_inventory_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "consent_logs" ADD CONSTRAINT "consent_logs_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  CREATE INDEX "cookie_configurations_enabled_categories_order_idx" ON "cookie_configurations_enabled_categories" USING btree ("order");
  CREATE INDEX "cookie_configurations_enabled_categories_parent_idx" ON "cookie_configurations_enabled_categories" USING btree ("parent_id");
  CREATE UNIQUE INDEX "cookie_configurations_tenant_idx" ON "cookie_configurations" USING btree ("tenant_id");
  CREATE INDEX "cookie_configurations_updated_at_idx" ON "cookie_configurations" USING btree ("updated_at");
  CREATE INDEX "cookie_configurations_created_at_idx" ON "cookie_configurations" USING btree ("created_at");
  CREATE INDEX "cookie_inventory_tenant_idx" ON "cookie_inventory" USING btree ("tenant_id");
  CREATE INDEX "cookie_inventory_updated_at_idx" ON "cookie_inventory" USING btree ("updated_at");
  CREATE INDEX "cookie_inventory_created_at_idx" ON "cookie_inventory" USING btree ("created_at");
  CREATE UNIQUE INDEX "consent_logs_consent_id_idx" ON "consent_logs" USING btree ("consent_id");
  CREATE INDEX "consent_logs_tenant_idx" ON "consent_logs" USING btree ("tenant_id");
  CREATE INDEX "consent_logs_updated_at_idx" ON "consent_logs" USING btree ("updated_at");
  CREATE INDEX "consent_logs_created_at_idx" ON "consent_logs" USING btree ("created_at");
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_cookie_configurations_fk" FOREIGN KEY ("cookie_configurations_id") REFERENCES "public"."cookie_configurations"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_cookie_inventory_fk" FOREIGN KEY ("cookie_inventory_id") REFERENCES "public"."cookie_inventory"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_consent_logs_fk" FOREIGN KEY ("consent_logs_id") REFERENCES "public"."consent_logs"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "payload_locked_documents_rels_cookie_configurations_id_idx" ON "payload_locked_documents_rels" USING btree ("cookie_configurations_id");
  CREATE INDEX "payload_locked_documents_rels_cookie_inventory_id_idx" ON "payload_locked_documents_rels" USING btree ("cookie_inventory_id");
  CREATE INDEX "payload_locked_documents_rels_consent_logs_id_idx" ON "payload_locked_documents_rels" USING btree ("consent_logs_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "cookie_configurations_enabled_categories" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "cookie_configurations" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "cookie_inventory" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "consent_logs" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "cookie_configurations_enabled_categories" CASCADE;
  DROP TABLE "cookie_configurations" CASCADE;
  DROP TABLE "cookie_inventory" CASCADE;
  DROP TABLE "consent_logs" CASCADE;
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_cookie_configurations_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_cookie_inventory_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_consent_logs_fk";
  
  DROP INDEX "payload_locked_documents_rels_cookie_configurations_id_idx";
  DROP INDEX "payload_locked_documents_rels_cookie_inventory_id_idx";
  DROP INDEX "payload_locked_documents_rels_consent_logs_id_idx";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "cookie_configurations_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "cookie_inventory_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "consent_logs_id";
  DROP TYPE "public"."enum_cookie_configurations_enabled_categories";
  DROP TYPE "public"."enum_cookie_configurations_styling_position";
  DROP TYPE "public"."enum_cookie_configurations_styling_theme";
  DROP TYPE "public"."enum_cookie_inventory_category";`)
}
