create table if not exists public.campaign_versions (
  id uuid primary key default gen_random_uuid(),
  organization_id uuid not null references public.organizations(id) on delete cascade,
  campaign_id uuid not null references public.campaigns(id) on delete cascade,
  created_by_auth_user_id uuid references auth.users(id) on delete set null,
  name text not null,
  subject text not null,
  from_name text not null,
  from_email text not null,
  editor_mode text not null,
  html_body text,
  text_body text,
  blocks jsonb not null default '[]'::jsonb,
  save_source text not null default 'manual',
  created_at timestamptz not null default now()
);

create index if not exists campaign_versions_campaign_created_idx
  on public.campaign_versions (campaign_id, created_at desc);

create index if not exists campaign_versions_org_created_idx
  on public.campaign_versions (organization_id, created_at desc);

alter table public.campaign_versions enable row level security;
