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

export class CreateTallerOtFacturacion1780100000000
  implements MigrationInterface
{
  name = "CreateTallerOtFacturacion1780100000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    const exists = await queryRunner.hasTable("taller_ot_facturacion");
    if (exists) {
      return;
    }

    await queryRunner.createTable(
      new Table({
        name: "taller_ot_facturacion",
        columns: [
          {
            name: "id",
            type: "int",
            isPrimary: true,
            isGenerated: true,
            generationStrategy: "increment",
          },
          { name: "ot_id", type: "int" },
          { name: "tienda", type: "int" },
          { name: "caja", type: "int" },
          { name: "tipo", type: "varchar", length: "10" },
          { name: "serie", type: "varchar", length: "10" },
          { name: "transac", type: "decimal", precision: 18, scale: 0 },
          {
            name: "estado_fel",
            type: "enum",
            enum: ["pendiente", "firmada", "contingencia"],
            default: "'pendiente'",
          },
          {
            name: "fecha_creacion",
            type: "datetime",
            default: "CURRENT_TIMESTAMP",
          },
          { name: "fecha_liquidacion", type: "datetime", isNullable: true },
        ],
      }),
      true,
    );

    await queryRunner.createIndex(
      "taller_ot_facturacion",
      new TableIndex({ name: "IDX_ot_facturacion_ot_id", columnNames: ["ot_id"] }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("taller_ot_facturacion", true);
  }
}
