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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "whyus_steps" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"label" varchar NOT NULL
  );
  
  CREATE TABLE "whyus_cards" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"line" varchar,
  	"text" varchar
  );
  
  CREATE TABLE "whyus_figures" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"n" varchar NOT NULL,
  	"label" varchar
  );
  
  CREATE TABLE "whyus_teacher_points" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"text" varchar NOT NULL
  );
  
  CREATE TABLE "whyus_quotes" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"text" varchar NOT NULL
  );
  
  CREATE TABLE "whyus" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"cta_href" varchar,
  	"updated_at" timestamp(3) with time zone,
  	"created_at" timestamp(3) with time zone
  );
  
  CREATE TABLE "whyus_locales" (
  	"seo_title" varchar NOT NULL,
  	"seo_description" varchar,
  	"kicker" varchar,
  	"h1" varchar NOT NULL,
  	"lead" varchar,
  	"cards_title" varchar,
  	"audience_title" varchar,
  	"student_tag" varchar,
  	"student_title" varchar,
  	"student_text" varchar,
  	"teacher_tag" varchar,
  	"teacher_title" varchar,
  	"teacher_text" varchar,
  	"quotes_title" varchar,
  	"cta_title" varchar,
  	"cta_lead" varchar,
  	"cta_label" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  ALTER TABLE "whyus_steps" ADD CONSTRAINT "whyus_steps_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."whyus"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "whyus_cards" ADD CONSTRAINT "whyus_cards_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."whyus"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "whyus_figures" ADD CONSTRAINT "whyus_figures_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."whyus"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "whyus_teacher_points" ADD CONSTRAINT "whyus_teacher_points_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."whyus"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "whyus_quotes" ADD CONSTRAINT "whyus_quotes_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."whyus"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "whyus_locales" ADD CONSTRAINT "whyus_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."whyus"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "whyus_steps_order_idx" ON "whyus_steps" USING btree ("_order");
  CREATE INDEX "whyus_steps_parent_id_idx" ON "whyus_steps" USING btree ("_parent_id");
  CREATE INDEX "whyus_steps_locale_idx" ON "whyus_steps" USING btree ("_locale");
  CREATE INDEX "whyus_cards_order_idx" ON "whyus_cards" USING btree ("_order");
  CREATE INDEX "whyus_cards_parent_id_idx" ON "whyus_cards" USING btree ("_parent_id");
  CREATE INDEX "whyus_cards_locale_idx" ON "whyus_cards" USING btree ("_locale");
  CREATE INDEX "whyus_figures_order_idx" ON "whyus_figures" USING btree ("_order");
  CREATE INDEX "whyus_figures_parent_id_idx" ON "whyus_figures" USING btree ("_parent_id");
  CREATE INDEX "whyus_figures_locale_idx" ON "whyus_figures" USING btree ("_locale");
  CREATE INDEX "whyus_teacher_points_order_idx" ON "whyus_teacher_points" USING btree ("_order");
  CREATE INDEX "whyus_teacher_points_parent_id_idx" ON "whyus_teacher_points" USING btree ("_parent_id");
  CREATE INDEX "whyus_teacher_points_locale_idx" ON "whyus_teacher_points" USING btree ("_locale");
  CREATE INDEX "whyus_quotes_order_idx" ON "whyus_quotes" USING btree ("_order");
  CREATE INDEX "whyus_quotes_parent_id_idx" ON "whyus_quotes" USING btree ("_parent_id");
  CREATE INDEX "whyus_quotes_locale_idx" ON "whyus_quotes" USING btree ("_locale");
  CREATE UNIQUE INDEX "whyus_locales_locale_parent_id_unique" ON "whyus_locales" USING btree ("_locale","_parent_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   DROP TABLE "whyus_steps" CASCADE;
  DROP TABLE "whyus_cards" CASCADE;
  DROP TABLE "whyus_figures" CASCADE;
  DROP TABLE "whyus_teacher_points" CASCADE;
  DROP TABLE "whyus_quotes" CASCADE;
  DROP TABLE "whyus" CASCADE;
  DROP TABLE "whyus_locales" CASCADE;`)
}
