-- ========================================
-- 07-create-missing-structures.sql
-- Crear commercial_structure y agency_level para 71 agentes faltantes
-- ========================================

SET @parent_agent_id = 786;  -- Open Insurances
SET @parent_agreement_id = (SELECT id FROM agreement WHERE agent_id = 786 LIMIT 1);
SET @cs_id = (SELECT COALESCE(MAX(id), 0) + 1 FROM commercial_structure);
SET @al_id = (SELECT COALESCE(MAX(id), 0) + 1 FROM agency_level);

-- Agente 1000 (Agreement 968)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1000, 968, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1091 (Agreement 1059)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1091, 1059, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1093 (Agreement 1061)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1093, 1061, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1094 (Agreement 1062)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1094, 1062, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1096 (Agreement 1064)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1096, 1064, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1097 (Agreement 1065)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1097, 1065, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1098 (Agreement 1066)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1098, 1066, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1101 (Agreement 1069)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1101, 1069, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1102 (Agreement 1070)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1102, 1070, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1103 (Agreement 1071)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1103, 1071, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1104 (Agreement 1072)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1104, 1072, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1105 (Agreement 1073)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1105, 1073, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1106 (Agreement 1074)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1106, 1074, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1107 (Agreement 1075)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1107, 1075, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1108 (Agreement 1076)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1108, 1076, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1110 (Agreement 1078)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1110, 1078, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1112 (Agreement 1080)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1112, 1080, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1113 (Agreement 1081)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1113, 1081, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1115 (Agreement 1083)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1115, 1083, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1116 (Agreement 1084)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1116, 1084, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1117 (Agreement 1085)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1117, 1085, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1118 (Agreement 1086)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1118, 1086, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1119 (Agreement 1087)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1119, 1087, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1121 (Agreement 1089)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1121, 1089, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1122 (Agreement 1090)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1122, 1090, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1123 (Agreement 1091)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1123, 1091, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1124 (Agreement 1092)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1124, 1092, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1126 (Agreement 1094)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1126, 1094, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1128 (Agreement 1096)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1128, 1096, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1129 (Agreement 1097)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1129, 1097, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1130 (Agreement 1098)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1130, 1098, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1131 (Agreement 1099)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1131, 1099, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1132 (Agreement 1100)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1132, 1100, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1133 (Agreement 1101)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1133, 1101, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1134 (Agreement 1102)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1134, 1102, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1135 (Agreement 1103)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1135, 1103, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1137 (Agreement 1105)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1137, 1105, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1173 (Agreement 1141)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1173, 1141, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1196 (Agreement 1164)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1196, 1164, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1198 (Agreement 1166)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1198, 1166, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1220 (Agreement 1188)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1220, 1188, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1222 (Agreement 1190)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1222, 1190, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1228 (Agreement 1196)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1228, 1196, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1258 (Agreement 1226)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1258, 1226, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1259 (Agreement 1227)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1259, 1227, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1260 (Agreement 1228)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1260, 1228, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1262 (Agreement 1230)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1262, 1230, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1281 (Agreement 1249)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1281, 1249, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1282 (Agreement 1250)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1282, 1250, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1283 (Agreement 1251)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1283, 1251, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1284 (Agreement 1252)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1284, 1252, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1287 (Agreement 1255)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1287, 1255, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1304 (Agreement 1272)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1304, 1272, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1312 (Agreement 1280)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1312, 1280, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1313 (Agreement 1281)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1313, 1281, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1317 (Agreement 1285)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1317, 1285, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1329 (Agreement 1297)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1329, 1297, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1350 (Agreement 1318)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1350, 1318, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1351 (Agreement 1319)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1351, 1319, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1356 (Agreement 1324)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1356, 1324, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1357 (Agreement 1325)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1357, 1325, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1358 (Agreement 1326)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1358, 1326, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1359 (Agreement 1327)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1359, 1327, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1369 (Agreement 1337)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1369, 1337, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1371 (Agreement 1339)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1371, 1339, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1373 (Agreement 1341)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1373, 1341, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1374 (Agreement 1342)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1374, 1342, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1375 (Agreement 1343)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1375, 1343, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1381 (Agreement 1349)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1381, 1349, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1412 (Agreement 1380)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1412, 1380, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- Agente 1415 (Agreement 1383)

INSERT INTO commercial_structure (id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@cs_id, 1415, 1383, NOW(), NOW());

INSERT INTO agency_level (id, commercial_structure_id, general_agent_id, agreement_id, created_at, updated_at)
VALUES (@al_id, @cs_id, @parent_agent_id, @parent_agreement_id, NOW(), NOW());

SET @cs_id = @cs_id + 1;
SET @al_id = @al_id + 1;

-- ========================================
-- ACTUALIZAR REFERENCIAS INVERSAS (71 agentes)
-- ========================================

-- Actualizar agreement.commercial_structure_id para los 71 agentes recién creados
-- (aquellos que todavía tienen commercial_structure_id NULL)
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.commercial_structure_id IS NULL
  AND cs.general_agent_id IN (
    1000,1091,1093,1094,1096,1097,1098,1101,1102,1103,1104,1105,1106,1107,1108,1110,
    1112,1113,1115,1116,1117,1118,1119,1121,1122,1123,1124,1126,1128,1129,1130,1131,
    1132,1133,1134,1135,1137,1173,1196,1198,1220,1222,1228,1258,1259,1260,1262,1281,
    1282,1283,1284,1287,1304,1312,1313,1317,1329,1350,1351,1356,1357,1358,1359,1369,
    1371,1373,1374,1375,1381,1412,1415
  );

-- Actualizar commercial_structure.agent_level_id = 6 (Level 2) para los 71
UPDATE commercial_structure cs
SET cs.agent_level_id = 6
WHERE cs.agent_level_id IS NULL
  AND cs.general_agent_id IN (
    1000,1091,1093,1094,1096,1097,1098,1101,1102,1103,1104,1105,1106,1107,1108,1110,
    1112,1113,1115,1116,1117,1118,1119,1121,1122,1123,1124,1126,1128,1129,1130,1131,
    1132,1133,1134,1135,1137,1173,1196,1198,1220,1222,1228,1258,1259,1260,1262,1281,
    1282,1283,1284,1287,1304,1312,1313,1317,1329,1350,1351,1356,1357,1358,1359,1369,
    1371,1373,1374,1375,1381,1412,1415
  );
