-- edr_dev reset: drop every contract booking, every schedule/built train,
-- and hand the whole fleet back as AVAILABLE.
-- Deliberately KEPT: contracts, companies, rates, yards, routes, the 5 wagons
-- and 7 locomotives parked in MAINTENANCE / OUT_OF_SERVICE, and the single
-- contract-less draft booking BK-2026-000011.
BEGIN;

-- 1. Detach the fleet FIRST — wagons point at trains, consists and schedules.
UPDATE freight.wagons
   SET train_id = NULL,
       current_train_schedule_id = NULL,
       train_set_wagon_id = NULL,
       sequence_number = NULL,
       import_train_number = NULL,
       export_train_number = NULL,
       status = 'AVAILABLE',
       updated_at = now()
 WHERE deleted_at IS NULL
   AND status <> 'MAINTENANCE';

UPDATE freight.locomotives
   SET status = 'AVAILABLE',
       updated_at = now()
 WHERE deleted_at IS NULL
   AND status NOT IN ('MAINTENANCE', 'OUT_OF_SERVICE');

-- 2. Booking-scoped rows (allocations, batch offers, movements, documents).
DELETE FROM freight.train_schedule_bookings;
DELETE FROM freight.booking_batch_offers;
DELETE FROM freight.wagon_booking_allocations;
DELETE FROM freight.wagon_allocation_bulk_loads;
DELETE FROM freight.booking_container_allocations;
DELETE FROM freight.wagon_movements;
DELETE FROM freight.customer_truck_containers;
DELETE FROM freight.customer_truck_assignments;
DELETE FROM freight.interchange_document_items;
DELETE FROM freight.facility_handling_events;
DELETE FROM freight.booking_handovers;
DELETE FROM freight.clearance_incidents;
DELETE FROM freight.clearance_milestones WHERE booking_id IS NOT NULL;
DELETE FROM freight.booking_document_review;
DELETE FROM freight.booking_contract_signatures;
DELETE FROM freight.booking_review_note;
DELETE FROM freight.booking_cargo_modifier;
DELETE FROM freight.booking_rate_snapshot;
DELETE FROM freight.containers WHERE booking_id IS NOT NULL;
DELETE FROM freight.booking_container;
DELETE FROM freight.cargoes WHERE booking_id IS NOT NULL;
DELETE FROM freight.first_mile;
DELETE FROM freight.last_mile;
DELETE FROM freight.booking_requests;

-- 3. Billing raised for those bookings.
DELETE FROM freight.invoice_lines
 WHERE invoice_id IN (SELECT id FROM freight.invoices WHERE source = 'booking');
UPDATE freight.invoices SET payment_id = NULL WHERE source = 'booking';
DELETE FROM freight.payment_refunds;
DELETE FROM freight.invoices WHERE source = 'booking';
DELETE FROM freight.payments;

-- 4. The bookings themselves (self-FK cleared first).
UPDATE freight.bookings SET consolidation_partner_id = NULL WHERE contract_id IS NOT NULL;
DELETE FROM freight.bookings WHERE contract_id IS NOT NULL;

-- 5. Schedules, built trains and consists.
DELETE FROM freight.train_checkpoint_events;
DELETE FROM freight.djibouti_import_incidents;
DELETE FROM freight.import_djibouti_operations;
DELETE FROM freight.train_schedules;
DELETE FROM freight.train_set_wagons;
DELETE FROM freight.train_set_locomotives;
DELETE FROM freight.train_sets;
DELETE FROM freight.train_locomotives;
DELETE FROM freight.trains;

COMMIT;
