-- ============================================
-- Script de mise à jour des répartitions analytiques
-- À exécuter dans le SQL Editor de Supabase
-- ============================================

-- Lignes normales (répartition seulement)
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Formation", "montant_ht": 4000}]'::jsonb WHERE numero_devis = 'DEV-20251229-00085';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Acquisition", "montant_ht": 7500}]'::jsonb WHERE numero_devis = 'DEV-20251215-00059';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'DEV-20260106-00100';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0006';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Formation", "montant_ht": 2200}]'::jsonb WHERE numero_devis = 'DEV-20260106-00099';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 3100}]'::jsonb WHERE numero_devis = 'DEV-20260108-00106';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 1530}, {"entite": "Acquisition", "montant_ht": 1000}]'::jsonb WHERE numero_devis = 'DEV-20251205-00037';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6430}, {"entite": "Acquisition", "montant_ht": 1090}]'::jsonb WHERE numero_devis = 'DEV-20251217-00066';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6198}]'::jsonb WHERE numero_devis = 'DEV-20251212-00057';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Formation", "montant_ht": 4400}]'::jsonb WHERE numero_devis = 'DEV-20260114-00131';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 330}, {"entite": "Acquisition", "montant_ht": 4590}]'::jsonb WHERE numero_devis = 'DEV-20251216-00064';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 330}, {"entite": "Acquisition", "montant_ht": 4590}]'::jsonb WHERE numero_devis = 'DEV-20251209-00047';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Formation", "montant_ht": 16928}]'::jsonb WHERE numero_devis = 'DEV-20251216-00065';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 29400}]'::jsonb WHERE numero_devis = 'DEV-20251212-00056';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 10140}, {"entite": "Acquisition", "montant_ht": 1390}]'::jsonb WHERE numero_devis = 'DEV-20251203-00026';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 4854}]'::jsonb WHERE numero_devis = '20250311-174608523';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Formation", "montant_ht": 3650}]'::jsonb WHERE numero_devis = 'MIG2026-0028';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5435}]'::jsonb WHERE numero_devis = '20251021-144928132';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 4400}]'::jsonb WHERE numero_devis = '20250506-143833434';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "Formation", "montant_ht": 4400}]'::jsonb WHERE numero_devis = 'DEV-20251128-00014';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0048';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 3000}, {"entite": "Formation", "montant_ht": 3450}]'::jsonb WHERE numero_devis = 'DEV-20251223-00079';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0050';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 7424}, {"entite": "Acquisition", "montant_ht": 990}]'::jsonb WHERE numero_devis = '20251117-094850005';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6728}]'::jsonb WHERE numero_devis = 'DEV-20251229-00086';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 7304}]'::jsonb WHERE numero_devis = 'MIG2026-0056';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 7744}]'::jsonb WHERE numero_devis = 'MIG2026-0059';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 7089}]'::jsonb WHERE numero_devis = '20251113-155707257';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5230}]'::jsonb WHERE numero_devis = 'DEV-20260126-00174';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5230}]'::jsonb WHERE numero_devis = 'DEV-20260126-00173';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5230}]'::jsonb WHERE numero_devis = 'DEV-20260126-00175';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 10140}, {"entite": "Acquisition", "montant_ht": 1390}]'::jsonb WHERE numero_devis = 'DEV-20251203-00029';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6600}]'::jsonb WHERE numero_devis = 'DEV-20251128-00017';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 9750}]'::jsonb WHERE numero_devis = 'DEV-20251216-00061';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6085}]'::jsonb WHERE numero_devis = 'MIG2026-0072';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 37857}]'::jsonb WHERE numero_devis = 'DEV-20251221-00076';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6612.5}, {"entite": "Acquisition", "montant_ht": 1300}]'::jsonb WHERE numero_devis = 'DEV-20251216-00062-OP2';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0078';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6464}]'::jsonb WHERE numero_devis = 'MIG2026-0079';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0080';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0081';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 10546}]'::jsonb WHERE numero_devis = 'MIG2026-0082';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0083';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6498}, {"entite": "Acquisition", "montant_ht": 990}]'::jsonb WHERE numero_devis = 'MIG2026-0085';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6410}]'::jsonb WHERE numero_devis = 'MIG2026-0086';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0087';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'MIG2026-0088';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6819}, {"entite": "Acquisition", "montant_ht": 1300}]'::jsonb WHERE numero_devis = 'DEV-20260108-00104-OCT';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6630}, {"entite": "Acquisition", "montant_ht": 990}]'::jsonb WHERE numero_devis = 'MIG2026-0089';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6085}]'::jsonb WHERE numero_devis = 'MIG2026-0093';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5435}]'::jsonb WHERE numero_devis = 'MIG2026-0096';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 8250.2}]'::jsonb WHERE numero_devis = 'DEV-20260109-00116-OP2';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 11038}, {"entite": "Acquisition", "montant_ht": 5200}]'::jsonb WHERE numero_devis = 'DEV-20260108-00104-NOV';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5230}]'::jsonb WHERE numero_devis = 'DEV-20260126-00173-NOV';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 7304}]'::jsonb WHERE numero_devis = '20250620-150028097';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5230}]'::jsonb WHERE numero_devis = 'DEV-20260126-00175-DEC';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 5230}]'::jsonb WHERE numero_devis = 'DEV-20260126-00174-DEC';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 6464}]'::jsonb WHERE numero_devis = 'MIG2026-0100';
UPDATE declarations_devis SET repartition_entites = '[{"entite": "OPECO", "montant_ht": 2900}]'::jsonb WHERE numero_devis = 'DEV-20251205-00034';

