-- Migration: Add multi-media support to generated_posts
-- Date: 2026-02-11
-- Description: Adds media_items JSONB array to support up to 3 images or 1 video per post

-- Add media_items column
ALTER TABLE generated_posts ADD COLUMN IF NOT EXISTS media_items JSONB DEFAULT '[]'::JSONB;

-- Add GIN index for performance on JSONB queries
CREATE INDEX IF NOT EXISTS idx_generated_posts_media_items ON generated_posts USING GIN (media_items);

-- Migrate existing image_url to media_items array
UPDATE generated_posts
SET media_items = jsonb_build_array(
    jsonb_build_object(
        'type', 'image',
        'url', image_url,
        'order', 0,
        'content_type', 'image/jpeg',
        'uploaded_at', NOW()
    )
)
WHERE image_url IS NOT NULL AND media_items = '[]'::JSONB;

-- Note: image_url column is kept for backward compatibility
-- New code should use media_items, but existing code still works

COMMENT ON COLUMN generated_posts.media_items IS 'JSONB array of media items (images/videos). Max 3 items. Structure: [{type, url, order, content_type, uploaded_at, metadata}]';
