import { MigrationInterface, QueryRunner } from "typeorm";

// Toggles administrables por tenant: "¿este cliente en verdad necesita
// tiendas propias / catálogo de productos propio / facturación POS?".
// Existen para que el checklist operativo (getResumenOperativo) y el gate de
// "Activar" (activarTenant) dejen de marcar como "falta esto" algo que el
// cliente nunca va a usar -- antes de esto, cualquier tenant con 0 tiendas
// facturando se veía igual, sin importar si eso era un olvido o una
// decisión explícita del negocio.
//
// DEFAULT 1 (true) en las tres: onboarding hoy asume que un taller normal
// SI usa tiendas, SI crea sus propios productos y SI factura -- son la
// excepción, no la regla. Poner DEFAULT 0 hubiera "apagado" retroactivamente
// el gate para cualquier tenant que ya pasó por Activar sin este chequeo.
export class AddOperationalTogglesToTenantConfig1780600000000
  implements MigrationInterface
{
  name = "AddOperationalTogglesToTenantConfig1780600000000";

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

    if (!table.findColumnByName("permite_tiendas")) {
      await queryRunner.query(
        "ALTER TABLE tenant_config ADD COLUMN permite_tiendas TINYINT(1) NOT NULL DEFAULT 1",
      );
    }
    if (!table.findColumnByName("permite_productos_propios")) {
      await queryRunner.query(
        "ALTER TABLE tenant_config ADD COLUMN permite_productos_propios TINYINT(1) NOT NULL DEFAULT 1",
      );
    }
    if (!table.findColumnByName("factura_habilitada")) {
      await queryRunner.query(
        "ALTER TABLE tenant_config ADD COLUMN factura_habilitada TINYINT(1) NOT NULL DEFAULT 1",
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE tenant_config
        DROP COLUMN permite_tiendas,
        DROP COLUMN permite_productos_propios,
        DROP COLUMN factura_habilitada
    `);
  }
}
