-- Arrange-data for flows/government_preemption.cy.ts. Idempotent.
--
-- The real government-company seeder (GovCompaniesSeeder /
-- gov-companies.data.ts) is NOT wired into app.module.ts (commented out —
-- an in-progress feature), so the e2e boot never creates it. This fixture
-- inserts the SAME fixed row (id/tin) as GOV_COMPANIES[0] ("Federal
-- Government of Ethiopia") — a no-op duplicate-safe insert if that seeder is
-- ever turned back on, and the only way e2e can book a government booking
-- today (POST /bookings requires companyId to resolve to a
-- kind='government', status='active' company + an active company profile).

INSERT INTO freight.companies
  (id, name, type, kind, status, tin, country, email, phone)
SELECT '0a1b0001-0000-4000-8000-000000000001'::uuid, 'Federal Government of Ethiopia',
       'customer', 'government', 'active', '0000000001', 'Ethiopia',
       'procurement@gov.et', '+251111000001'
WHERE NOT EXISTS (
  SELECT 1 FROM freight.companies WHERE id = '0a1b0001-0000-4000-8000-000000000001'::uuid
);

INSERT INTO freight.company_profiles
  (id, company_id, type, reference, status)
SELECT '0b1c0001-0000-4000-8000-000000000001'::uuid,
       '0a1b0001-0000-4000-8000-000000000001'::uuid, 'importer', 'IM-90001', 'active'
WHERE NOT EXISTS (
  SELECT 1 FROM freight.company_profiles WHERE id = '0b1c0001-0000-4000-8000-000000000001'::uuid
);

-- Dedicated 3-wagon BUILT container train at DJIB_PORT (the default import
-- corridor's origin). A loco-pair schedule's max_wagons is NOT a real cap —
-- syncScheduleMaxWagons recomputes it from the locomotive's length every fill
-- pass (54 for a standard-length loco), ignoring maxWagonsPerTrain entirely.
-- A BUILT train's coupled wagon count IS the cap, immune to that recompute
-- (see seed-adjust-consist.sql for the same pattern) — the only way to get a
-- genuinely small, exact-fit train for the preemption scenario below.
INSERT INTO freight.locomotives
  (id, code, max_pull_weight_tons, max_train_length_meters, current_yard_id)
SELECT gen_random_uuid(), v.code, 9000, 760, y.id
FROM (VALUES ('LOCO-GOV-A'), ('LOCO-GOV-B')) AS v(code)
JOIN freight.yards y ON y.code = 'DJIB_PORT'
WHERE NOT EXISTS (SELECT 1 FROM freight.locomotives l WHERE l.code = v.code);

INSERT INTO freight.trains (id, code, train_name, capacity_tons, current_yard_id)
SELECT gen_random_uuid(), 'TRN-GOV-1', 'E2E Government Preemption Carrier', 300, y.id
FROM freight.yards y WHERE y.code = 'DJIB_PORT'
  AND NOT EXISTS (SELECT 1 FROM freight.trains t WHERE t.code = 'TRN-GOV-1');

INSERT INTO freight.train_locomotives (id, train_id, locomotive_id, sequence_no)
SELECT gen_random_uuid(), t.id, l.id, v.seq
FROM (VALUES ('LOCO-GOV-A', 0), ('LOCO-GOV-B', 1)) AS v(loco_code, seq)
JOIN freight.trains t ON t.code = 'TRN-GOV-1'
JOIN freight.locomotives l ON l.code = v.loco_code
WHERE NOT EXISTS (
  SELECT 1 FROM freight.train_locomotives tl
  WHERE tl.train_id = t.id AND tl.locomotive_id = l.id
);

INSERT INTO freight.wagons (id, wagon_number, wagon_type_id, current_yard_id)
SELECT gen_random_uuid(), v.num, wt.id, y.id
FROM (VALUES ('WGN-GOV-C1', 1), ('WGN-GOV-C2', 2), ('WGN-GOV-C3', 3)) AS v(num, seq)
JOIN freight.wagon_types wt ON wt.code = 'NW5'
JOIN freight.yards y ON y.code = 'DJIB_PORT'
WHERE NOT EXISTS (SELECT 1 FROM freight.wagons w WHERE w.wagon_number = v.num);

UPDATE freight.wagons w
SET train_id = t.id, sequence_number = v.seq, status = 'ASSIGNED',
    current_yard_id = t.current_yard_id
FROM freight.trains t,
     (VALUES ('WGN-GOV-C1', 1), ('WGN-GOV-C2', 2), ('WGN-GOV-C3', 3)) AS v(num, seq)
WHERE w.wagon_number = v.num AND t.code = 'TRN-GOV-1'
  AND (w.train_id IS DISTINCT FROM t.id OR w.sequence_number IS DISTINCT FROM v.seq);
