import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "transaction_item_taxes" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "tax_id" character varying, "tax_name" character varying, "taxt_value" numeric, "tax_total_value" numeric, "transaction_id" uuid, CONSTRAINT "PK_fc5f6da61b24eb5bfdd503b0a0d" PRIMARY KEY ("id"))`,
    );
    await queryRunner.query(
      `CREATE TABLE "t_breakdown_item_taxes" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "tax_id" character varying, "tax_name" character varying, "taxt_value" numeric, "tax_total_value" numeric, "transaction_id" uuid, CONSTRAINT "PK_a1ef08d2c68169a50102aa70eca" PRIMARY KEY ("id"))`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_items" ADD "total_profit_share" numeric`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" ADD "total_profit_share" numeric`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_taxes" ADD CONSTRAINT "FK_f5c4966a381d903899cafb4b5ba" FOREIGN KEY ("transaction_id") REFERENCES "transaction_items"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
    await queryRunner.query(
      `ALTER TABLE "t_breakdown_item_taxes" ADD CONSTRAINT "FK_74bedce7e94f6707ddf26ef0c0f" FOREIGN KEY ("transaction_id") REFERENCES "transaction_item_breakdowns"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_items" ADD "payment_total_dpp" numeric`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" ADD "payment_total_dpp" numeric`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_items" ADD "payment_total_tax" numeric`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" ADD "payment_total_tax" numeric`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" ADD "total_share_tenant" numeric`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "t_breakdown_item_taxes" DROP CONSTRAINT "FK_74bedce7e94f6707ddf26ef0c0f"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_taxes" DROP CONSTRAINT "FK_f5c4966a381d903899cafb4b5ba"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "total_profit_share"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_items" DROP COLUMN "total_profit_share"`,
    );
    await queryRunner.query(`DROP TABLE "t_breakdown_item_taxes"`);
    await queryRunner.query(`DROP TABLE "transaction_item_taxes"`);
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "payment_total_dpp"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_items" DROP COLUMN "payment_total_dpp"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "payment_total_tax"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_items" DROP COLUMN "payment_total_tax"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "total_share_tenant"`,
    );
  }
}
