-- Migration 052: SEO Settings Table
-- Key/value store for SEO Orchestrator configuration

CREATE TABLE IF NOT EXISTS seo_settings (
  id SERIAL PRIMARY KEY,
  key TEXT UNIQUE NOT NULL,
  value JSONB NOT NULL,
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

-- Create index on key for fast lookups
CREATE INDEX IF NOT EXISTS idx_seo_settings_key ON seo_settings(key);

-- Seed with default settings
INSERT INTO seo_settings (key, value) VALUES
  -- Section 1: Global Content Generation Settings
  ('primary_prompt_template', '"You are a cannabis industry content expert. Generate SEO-optimized content for {{page_type}} pages about {{subject}}. Focus on: {{focus_areas}}. Maintain a {{tone}} tone and keep content {{length}}."'),
  ('regeneration_prompt_template', '"Regenerate the following SEO content with fresh perspectives. Original topic: {{subject}}. Improve upon: {{improvement_areas}}. Maintain compliance with cannabis industry standards."'),
  ('default_content_length', '"medium"'),
  ('tone_voice', '"informational"'),

  -- Section 2: Automatic Refresh Rules
  ('auto_refresh_interval', '"weekly"'),
  ('trigger_pct_product_change', 'true'),
  ('trigger_pct_brand_change', 'true'),
  ('trigger_new_stores', 'true'),
  ('trigger_market_shift', 'false'),
  ('webhook_url', '""'),
  ('notify_on_trigger', 'false'),

  -- Section 3: Page-Level Defaults
  ('default_title_template', '"{{state_name}} Dispensaries | Find Cannabis Near You | CannaiQ"'),
  ('default_meta_description_template', '"Discover the best dispensaries in {{state_name}}. Browse {{dispensary_count}}+ licensed retailers, compare prices, and find cannabis products near you."'),
  ('default_slug_template', '"dispensaries-{{state_code_lower}}"'),
  ('default_og_image_template', '"/images/seo/og-{{state_code_lower}}.jpg"'),
  ('enable_ai_images', 'false'),

  -- Section 4: Crawl / Dataset Configuration
  ('primary_data_provider', '"cannaiq"'),
  ('fallback_data_provider', '"dutchie"'),
  ('min_data_freshness_hours', '24'),
  ('stale_data_behavior', '"allow_with_warning"')
ON CONFLICT (key) DO NOTHING;

-- Record migration
INSERT INTO schema_migrations (version, name, applied_at)
VALUES ('052', 'seo_settings', NOW())
ON CONFLICT (version) DO NOTHING;
