-- Al FINAL del script 04, agregar:

-- ========================================
-- ACTUALIZAR REFERENCIAS INVERSAS
-- ========================================

-- 1. Actualizar agreement.commercial_structure_id
UPDATE agreement a
INNER JOIN commercial_structure cs ON cs.agreement_id = a.id AND cs.general_agent_id = a.agent_id
SET a.commercial_structure_id = cs.id
WHERE a.id >= (SELECT MAX(id) - 419 FROM agreement);

-- 2. Actualizar commercial_structure.agent_level_id = 6 (Level 2)
UPDATE commercial_structure cs
SET cs.agent_level_id = 6
WHERE cs.general_agent_id >= (SELECT MAX(id) - 419 FROM general_agent)
  AND cs.general_agent_id <= (SELECT MAX(id) FROM general_agent);

