import { MigrationInterface, QueryRunner } from "typeorm"

export class CreateProjectPartyAssignmentsTable1763702047666 implements MigrationInterface {
  name = "CreateProjectPartyAssignmentsTable1763702047666"

  public async up(queryRunner: QueryRunner): Promise<void> {
    // 1️⃣ Create ENUM first
    await queryRunner.query(`
            CREATE TYPE "public"."project_party_assignments_party_type_enum"
            AS ENUM ('vendor', 'contractor', 'consultant')
        `)

    // 2️⃣ Create table using the ENUM
    await queryRunner.query(`
            CREATE TABLE "project_party_assignments" (
                "created_by" integer,
                "updated_by" integer,
                "deleted_by" integer,
                "created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
                "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(),
                "deleted_at" TIMESTAMP,
                "id" SERIAL NOT NULL,
                "project_id" integer NOT NULL,
                "party_id" integer NOT NULL,
                "party_type" "public"."project_party_assignments_party_type_enum" NOT NULL,
                CONSTRAINT "PK_9c7127d4c3542f38f31e13dc935" PRIMARY KEY ("id")
            )
        `)

    await queryRunner.query(`
            ALTER TABLE "project_party_assignments" 
            ADD CONSTRAINT "FK_723936673f82d9cd8162ce2e7b5" 
            FOREIGN KEY ("project_id") REFERENCES "projects"("id")
            ON DELETE NO ACTION ON UPDATE NO ACTION
        `)
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            ALTER TABLE "project_party_assignments" 
            DROP CONSTRAINT "FK_723936673f82d9cd8162ce2e7b5"
        `)

    await queryRunner.query(`DROP TABLE "project_party_assignments"`)

    // Drop ENUM
    await queryRunner.query(`
            DROP TYPE "public"."project_party_assignments_party_type_enum"
        `)
  }
}