-- ============================================
-- CAS SPÉCIAUX : Correction de montant + répartition
-- ============================================

-- 20250620-145316943 : Montant 9848 → 6248€, 100% OPECO
UPDATE declarations_devis 
SET montant_total_ht = 6248, 
    repartition_entites = '[{"entite": "OPECO", "montant_ht": 6248}]'::jsonb 
WHERE numero_devis = '20250620-145316943';

-- 20251124-100153539 : Montant 7140 → 5550€, Acquisition: 3590, OPECO: 1960
UPDATE declarations_devis 
SET montant_total_ht = 5550, 
    repartition_entites = '[{"entite": "Acquisition", "montant_ht": 3590}, {"entite": "OPECO", "montant_ht": 1960}]'::jsonb 
WHERE numero_devis = '20251124-100153539';

-- DEV-20251209-00049 : Montant 10372.5 → 9282.5€, Acquisition: 1090, OPECO: 8192.5
UPDATE declarations_devis 
SET montant_total_ht = 9282.5, 
    repartition_entites = '[{"entite": "Acquisition", "montant_ht": 1090}, {"entite": "OPECO", "montant_ht": 8192.5}]'::jsonb 
WHERE numero_devis = 'DEV-20251209-00049';

-- MIG2026-0092 : Montant 135300 → 90200€, 100% OPECO
UPDATE declarations_devis 
SET montant_total_ht = 90200, 
    repartition_entites = '[{"entite": "OPECO", "montant_ht": 90200}]'::jsonb 
WHERE numero_devis = 'MIG2026-0092';

-- MIG2026-0098 : Montant 6450 → 3450€, 100% Formation
UPDATE declarations_devis 
SET montant_total_ht = 3450, 
    repartition_entites = '[{"entite": "Formation", "montant_ht": 3450}]'::jsonb 
WHERE numero_devis = 'MIG2026-0098';

-- ============================================
-- Vérification
-- ============================================
SELECT 
  numero_devis, 
  montant_total_ht,
  repartition_entites
FROM declarations_devis 
WHERE repartition_entites IS NOT NULL 
  AND repartition_entites != '[]'::jsonb
ORDER BY numero_devis
LIMIT 10;
