// Core data model for the review portal.
// Mirrors the database schema we'll create in Phase 4 (Supabase).

export const PAGE_STATUSES = [
  "open",
  "creating",
  "finalizing",
  "revision pending",
  "revising",
  "drafted",
  "completed",
] as const;

export type PageStatus = (typeof PAGE_STATUSES)[number];

// The "Review" column. Stored in the `pages.signoff` DB column for historical
// reasons; the UI labels it "Review".
export const SIGNOFF_OPTIONS = [
  "not ready",
  "ready for review",
  "revision not ready",
  "revision needed",
  "approved",
] as const;

export type Signoff = (typeof SIGNOFF_OPTIONS)[number];

export const NOTE_CATEGORIES = [
  "Revision",
  "Bug",
  "Question",
  "Live Changelog",
  "Informational",
] as const;

export type NoteCategory = (typeof NOTE_CATEGORIES)[number];

export const DEFAULT_NOTE_CATEGORY: NoteCategory = "Revision";

export interface Project {
  id: string;
  slug: string; // e.g. "brentwood"
  name: string;
  staging_domain: string | null; // e.g. "dev.brentwood.ca"
  live_domain: string | null; // e.g. "brentwood.ca"
}

// Project-level "General Issues" (not tied to a page).
export const ISSUE_STATUSES = [
  "Open",
  "Addressing",
  "Ready for Review",
  "Revision Required",
  "Address Later",
  "FYI Only",
  "Resolved",
] as const;

export type IssueStatus = (typeof ISSUE_STATUSES)[number];

export const DEFAULT_ISSUE_STATUS: IssueStatus = "Open";

export interface IssueComment {
  id: string;
  issue_id: string;
  content: string;
  image_url: string | null;
  author_email: string | null;
  created_at: string;
}

export interface Issue {
  id: string;
  project_id: string;
  category: NoteCategory;
  content: string;
  image_url: string | null;
  status: IssueStatus;
  action_to: string[]; // assignee emails
  prev_action_to: string[]; // assignees before "Ready for Review" (for restore)
  author_email: string | null;
  created_at: string;
  comments: IssueComment[];
}

export interface Comment {
  id: string;
  note_id: string;
  content: string;
  image_url: string | null; // pasted screenshot, if any
  author_email: string | null;
  created_at: string; // ISO timestamp
}

export interface Note {
  id: string;
  // A note hangs off exactly one of a page or a feature.
  page_id: string | null;
  feature_id?: string | null;
  content: string; // text / markdown
  image_url: string | null; // pasted screenshot, if any
  category: NoteCategory;
  resolved: boolean;
  author_email: string | null;
  created_at: string; // ISO timestamp
  comments: Comment[];
}

export interface StatusLog {
  id: string;
  page_id: string;
  previous_status: PageStatus | null;
  new_status: PageStatus;
  changed_at: string; // ISO timestamp
}

export interface Page {
  id: string;
  project_id: string;
  page_name: string;
  slug: string; // path appended to the project domains, e.g. "/about"
  status: PageStatus;
  signoff: Signoff;
  tags: string[];
  notes: Note[];
}

// A project plus its pages, the shape the table view consumes.
export interface ProjectWithPages extends Project {
  pages: Page[];
}

// A tracked feature. Same Status/Review columns as a page, but described by
// free-text (markdown) instead of a URL slug. Notes reuse the notes table.
export interface Feature {
  id: string;
  project_id: string;
  description: string;
  status: PageStatus;
  signoff: Signoff;
  author_email: string | null;
  created_at: string;
  notes: Note[];
}
