import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";

// Ambas nullable y aditivas: las OT existentes quedan sin pago confirmado,
// que solo se exige cuando la facturacion aplica a la OT (parametro
// facturacion.exige_pago_para_entrega), asi que ningun flujo actual cambia.
export class AddPagoConfirmadoToOrdenTrabajo1781100000000 implements MigrationInterface {
  name = "AddPagoConfirmadoToOrdenTrabajo1781100000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable("taller_ordenes_trabajo");
    if (!table) return;

    if (!table.findColumnByName("pago_confirmado_at")) {
      await queryRunner.addColumn(
        "taller_ordenes_trabajo",
        new TableColumn({
          name: "pago_confirmado_at",
          type: "datetime",
          isNullable: true,
        }),
      );
    }
    if (!table.findColumnByName("pago_confirmado_por")) {
      await queryRunner.addColumn(
        "taller_ordenes_trabajo",
        new TableColumn({
          name: "pago_confirmado_por",
          type: "int",
          isNullable: true,
        }),
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable("taller_ordenes_trabajo");
    if (!table) return;

    if (table.findColumnByName("pago_confirmado_por")) {
      await queryRunner.dropColumn(
        "taller_ordenes_trabajo",
        "pago_confirmado_por",
      );
    }
    if (table.findColumnByName("pago_confirmado_at")) {
      await queryRunner.dropColumn(
        "taller_ordenes_trabajo",
        "pago_confirmado_at",
      );
    }
  }
}
