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_privacy_policy_settings_provider" AS ENUM('alfright', 'internal');
  CREATE TYPE "public"."enum_privacy_policy_settings_alfright_language" AS ENUM('de-de', 'de-at', 'de-ch', 'en-gb', 'en-us');
  CREATE TABLE "privacy_policy_settings" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer NOT NULL,
  	"title" varchar DEFAULT 'Datenschutzerklärung' NOT NULL,
  	"provider" "enum_privacy_policy_settings_provider" DEFAULT 'alfright' NOT NULL,
  	"alfright_tenant_id" varchar DEFAULT 'alfright_schutzteam',
  	"alfright_api_key" varchar,
  	"alfright_language" "enum_privacy_policy_settings_alfright_language" DEFAULT 'de-de',
  	"alfright_iframe_height" numeric DEFAULT 4000,
  	"styling_header_color" varchar DEFAULT '#ca8a04',
  	"styling_header_font" varchar DEFAULT 'Inter, sans-serif',
  	"styling_header_size" varchar DEFAULT '24px',
  	"styling_subheader_size" varchar DEFAULT '18px',
  	"styling_font_color" varchar DEFAULT '#f3f4f6',
  	"styling_text_font" varchar DEFAULT 'Inter, sans-serif',
  	"styling_text_size" varchar DEFAULT '16px',
  	"styling_link_color" varchar DEFAULT '#ca8a04',
  	"styling_background_color" varchar DEFAULT '#111827',
  	"show_cookie_table" boolean DEFAULT true,
  	"cookie_table_title" varchar DEFAULT 'Übersicht der verwendeten Cookies',
  	"cookie_table_description" varchar DEFAULT 'Ergänzend zur Datenschutzerklärung finden Sie hier eine detaillierte Übersicht aller auf dieser Website eingesetzten Cookies. Sie können Ihre Cookie-Einstellungen jederzeit über den Link "Cookie-Einstellungen" im Footer anpassen.',
  	"seo_meta_title" varchar DEFAULT 'Datenschutzerklärung',
  	"seo_meta_description" varchar DEFAULT 'Informationen zum Datenschutz und zur Verarbeitung Ihrer personenbezogenen Daten.',
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_banner_description" SET DEFAULT 'Diese Website verwendet Cookies, um Ihnen die bestmögliche Erfahrung zu bieten.';
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_category_labels_functional_description" SET DEFAULT 'Diese Cookies ermöglichen erweiterte Funktionen.';
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_category_labels_analytics_description" SET DEFAULT 'Diese Cookies helfen uns zu verstehen, wie Besucher die Website nutzen.';
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_category_labels_marketing_description" SET DEFAULT 'Diese Cookies werden für Werbezwecke verwendet.';
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "privacy_policy_settings_id" integer;
  ALTER TABLE "privacy_policy_settings" ADD CONSTRAINT "privacy_policy_settings_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  CREATE UNIQUE INDEX "privacy_policy_settings_tenant_idx" ON "privacy_policy_settings" USING btree ("tenant_id");
  CREATE INDEX "privacy_policy_settings_updated_at_idx" ON "privacy_policy_settings" USING btree ("updated_at");
  CREATE INDEX "privacy_policy_settings_created_at_idx" ON "privacy_policy_settings" USING btree ("created_at");
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_privacy_policy_settings_fk" FOREIGN KEY ("privacy_policy_settings_id") REFERENCES "public"."privacy_policy_settings"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "payload_locked_documents_rels_privacy_policy_settings_id_idx" ON "payload_locked_documents_rels" USING btree ("privacy_policy_settings_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "privacy_policy_settings" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "privacy_policy_settings" CASCADE;
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_privacy_policy_settings_fk";
  
  DROP INDEX "payload_locked_documents_rels_privacy_policy_settings_id_idx";
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_banner_description" SET DEFAULT 'Diese Website verwendet Cookies, um Ihnen die bestmögliche Erfahrung zu bieten. Sie können Ihre Einstellungen jederzeit anpassen.';
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_category_labels_functional_description" SET DEFAULT 'Diese Cookies ermöglichen erweiterte Funktionen und Personalisierung.';
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_category_labels_analytics_description" SET DEFAULT 'Diese Cookies helfen uns zu verstehen, wie Besucher mit der Website interagieren.';
  ALTER TABLE "cookie_configurations" ALTER COLUMN "translations_de_category_labels_marketing_description" SET DEFAULT 'Diese Cookies werden verwendet, um Werbung relevanter für Sie zu gestalten.';
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "privacy_policy_settings_id";
  DROP TYPE "public"."enum_privacy_policy_settings_provider";
  DROP TYPE "public"."enum_privacy_policy_settings_alfright_language";`)
}
