-- ImageHub: structured review decisions. The task pipeline applies accept/acceptWithCorrections/
-- reject moves, but until now the verdict survived only as a free-text Vietnamese audit string —
-- not queryable, no reviewer/stage FK, no reject reason. This append-only table records every
-- review decision so review history + per-reviewer accept/reject counters become real. Idempotent.
-- Apply after 024 (no semicolons inside comments or string literals — the runner splitter is naive):
--   docker exec -i initiative-postgres psql -U initiative -d initiatives < be0/migrations/025_imagehub_task_review_events.sql

CREATE TABLE IF NOT EXISTS imagehub_task_review_events (
  id BIGSERIAL PRIMARY KEY,
  dataset_id UUID NOT NULL REFERENCES imagehub_datasets(id) ON DELETE CASCADE,
  task_id UUID NOT NULL REFERENCES imagehub_tasks(id) ON DELETE CASCADE,
  stage_id UUID REFERENCES imagehub_dataset_stages(id) ON DELETE SET NULL,
  reviewer_user_id UUID REFERENCES users(id) ON DELETE SET NULL,
  decision TEXT NOT NULL CHECK (decision IN ('accept','acceptWithCorrections','reject')),
  note TEXT NOT NULL DEFAULT '',
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- Per-reviewer counters over a date window (the productivity panel query).
CREATE INDEX IF NOT EXISTS idx_imagehub_review_events_reviewer
  ON imagehub_task_review_events (dataset_id, reviewer_user_id, created_at);

-- A task's review history (chronological).
CREATE INDEX IF NOT EXISTS idx_imagehub_review_events_task
  ON imagehub_task_review_events (task_id, created_at);
