-- Estado: HISTORICO / TRANSICIONAL.
-- No ejecutar en entornos nuevos salvo auditoria de un split legacy ya realizado.
-- Sustituido por: validate-proforma-tax-config.sql y validate-document-series-migration.sql para el estado vigente.
-- Fase 1D
-- Validaciones SQL tras create/backfill de Proformas / Issued Invoices V2.

-- ---------------------------------------------------------------------------
-- Conteos cabeceras
-- ---------------------------------------------------------------------------

SELECT COUNT(*) AS legacy_proformas
FROM customer_invoices
WHERE is_proforma = 1
  AND deleted_at IS NULL;

SELECT COUNT(*) AS new_proformas
FROM proformas
WHERE deleted_at IS NULL;

SELECT COUNT(*) AS legacy_issued_invoices
FROM customer_invoices
WHERE is_proforma = 0
  AND deleted_at IS NULL;

SELECT COUNT(*) AS new_issued_invoices
FROM issued_invoices
WHERE deleted_at IS NULL;

-- ---------------------------------------------------------------------------
-- Conteos items
-- ---------------------------------------------------------------------------

SELECT COUNT(*) AS legacy_proforma_items
FROM customer_invoice_items i
JOIN customer_invoices ci ON ci.id = i.invoice_id
WHERE ci.is_proforma = 1
  AND ci.deleted_at IS NULL;

SELECT COUNT(*) AS new_proforma_items
FROM proforma_items;

SELECT COUNT(*) AS legacy_issued_invoice_items
FROM customer_invoice_items i
JOIN customer_invoices ci ON ci.id = i.invoice_id
WHERE ci.is_proforma = 0
  AND ci.deleted_at IS NULL;

SELECT COUNT(*) AS new_issued_invoice_items
FROM issued_invoice_items;

-- ---------------------------------------------------------------------------
-- Conteos taxes
-- ---------------------------------------------------------------------------

SELECT COUNT(*) AS legacy_proforma_taxes
FROM customer_invoice_taxes t
JOIN customer_invoices ci ON ci.id = t.invoice_id
WHERE ci.is_proforma = 1
  AND ci.deleted_at IS NULL;

SELECT COUNT(*) AS new_proforma_taxes
FROM proforma_taxes;

SELECT COUNT(*) AS legacy_issued_invoice_taxes
FROM customer_invoice_taxes t
JOIN customer_invoices ci ON ci.id = t.invoice_id
WHERE ci.is_proforma = 0
  AND ci.deleted_at IS NULL;

SELECT COUNT(*) AS new_issued_invoice_taxes
FROM issued_invoice_taxes;

-- ---------------------------------------------------------------------------
-- Duplicados fiscales
-- ---------------------------------------------------------------------------

SELECT company_id, invoice_series_code, invoice_number, COUNT(*) AS total
FROM issued_invoices
GROUP BY company_id, invoice_series_code, invoice_number
HAVING COUNT(*) > 1;

-- ---------------------------------------------------------------------------
-- Huérfanos
-- ---------------------------------------------------------------------------

SELECT COUNT(*) AS issued_without_source
FROM issued_invoices ii
LEFT JOIN proformas p ON p.id = ii.source_proforma_id
WHERE ii.source_proforma_id IS NOT NULL
  AND p.id IS NULL;

SELECT COUNT(*) AS proforma_items_without_header
FROM proforma_items pi
LEFT JOIN proformas p ON p.id = pi.proforma_id
WHERE p.id IS NULL;

SELECT COUNT(*) AS issued_items_without_header
FROM issued_invoice_items iii
LEFT JOIN issued_invoices ii ON ii.id = iii.issued_invoice_id
WHERE ii.id IS NULL;

SELECT COUNT(*) AS proforma_taxes_without_header
FROM proforma_taxes pt
LEFT JOIN proformas p ON p.id = pt.proforma_id
WHERE p.id IS NULL;

SELECT COUNT(*) AS issued_taxes_without_header
FROM issued_invoice_taxes iit
LEFT JOIN issued_invoices ii ON ii.id = iit.issued_invoice_id
WHERE ii.id IS NULL;

-- ---------------------------------------------------------------------------
-- Verifactu
-- ---------------------------------------------------------------------------

SELECT COUNT(*) AS orphan_verifactu_records
FROM verifactu_records vr
LEFT JOIN issued_invoices ii ON ii.id = vr.issued_invoice_id
WHERE vr.issued_invoice_id IS NOT NULL
  AND ii.id IS NULL;

SELECT COUNT(*) AS legacy_invoice_links_pending
FROM verifactu_records vr
JOIN customer_invoices ci ON ci.id = vr.invoice_id
WHERE ci.is_proforma = 0
  AND vr.invoice_id IS NOT NULL
  AND vr.issued_invoice_id IS NULL;

-- ---------------------------------------------------------------------------
-- Totales monetarios
-- ---------------------------------------------------------------------------

SELECT
  SUM(ci.total_amount_value) AS legacy_proformas_total_amount_value,
  SUM(p.total_amount_value) AS new_proformas_total_amount_value
FROM customer_invoices ci
LEFT JOIN proformas p ON p.id = ci.id
WHERE ci.is_proforma = 1
  AND ci.deleted_at IS NULL;

SELECT
  SUM(ci.total_amount_value) AS legacy_issued_total_amount_value,
  SUM(ii.total_amount_value) AS new_issued_total_amount_value
FROM customer_invoices ci
LEFT JOIN issued_invoices ii ON ii.id = ci.id
WHERE ci.is_proforma = 0
  AND ci.deleted_at IS NULL;

SELECT
  SUM(ci.taxable_amount_value) AS legacy_proformas_taxable_amount_value,
  SUM(p.taxable_amount_value) AS new_proformas_taxable_amount_value
FROM customer_invoices ci
LEFT JOIN proformas p ON p.id = ci.id
WHERE ci.is_proforma = 1
  AND ci.deleted_at IS NULL;

SELECT
  SUM(ci.taxable_amount_value) AS legacy_issued_taxable_amount_value,
  SUM(ii.taxable_amount_value) AS new_issued_taxable_amount_value
FROM customer_invoices ci
LEFT JOIN issued_invoices ii ON ii.id = ci.id
WHERE ci.is_proforma = 0
  AND ci.deleted_at IS NULL;

-- ---------------------------------------------------------------------------
-- Enlaces clave de negocio
-- ---------------------------------------------------------------------------

SELECT
  ii.id,
  ii.source_proforma_id,
  p.id AS matched_proforma_id
FROM issued_invoices ii
LEFT JOIN proformas p ON p.id = ii.source_proforma_id
WHERE ii.source_proforma_id IS NOT NULL
  AND p.id IS NULL;
