-- Newsletter digest templates
CREATE TABLE IF NOT EXISTS public.newsletter_templates (
  id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  name text NOT NULL,
  description text,
  layout text NOT NULL DEFAULT 'standard', -- standard | featured | minimal | two-column
  style text NOT NULL DEFAULT 'dark',       -- dark | light | branded
  subject_prefix text DEFAULT '',
  header_text text DEFAULT '',
  footer_text text DEFAULT '',
  accent_color text DEFAULT '#3b82f6',
  article_blocks jsonb DEFAULT '[]'::jsonb,
  is_preset boolean DEFAULT false,
  created_at timestamptz DEFAULT now(),
  updated_at timestamptz DEFAULT now()
);

-- RLS
ALTER TABLE public.newsletter_templates ENABLE ROW LEVEL SECURITY;

-- Authenticated users (admins) can do everything
CREATE POLICY "Authenticated users can manage templates"
  ON public.newsletter_templates
  FOR ALL
  TO authenticated
  USING (true)
  WITH CHECK (true);

-- Seed preset templates
INSERT INTO public.newsletter_templates (name, description, layout, style, subject_prefix, header_text, footer_text, accent_color, article_blocks, is_preset)
VALUES
(
  'Weekly Digest',
  'Classic weekly roundup with featured lead story and supporting articles',
  'featured',
  'dark',
  'BroadcastBeat Weekly —',
  'Your weekly roundup of the most important stories in broadcast engineering.',
  'Thanks for reading. Forward this to a colleague who''d find it useful.',
  '#3b82f6',
  '[{"title":"","excerpt":"","url":"","category":"","featured":true},{"title":"","excerpt":"","url":"","category":"","featured":false},{"title":"","excerpt":"","url":"","category":"","featured":false}]'::jsonb,
  true
),
(
  'Breaking News Flash',
  'Urgent single-story alert with bold header and call-to-action',
  'minimal',
  'dark',
  'BREAKING:',
  'Important update from BroadcastBeat.',
  'Stay tuned for further updates.',
  '#ef4444',
  '[{"title":"","excerpt":"","url":"","category":"","featured":true}]'::jsonb,
  true
),
(
  'Product Spotlight',
  'Two-column layout showcasing new products and gear reviews',
  'two-column',
  'dark',
  'New in Broadcast Tech —',
  'The latest products, gear reviews, and technology launches.',
  'Have a product to feature? Contact our editorial team.',
  '#8b5cf6',
  '[{"title":"","excerpt":"","url":"","category":""},{"title":"","excerpt":"","url":"","category":""},{"title":"","excerpt":"","url":"","category":""},{"title":"","excerpt":"","url":"","category":""}]'::jsonb,
  true
),
(
  'Event Roundup',
  'Post-event summary with highlights and key takeaways',
  'standard',
  'dark',
  'Event Recap —',
  'Here''s what happened and what it means for the industry.',
  'See you at the next event.',
  '#10b981',
  '[{"title":"","excerpt":"","url":"","category":""},{"title":"","excerpt":"","url":"","category":""},{"title":"","excerpt":"","url":"","category":""}]'::jsonb,
  true
),
(
  'Monthly Industry Report',
  'Comprehensive monthly digest with multiple categories and analysis',
  'standard',
  'dark',
  'BroadcastBeat Monthly —',
  'Your comprehensive monthly briefing on broadcast engineering trends, products, and industry news.',
  'This report is compiled by the BroadcastBeat editorial team. Share with your network.',
  '#f59e0b',
  '[{"title":"","excerpt":"","url":"","category":"Industry News"},{"title":"","excerpt":"","url":"","category":"Products"},{"title":"","excerpt":"","url":"","category":"Technology"},{"title":"","excerpt":"","url":"","category":"Events"},{"title":"","excerpt":"","url":"","category":"Analysis"}]'::jsonb,
  true
);
