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

/**
 * «سياسة الخصوصية» and «شروط الاستخدام»: two globals, each with a row table, a per-language table and a table
 * of numbered sections.
 *
 * Generated by `payload migrate:create`, with one thing taken back out of it by hand. The generator also wrote
 * `ALTER TABLE "settings" ADD COLUMN "support_enabled" …` and the other two live-support columns, because the
 * migration that added them (20261011_090000_live_support) was written by hand and left no `.json` snapshot
 * behind, so this diff was taken against a picture of the database from before them. Those three statements are
 * removed here: on any database where live support already ran they stop at «column already exists», and a site
 * whose migrations fail does not start at all. This migration's own `.json` snapshot does hold them, so the next
 * generated migration diffs against the truth again.
 */
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "privacy_sections" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"text" varchar NOT NULL
  );
  
  CREATE TABLE "privacy" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"updated_at" timestamp(3) with time zone,
  	"created_at" timestamp(3) with time zone
  );
  
  CREATE TABLE "privacy_locales" (
  	"seo_title" varchar NOT NULL,
  	"seo_description" varchar,
  	"h1" varchar NOT NULL,
  	"updated" varchar,
  	"lead" varchar,
  	"closing" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  CREATE TABLE "terms_sections" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"text" varchar NOT NULL
  );
  
  CREATE TABLE "terms" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"updated_at" timestamp(3) with time zone,
  	"created_at" timestamp(3) with time zone
  );
  
  CREATE TABLE "terms_locales" (
  	"seo_title" varchar NOT NULL,
  	"seo_description" varchar,
  	"h1" varchar NOT NULL,
  	"updated" varchar,
  	"lead" varchar,
  	"closing" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  ALTER TABLE "privacy_sections" ADD CONSTRAINT "privacy_sections_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."privacy"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "privacy_locales" ADD CONSTRAINT "privacy_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."privacy"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "terms_sections" ADD CONSTRAINT "terms_sections_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."terms"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "terms_locales" ADD CONSTRAINT "terms_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."terms"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "privacy_sections_order_idx" ON "privacy_sections" USING btree ("_order");
  CREATE INDEX "privacy_sections_parent_id_idx" ON "privacy_sections" USING btree ("_parent_id");
  CREATE INDEX "privacy_sections_locale_idx" ON "privacy_sections" USING btree ("_locale");
  CREATE UNIQUE INDEX "privacy_locales_locale_parent_id_unique" ON "privacy_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "terms_sections_order_idx" ON "terms_sections" USING btree ("_order");
  CREATE INDEX "terms_sections_parent_id_idx" ON "terms_sections" USING btree ("_parent_id");
  CREATE INDEX "terms_sections_locale_idx" ON "terms_sections" USING btree ("_locale");
  CREATE UNIQUE INDEX "terms_locales_locale_parent_id_unique" ON "terms_locales" USING btree ("_locale","_parent_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   DROP TABLE "privacy_sections" CASCADE;
  DROP TABLE "privacy" CASCADE;
  DROP TABLE "privacy_locales" CASCADE;
  DROP TABLE "terms_sections" CASCADE;
  DROP TABLE "terms" CASCADE;
  DROP TABLE "terms_locales" CASCADE;`)
}
