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

export async function up({ db }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
    -- Status Enum
    CREATE TYPE "public"."enum_form_submissions_status" AS ENUM('new', 'read', 'in-progress', 'waiting', 'completed', 'archived');

    -- Priority Enum
    CREATE TYPE "public"."enum_form_submissions_priority" AS ENUM('high', 'normal', 'low');

    -- Response Method Enum
    CREATE TYPE "public"."enum_form_submissions_response_tracking_response_method" AS ENUM('email', 'phone', 'in-person', 'letter');

    -- Add new columns to form_submissions
    ALTER TABLE "form_submissions"
      ADD COLUMN "status" "enum_form_submissions_status" DEFAULT 'new',
      ADD COLUMN "priority" "enum_form_submissions_priority" DEFAULT 'normal',
      ADD COLUMN "assigned_to_id" integer,
      ADD COLUMN "read_at" timestamp(3) with time zone,
      ADD COLUMN "read_by_id" integer,
      ADD COLUMN "response_tracking_responded" boolean DEFAULT false,
      ADD COLUMN "response_tracking_responded_at" timestamp(3) with time zone,
      ADD COLUMN "response_tracking_responded_by_id" integer,
      ADD COLUMN "response_tracking_response_method" "enum_form_submissions_response_tracking_response_method",
      ADD COLUMN "response_tracking_response_summary" varchar;

    -- Create internal notes table
    CREATE TABLE "form_submissions_internal_notes" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL,
      "id" varchar PRIMARY KEY NOT NULL,
      "note" varchar NOT NULL,
      "author_id" integer,
      "created_at" timestamp(3) with time zone
    );

    -- Create tags table
    CREATE TABLE "form_submissions_tags" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL,
      "id" varchar PRIMARY KEY NOT NULL,
      "tag" varchar NOT NULL
    );

    -- Add foreign keys
    ALTER TABLE "form_submissions"
      ADD CONSTRAINT "form_submissions_assigned_to_id_users_id_fk"
      FOREIGN KEY ("assigned_to_id") REFERENCES "public"."users"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

    ALTER TABLE "form_submissions"
      ADD CONSTRAINT "form_submissions_read_by_id_users_id_fk"
      FOREIGN KEY ("read_by_id") REFERENCES "public"."users"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

    ALTER TABLE "form_submissions"
      ADD CONSTRAINT "form_submissions_response_tracking_responded_by_id_users_id_fk"
      FOREIGN KEY ("response_tracking_responded_by_id") REFERENCES "public"."users"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

    ALTER TABLE "form_submissions_internal_notes"
      ADD CONSTRAINT "form_submissions_internal_notes_author_id_users_id_fk"
      FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

    ALTER TABLE "form_submissions_internal_notes"
      ADD CONSTRAINT "form_submissions_internal_notes_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."form_submissions"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

    ALTER TABLE "form_submissions_tags"
      ADD CONSTRAINT "form_submissions_tags_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."form_submissions"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

    -- Create indexes
    CREATE INDEX "form_submissions_status_idx" ON "form_submissions" USING btree ("status");
    CREATE INDEX "form_submissions_priority_idx" ON "form_submissions" USING btree ("priority");
    CREATE INDEX "form_submissions_assigned_to_idx" ON "form_submissions" USING btree ("assigned_to_id");
    CREATE INDEX "form_submissions_internal_notes_order_idx" ON "form_submissions_internal_notes" USING btree ("_order");
    CREATE INDEX "form_submissions_internal_notes_parent_id_idx" ON "form_submissions_internal_notes" USING btree ("_parent_id");
    CREATE INDEX "form_submissions_tags_order_idx" ON "form_submissions_tags" USING btree ("_order");
    CREATE INDEX "form_submissions_tags_parent_id_idx" ON "form_submissions_tags" USING btree ("_parent_id");
  `)
}

export async function down({ db }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
    -- Drop tables
    DROP TABLE IF EXISTS "form_submissions_tags" CASCADE;
    DROP TABLE IF EXISTS "form_submissions_internal_notes" CASCADE;

    -- Drop indexes
    DROP INDEX IF EXISTS "form_submissions_status_idx";
    DROP INDEX IF EXISTS "form_submissions_priority_idx";
    DROP INDEX IF EXISTS "form_submissions_assigned_to_idx";

    -- Drop foreign keys and columns
    ALTER TABLE "form_submissions"
      DROP CONSTRAINT IF EXISTS "form_submissions_assigned_to_id_users_id_fk",
      DROP CONSTRAINT IF EXISTS "form_submissions_read_by_id_users_id_fk",
      DROP CONSTRAINT IF EXISTS "form_submissions_response_tracking_responded_by_id_users_id_fk";

    ALTER TABLE "form_submissions"
      DROP COLUMN IF EXISTS "status",
      DROP COLUMN IF EXISTS "priority",
      DROP COLUMN IF EXISTS "assigned_to_id",
      DROP COLUMN IF EXISTS "read_at",
      DROP COLUMN IF EXISTS "read_by_id",
      DROP COLUMN IF EXISTS "response_tracking_responded",
      DROP COLUMN IF EXISTS "response_tracking_responded_at",
      DROP COLUMN IF EXISTS "response_tracking_responded_by_id",
      DROP COLUMN IF EXISTS "response_tracking_response_method",
      DROP COLUMN IF EXISTS "response_tracking_response_summary";

    -- Drop enums
    DROP TYPE IF EXISTS "public"."enum_form_submissions_status";
    DROP TYPE IF EXISTS "public"."enum_form_submissions_priority";
    DROP TYPE IF EXISTS "public"."enum_form_submissions_response_tracking_response_method";
  `)
}
