import type { Pool } from 'pg';
import { prisma } from '../config/database.js';

export async function getAllRegimenes() {
  return prisma.regimen.findMany({
    where: { activo: true },
    orderBy: { clave: 'asc' },
  });
}

export async function getRegimenesIgnorados(tenantId: string) {
  const rows = await prisma.tenantRegimenIgnorado.findMany({
    where: { tenantId },
    include: { regimen: true },
    orderBy: { regimen: { clave: 'asc' } },
  });
  return rows.map(r => r.regimen);
}

export async function getRegimenesIgnoradosClaves(tenantId: string): Promise<string[]> {
  const rows = await prisma.tenantRegimenIgnorado.findMany({
    where: { tenantId },
    include: { regimen: { select: { clave: true } } },
  });
  return rows.map(r => r.regimen.clave);
}

export async function getRegimenesActivos(tenantId: string) {
  const rows = await prisma.tenantRegimenActivo.findMany({
    where: { tenantId },
    include: { regimen: true },
    orderBy: { regimen: { clave: 'asc' } },
  });
  return rows.map(r => r.regimen);
}

export async function getRegimenesActivosClaves(tenantId: string): Promise<string[]> {
  const rows = await prisma.tenantRegimenActivo.findMany({
    where: { tenantId },
    include: { regimen: { select: { clave: true } } },
  });
  return rows.map(r => r.regimen.clave);
}

/**
 * Resuelve las claves de regímenes activos para la alerta de discrepancia.
 * Si hay contribuyenteId, lee de contribuyentes.regimen_fiscal (comma-separated).
 * Si no, fallback a TenantRegimenActivo (tabla central).
 */
export async function getRegimenesActivosClavesEfectivos(
  tenantId: string,
  pool: Pool,
  contribuyenteId?: string | null,
): Promise<string[]> {
  if (contribuyenteId) {
    const safeId = contribuyenteId.replace(/[^a-f0-9-]/gi, '');
    const { rows } = await pool.query(
      `SELECT regimen_fiscal FROM contribuyentes WHERE entidad_id = $1`,
      [safeId],
    );
    if (rows.length > 0 && rows[0].regimen_fiscal) {
      return rows[0].regimen_fiscal.split(',').map((c: string) => c.trim()).filter(Boolean);
    }
    return [];
  }
  return getRegimenesActivosClaves(tenantId);
}

export async function setRegimenesActivos(tenantId: string, regimenIds: number[]) {
  await prisma.tenantRegimenActivo.deleteMany({ where: { tenantId } });

  if (regimenIds.length > 0) {
    await prisma.tenantRegimenActivo.createMany({
      data: regimenIds.map(regimenId => ({ tenantId, regimenId })),
    });
  }

  return getRegimenesActivos(tenantId);
}

export async function setRegimenesIgnorados(tenantId: string, regimenIds: number[]) {
  // Delete all existing and re-insert
  await prisma.tenantRegimenIgnorado.deleteMany({ where: { tenantId } });

  if (regimenIds.length > 0) {
    await prisma.tenantRegimenIgnorado.createMany({
      data: regimenIds.map(regimenId => ({ tenantId, regimenId })),
    });
  }

  return getRegimenesIgnorados(tenantId);
}
