import { MigrationInterface, QueryRunner, Table, TableIndex } from "typeorm";

export class AddPublicCotizacionFlow1761300000000
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    const cotizaciones = await queryRunner.getTable("taller_cotizaciones");
    if (cotizaciones && !cotizaciones.findColumnByName("public_token")) {
      await queryRunner.query(
        "ALTER TABLE taller_cotizaciones ADD COLUMN public_token varchar(64) NULL AFTER aprobado_por",
      );
      await queryRunner.query(
        "ALTER TABLE taller_cotizaciones ADD COLUMN public_expires_at datetime NULL AFTER public_token",
      );
      await queryRunner.query(
        "ALTER TABLE taller_cotizaciones ADD COLUMN decision_cliente_at datetime NULL AFTER public_expires_at",
      );
      await queryRunner.query(
        "ALTER TABLE taller_cotizaciones ADD COLUMN decision_cliente_nombre varchar(255) NULL AFTER decision_cliente_at",
      );
      await queryRunner.query(
        "ALTER TABLE taller_cotizaciones ADD COLUMN decision_cliente_firma text NULL AFTER decision_cliente_nombre",
      );
      await queryRunner.query(
        "ALTER TABLE taller_cotizaciones ADD COLUMN decision_cliente_motivo text NULL AFTER decision_cliente_firma",
      );
      await queryRunner.createIndex(
        "taller_cotizaciones",
        new TableIndex({
          name: "IDX_cot_public_token",
          columnNames: ["public_token"],
          isUnique: true,
        }),
      );
    }

    const versionsExists = await queryRunner.hasTable("taller_cotizacion_versiones");
    if (!versionsExists) {
      await queryRunner.createTable(
        new Table({
          name: "taller_cotizacion_versiones",
          columns: [
            { name: "id", type: "int", isPrimary: true, isGenerated: true, generationStrategy: "increment" },
            { name: "cotizacion_id", type: "int" },
            { name: "version", type: "int" },
            { name: "estado", type: "varchar", length: "50" },
            { name: "snapshot", type: "json" },
            { name: "motivo", type: "text", isNullable: true },
            { name: "created_by", type: "int", isNullable: true },
            { name: "created_at", type: "timestamp", default: "CURRENT_TIMESTAMP" },
          ],
        }),
      );
      await queryRunner.createIndex(
        "taller_cotizacion_versiones",
        new TableIndex({
          name: "IDX_cot_version_cotizacion",
          columnNames: ["cotizacion_id"],
        }),
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("taller_cotizacion_versiones", true);
    const cotizaciones = await queryRunner.getTable("taller_cotizaciones");
    if (cotizaciones?.findColumnByName("public_token")) {
      await queryRunner.dropIndex("taller_cotizaciones", "IDX_cot_public_token");
      await queryRunner.query("ALTER TABLE taller_cotizaciones DROP COLUMN decision_cliente_motivo");
      await queryRunner.query("ALTER TABLE taller_cotizaciones DROP COLUMN decision_cliente_firma");
      await queryRunner.query("ALTER TABLE taller_cotizaciones DROP COLUMN decision_cliente_nombre");
      await queryRunner.query("ALTER TABLE taller_cotizaciones DROP COLUMN decision_cliente_at");
      await queryRunner.query("ALTER TABLE taller_cotizaciones DROP COLUMN public_expires_at");
      await queryRunner.query("ALTER TABLE taller_cotizaciones DROP COLUMN public_token");
    }
  }
}
