-- ============================================================
-- BharathTrucks — Trips Migration
-- Run AFTER loads migration
-- ============================================================

CREATE TABLE IF NOT EXISTS trips (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  load_id UUID NOT NULL REFERENCES loads(id),
  driver_id UUID NOT NULL REFERENCES app_users(id),
  shipper_id UUID NOT NULL REFERENCES app_users(id),
  bid_id UUID NOT NULL REFERENCES bids(id),
  amount NUMERIC(10,2) NOT NULL,
  status TEXT DEFAULT 'confirmed' CHECK (status IN ('confirmed', 'picked_up', 'in_transit', 'delivered', 'cancelled')),
  picked_up_at TIMESTAMPTZ,
  delivered_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_trips_driver ON trips(driver_id);
CREATE INDEX IF NOT EXISTS idx_trips_shipper ON trips(shipper_id);
CREATE INDEX IF NOT EXISTS idx_trips_status ON trips(status);

ALTER TABLE trips ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow all on trips" ON trips FOR ALL USING (true) WITH CHECK (true);
