-- Trips: group transactions (via transaction_overrides.trip_id) into named
-- trips with analytics. Catch-up migration — this DDL was applied directly to
-- the live DB when the feature shipped; idempotent so re-running is safe.

CREATE TABLE IF NOT EXISTS trips (
  id          SERIAL PRIMARY KEY,
  owner_id    INTEGER NOT NULL REFERENCES participants(id),
  name        VARCHAR(255) NOT NULL,
  description TEXT,
  start_date  DATE,
  end_date    DATE,
  color       VARCHAR(20) NOT NULL DEFAULT '#6366f1',
  archived    BOOLEAN NOT NULL DEFAULT false,
  created_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

ALTER TABLE transaction_overrides
  ADD COLUMN IF NOT EXISTS trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_tx_overrides_trip_id
  ON transaction_overrides (trip_id) WHERE trip_id IS NOT NULL;
