import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddOrderEntity1697108572307 implements MigrationInterface {
	name = 'AddOrderEntity1697108572307'

	public async up(queryRunner: QueryRunner): Promise<void> {
		await queryRunner.query(`CREATE TYPE "public"."order_state_enum" AS ENUM('open', 'done', 'cancelled')`)
		await queryRunner.query(
			`CREATE TABLE "order" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "tenantId" uuid, "createdBy" uuid, "lastUpdatedAt" TIMESTAMP NOT NULL DEFAULT now(), "lastUpdatedBy" uuid, "deletedAt" TIMESTAMP, "deletedBy" uuid, "currencyCode" character varying(3) NOT NULL, "outstandingAmount" numeric NOT NULL, "paidAmount" numeric NOT NULL, "baseAmount" numeric NOT NULL, "taxPercentage" numeric NOT NULL, "taxAmount" numeric NOT NULL, "totalAmount" numeric NOT NULL, "state" "public"."order_state_enum" NOT NULL DEFAULT 'open', "tableId" uuid NOT NULL, CONSTRAINT "PK_1031171c13130102495201e3e20" PRIMARY KEY ("id"))`,
		)
		await queryRunner.query(
			`CREATE TYPE "public"."order_item_quantityunit_enum" AS ENUM('ml', 'l', 'mg', 'g', 'kg', 'pcs')`,
		)
		await queryRunner.query(
			`CREATE TYPE "public"."order_item_state_enum" AS ENUM('pending', 'ready_to_cook', 'cooking', 'ready_to_serve', 'served', 'done', 'rejected')`,
		)
		await queryRunner.query(
			`CREATE TABLE "order_item" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "tenantId" uuid, "createdBy" uuid, "lastUpdatedAt" TIMESTAMP NOT NULL DEFAULT now(), "lastUpdatedBy" uuid, "deletedAt" TIMESTAMP, "deletedBy" uuid, "name" character varying(60) NOT NULL, "quantity" numeric NOT NULL, "quantityUnit" "public"."order_item_quantityunit_enum" NOT NULL DEFAULT 'pcs', "unitPrice" numeric NOT NULL, "paidQuantity" numeric NOT NULL, "paid" boolean NOT NULL DEFAULT false, "state" "public"."order_item_state_enum" NOT NULL DEFAULT 'pending', "course" integer, "orderId" uuid NOT NULL, "menuItemId" uuid NOT NULL, CONSTRAINT "PK_d01158fe15b1ead5c26fd7f4e90" PRIMARY KEY ("id"))`,
		)
		await queryRunner.query(
			`ALTER TABLE "order" ADD CONSTRAINT "FK_a9757413db9333d4bb21a2a42aa" FOREIGN KEY ("tableId") REFERENCES "table"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
		)
		await queryRunner.query(
			`ALTER TABLE "order_item" ADD CONSTRAINT "FK_646bf9ece6f45dbe41c203e06e0" FOREIGN KEY ("orderId") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
		)
		await queryRunner.query(
			`ALTER TABLE "order_item" ADD CONSTRAINT "FK_caa901372ba1b5aa30d1950b458" FOREIGN KEY ("menuItemId") REFERENCES "menu_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
		)
	}

	public async down(queryRunner: QueryRunner): Promise<void> {
		await queryRunner.query(`ALTER TABLE "order_item" DROP CONSTRAINT "FK_caa901372ba1b5aa30d1950b458"`)
		await queryRunner.query(`ALTER TABLE "order_item" DROP CONSTRAINT "FK_646bf9ece6f45dbe41c203e06e0"`)
		await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_a9757413db9333d4bb21a2a42aa"`)
		await queryRunner.query(`DROP TABLE "order_item"`)
		await queryRunner.query(`DROP TYPE "public"."order_item_state_enum"`)
		await queryRunner.query(`DROP TYPE "public"."order_item_quantityunit_enum"`)
		await queryRunner.query(`DROP TABLE "order"`)
		await queryRunner.query(`DROP TYPE "public"."order_state_enum"`)
	}
}
