import { MigrationInterface, QueryRunner } from 'typeorm';

export class Item1718097870787 implements MigrationInterface {
  name = 'Item1718097870787';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TYPE "public"."items_status_enum" AS ENUM('active', 'cancel', 'confirmed', 'draft', 'expired', 'inactive', 'pending', 'refunded', 'rejected', 'settled', 'waiting')`,
    );
    await queryRunner.query(
      `CREATE TYPE "public"."items_item_type_enum" AS ENUM('tiket masuk', 'wahana', 'bundling', 'free gift')`,
    );
    await queryRunner.query(
      `CREATE TYPE "public"."items_limit_type_enum" AS ENUM('no limit', 'time limit', 'qty limit')`,
    );
    await queryRunner.query(
      `CREATE TABLE "items" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "creator_id" character varying(36), "creator_name" character varying(125), "editor_id" character varying(36), "editor_name" character varying(125), "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "status" "public"."items_status_enum" NOT NULL DEFAULT 'draft', "name" character varying NOT NULL, "image" character varying, "item_type" "public"."items_item_type_enum" NOT NULL DEFAULT 'tiket masuk', "hpp" bigint, "sales_margin" integer, "total_price" bigint, "base_price" bigint, "use_queue" boolean NOT NULL DEFAULT false, "show_to_booking" boolean NOT NULL DEFAULT false, "limit_type" "public"."items_limit_type_enum" NOT NULL DEFAULT 'no limit', "limit_value" integer, "item_category_id" uuid, "tenant_id" uuid, CONSTRAINT "PK_ba5885359424c15ca6b9e79bcf6" PRIMARY KEY ("id"))`,
    );
    await queryRunner.query(
      `CREATE TABLE "item_bundlings" ("item_bundling_id" uuid NOT NULL, "item_id" uuid NOT NULL, CONSTRAINT "PK_da8e062f97fe862c3e60ed929b5" PRIMARY KEY ("item_bundling_id", "item_id"))`,
    );
    await queryRunner.query(
      `CREATE INDEX "IDX_a50e7abf2caba4d0394f3726b8" ON "item_bundlings" ("item_bundling_id") `,
    );
    await queryRunner.query(
      `CREATE INDEX "IDX_edb9c8d945303a725f6b8648a0" ON "item_bundlings" ("item_id") `,
    );
    await queryRunner.query(
      `ALTER TABLE "items" ADD CONSTRAINT "FK_205e327d4c1bf3cdc00852845e6" FOREIGN KEY ("item_category_id") REFERENCES "item_categories"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
    await queryRunner.query(
      `ALTER TABLE "items" ADD CONSTRAINT "FK_d7d027b642add7f0e77c36b874f" FOREIGN KEY ("tenant_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
    await queryRunner.query(
      `ALTER TABLE "item_bundlings" ADD CONSTRAINT "FK_a50e7abf2caba4d0394f3726b86" FOREIGN KEY ("item_bundling_id") REFERENCES "items"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
    await queryRunner.query(
      `ALTER TABLE "item_bundlings" ADD CONSTRAINT "FK_edb9c8d945303a725f6b8648a00" FOREIGN KEY ("item_id") REFERENCES "items"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "item_bundlings" DROP CONSTRAINT "FK_edb9c8d945303a725f6b8648a00"`,
    );
    await queryRunner.query(
      `ALTER TABLE "item_bundlings" DROP CONSTRAINT "FK_a50e7abf2caba4d0394f3726b86"`,
    );
    await queryRunner.query(
      `ALTER TABLE "items" DROP CONSTRAINT "FK_d7d027b642add7f0e77c36b874f"`,
    );
    await queryRunner.query(
      `ALTER TABLE "items" DROP CONSTRAINT "FK_205e327d4c1bf3cdc00852845e6"`,
    );
    await queryRunner.query(
      `DROP INDEX "public"."IDX_edb9c8d945303a725f6b8648a0"`,
    );
    await queryRunner.query(
      `DROP INDEX "public"."IDX_a50e7abf2caba4d0394f3726b8"`,
    );
    await queryRunner.query(`DROP TABLE "item_bundlings"`);
    await queryRunner.query(`DROP TABLE "items"`);
    await queryRunner.query(`DROP TYPE "public"."items_limit_type_enum"`);
    await queryRunner.query(`DROP TYPE "public"."items_item_type_enum"`);
    await queryRunner.query(`DROP TYPE "public"."items_status_enum"`);
  }
}
