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

// maeplu is an external ERP table, not created by this codebase — additive
// only. es_taller/es_producto are not part of the ERP master schema; some
// client databases already have them added by hand outside of migrations.
export class AddEsTallerEsProductoToMaeplu1761800000000
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable("maeplu");
    if (!table) {
      return;
    }

    if (!table.findColumnByName("es_taller")) {
      await queryRunner.addColumn(
        "maeplu",
        new TableColumn({
          name: "es_taller",
          type: "char",
          length: "1",
          isNullable: true,
          default: "'N'",
        }),
      );
    }

    if (!table.findColumnByName("es_producto")) {
      await queryRunner.addColumn(
        "maeplu",
        new TableColumn({
          name: "es_producto",
          type: "char",
          length: "1",
          isNullable: true,
          default: "'N'",
        }),
      );
    }
  }

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

    if (table.findColumnByName("es_producto")) {
      await queryRunner.dropColumn("maeplu", "es_producto");
    }

    if (table.findColumnByName("es_taller")) {
      await queryRunner.dropColumn("maeplu", "es_taller");
    }
  }
}
