create type public.comment_status as enum ('pending','approved','rejected');
create table public.comments (
  id uuid primary key default gen_random_uuid(),
  page_key text not null check (char_length(page_key) between 3 and 180),
  parent_id uuid references public.comments(id) on delete set null,
  display_name text not null check (char_length(display_name) between 1 and 80),
  body text not null check (char_length(body) between 1 and 1200),
  website text,
  locale text not null default 'en',
  status public.comment_status not null default 'pending',
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);
create index comments_public_idx on public.comments(page_key,created_at desc) where status='approved';
alter table public.comments enable row level security;
create policy "approved comments are public" on public.comments for select using (status='approved');
-- Do not add a public INSERT policy. Submit through the validated Edge Function.
