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

// La config de facturacion POS es por TIENDA, no por tenant: un tenant puede
// operar varias tiendas fisicas (OrdenTrabajo.tiendaId) y "computadora" en
// POSTouch resuelve a una tienda/caja puntual (Domicilio::createOrdenesEdngt
// -> login($parG) fija $par segun el computadora recibido) -- guardarla una
// sola vez por tenant mandaria las OTs de todas las tiendas a la misma
// tienda/caja de POSTouch. Mismo patron que AddTiendaPrintConfig (columnas
// taller_* agregadas a maetie, nunca tocadas por otro sistema que ya escribe
// esa tabla).
export class AddTiendaFacturacionPosConfig1780400000000
  implements MigrationInterface
{
  name = "AddTiendaFacturacionPosConfig1780400000000";

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

    if (!table) {
      return;
    }

    if (!table.findColumnByName("taller_facturacion_pos_habilitada")) {
      await queryRunner.addColumn(
        "maetie",
        new TableColumn({
          name: "taller_facturacion_pos_habilitada",
          type: "tinyint",
          width: 1,
          default: 0,
          isNullable: false,
        }),
      );
    }

    if (!table.findColumnByName("taller_facturacion_pos_url")) {
      await queryRunner.addColumn(
        "maetie",
        new TableColumn({
          name: "taller_facturacion_pos_url",
          type: "varchar",
          length: "500",
          isNullable: true,
        }),
      );
    }

    if (!table.findColumnByName("taller_facturacion_pos_username")) {
      await queryRunner.addColumn(
        "maetie",
        new TableColumn({
          name: "taller_facturacion_pos_username",
          type: "varchar",
          length: "128",
          isNullable: true,
        }),
      );
    }

    if (!table.findColumnByName("taller_facturacion_pos_password_encrypted")) {
      await queryRunner.addColumn(
        "maetie",
        new TableColumn({
          name: "taller_facturacion_pos_password_encrypted",
          type: "text",
          isNullable: true,
        }),
      );
    }

    if (!table.findColumnByName("taller_facturacion_pos_computadora")) {
      await queryRunner.addColumn(
        "maetie",
        new TableColumn({
          name: "taller_facturacion_pos_computadora",
          type: "varchar",
          length: "64",
          isNullable: true,
        }),
      );
    }

    if (!table.findColumnByName("taller_facturacion_pos_precio_decimales")) {
      await queryRunner.addColumn(
        "maetie",
        new TableColumn({
          name: "taller_facturacion_pos_precio_decimales",
          type: "int",
          isNullable: true,
        }),
      );
    }
  }

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

    if (!table) {
      return;
    }

    for (const column of [
      "taller_facturacion_pos_precio_decimales",
      "taller_facturacion_pos_computadora",
      "taller_facturacion_pos_password_encrypted",
      "taller_facturacion_pos_username",
      "taller_facturacion_pos_url",
      "taller_facturacion_pos_habilitada",
    ]) {
      if (table.findColumnByName(column)) {
        await queryRunner.dropColumn("maetie", column);
      }
    }
  }
}
