-- ============================================
-- Add Video Source Columns for YouTube/Embed Toggle
-- ============================================
-- This migration adds support for dual video sources (YouTube and Embed)
-- at the product level, allowing quick switching between sources

-- Add video source columns to bootcamp_lessons table
ALTER TABLE bootcamp_lessons
  ADD COLUMN IF NOT EXISTS youtube_url TEXT,
  ADD COLUMN IF NOT EXISTS embed_code TEXT;

-- Migrate existing video_url to youtube_url
UPDATE bootcamp_lessons
SET youtube_url = video_url
WHERE video_url IS NOT NULL
  AND youtube_url IS NULL;

-- Note: Keep old video_url column for backward compatibility
-- Can drop after verification if desired

-- Add video_source column to products table
ALTER TABLE products
  ADD COLUMN IF NOT EXISTS video_source TEXT DEFAULT 'youtube',
  ADD COLUMN IF NOT EXISTS video_source_config JSONB DEFAULT '{}';

-- Add constraint to ensure valid sources
ALTER TABLE products
  DROP CONSTRAINT IF EXISTS products_video_source_check;

ALTER TABLE products
  ADD CONSTRAINT products_video_source_check
  CHECK (video_source IN ('youtube', 'embed'));

-- Create index for faster queries
CREATE INDEX IF NOT EXISTS idx_products_video_source ON products(video_source);

-- Add comments for documentation
COMMENT ON COLUMN products.video_source IS 'Active video source for bootcamp lessons: youtube or embed';
COMMENT ON COLUMN products.video_source_config IS 'Configuration metadata for video source settings';
COMMENT ON COLUMN bootcamp_lessons.youtube_url IS 'YouTube video URL for the lesson';
COMMENT ON COLUMN bootcamp_lessons.embed_code IS 'Custom embed code (Adilo, Vimeo, iframe) for the lesson';
