import { readFileSync } from 'fs';
import { join } from 'path';

const sql = readFileSync(join(__dirname, '../../prisma/migrations/20261105233000_ofertas_moneda/migration.sql'), 'utf8');
const sinComentarios = sql.replace(/--.*$/gm, '');

describe('migración 20261105233000_ofertas_moneda (B4)', () => {
  it('solo altera ofertas', () => {
    const alteradas = [...sinComentarios.matchAll(/(?:ALTER TABLE|UPDATE)\s+"?(\w+)"?/gi)].map((m) => m[1]);
    expect(new Set(alteradas)).toEqual(new Set(['ofertas']));
  });
  it('agrega moneda VARCHAR(3) nullable', () => {
    expect(sinComentarios).toMatch(/ADD COLUMN "moneda" VARCHAR\(3\);/);
    expect(sinComentarios).not.toMatch(/NOT NULL/i);
  });
  it('el UPDATE es solo de descuento_monto y precio_especial y usa es_principal con COALESCE a PYG', () => {
    expect(sinComentarios).toMatch(/WHERE o\."tipo" IN \('descuento_monto', 'precio_especial'\)/);
    expect(sinComentarios).toMatch(/"es_principal" = true/);
    expect(sinComentarios).toMatch(/COALESCE\(/);
    expect(sinComentarios).toMatch(/'PYG'\)/);
    expect(sinComentarios).toMatch(/LIMIT 1/);
  });
  it('sin DELETE, DROP ni INSERT', () => {
    expect(sinComentarios).not.toMatch(/\b(DELETE|DROP|INSERT)\b/i);
  });
});
