-- ============================================================
-- HeavyHaul Agent — 2026-09-07 meeting (Tasks 53, 60, 62).
--
-- 1. Routes are delivered in THREE formats, not one (Task 53). Google Maps
--    parts already live in service_requests.route_links (migration 0005).
--    Nash: "it's 3 types of maps delivered. First one, it's a GPX file.
--    Second one, it's a Hummer GPS. And the third one is Google."
--
-- 2. Per-participant trip completion (Task 60). Nash: "this marks it
--    complete for me as the user… I don't want to force close, force
--    complete for all users this trip." Completion is per participant;
--    everyone else is only ASKED. draft / waiting_for_permits / cancelled
--    stay trip-wide — they describe permit processing, not one person's
--    involvement.
--
-- 3. Per-trip, per-user chat privacy (Task 62). Nash: "let broker and
--    dispatch see my questions and answers, yes or no… if I put no for this
--    trip, then nobody's gonna see my questions and the answers."
--    Explicitly NOT branded as an "incognito mode" — "it's nothing cool.
--    It's just share or not share."
-- ============================================================

-- 1. Route deliverables ---------------------------------------------------

alter table public.service_requests
  add column if not exists route_gpx_url text,
  add column if not exists route_hummer_url text;

comment on column public.service_requests.route_gpx_url is
  'GPX file for the truck GPS — the driver sends it over Bluetooth to the in-cab device.';
comment on column public.service_requests.route_hummer_url is
  'Deep link that opens this route in the Hummer GPS truck-navigation app.';

-- 2. Per-participant completion -------------------------------------------

alter table public.trip_participants
  add column if not exists completed_at timestamptz,
  add column if not exists completion_prompt_dismissed_at timestamptz;

comment on column public.trip_participants.completed_at is
  'When THIS participant marked the trip complete for themselves. Null = still active for them.';
comment on column public.trip_participants.completion_prompt_dismissed_at is
  'When this participant answered the "someone else completed this trip" prompt, so it stops asking.';

-- 3. Chat privacy ----------------------------------------------------------

alter table public.chat_messages
  add column if not exists is_private boolean not null default false,
  add column if not exists private_for_user_id uuid;

comment on column public.chat_messages.is_private is
  'True = visible only to private_for_user_id. An AI answer inherits the flag of the question that produced it.';
comment on column public.chat_messages.private_for_user_id is
  'The asker who owns this private message. Null when is_private is false.';

-- Which question an AI answer replies to. Needed so "also hide my earlier
-- questions" can hide the right answers: pairing by timestamp adjacency can
-- pick up another participant's answer when two people ask seconds apart.
alter table public.chat_messages
  add column if not exists answer_to_message_id uuid
    references public.chat_messages (id) on delete set null;

comment on column public.chat_messages.answer_to_message_id is
  'For an AI message: the participant question it answers.';

-- Only private rows carry an owner, and every private row has one.
alter table public.chat_messages
  drop constraint if exists chat_messages_private_owner_check;
alter table public.chat_messages
  add constraint chat_messages_private_owner_check
  check ((is_private = false and private_for_user_id is null)
      or (is_private = true and private_for_user_id is not null));

-- The chat read path filters on this on every poll (every 4s per open trip).
create index if not exists chat_messages_privacy_idx
  on public.chat_messages (trip_id, is_private, private_for_user_id);

-- Sharing is the current behaviour, so it stays the default; a user opts out
-- per trip.
alter table public.trip_participants
  add column if not exists share_chat boolean not null default true;

comment on column public.trip_participants.share_chat is
  'False = this participant''s agent questions and answers on this trip are hidden from the other participants.';
