-- Migration pour corriger les contrats où structure = production_name
-- Ces contrats ont été créés/modifiés avec un bug qui mettait la production dans structure

-- Mettre à jour le contrat "La Nuit avant Noël" spécifique
UPDATE cddu_contracts
SET structure = organizations.name
FROM organizations
WHERE cddu_contracts.org_id = organizations.id
  AND cddu_contracts.id = '9240863d-be8a-449a-918f-68a77eff3bed'
  AND cddu_contracts.structure = 'La Nuit avant Noël';

-- Corriger tous les autres contrats potentiellement affectés
-- (où structure = production_name et structure != organization.name)
UPDATE cddu_contracts
SET structure = organizations.name
FROM organizations
WHERE cddu_contracts.org_id = organizations.id
  AND cddu_contracts.structure = cddu_contracts.production_name
  AND cddu_contracts.structure != organizations.name;

-- Vérification: afficher les contrats mis à jour
SELECT 
  id,
  contract_number,
  structure,
  production_name,
  org_id,
  employee_name,
  start_date
FROM cddu_contracts
WHERE structure != (SELECT name FROM organizations WHERE id = cddu_contracts.org_id)
ORDER BY created_at DESC
LIMIT 20;
