-- Add status field to sub_course_videos and practices
-- Status values: DRAFT, PUBLISHED, INACTIVE, ARCHIVED
-- ARCHIVED is used for soft deletes

-- Add status column to sub_course_videos
ALTER TABLE sub_course_videos 
ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'DRAFT'
CHECK (status IN ('DRAFT', 'PUBLISHED', 'INACTIVE', 'ARCHIVED'));

-- Migrate existing data based on is_active and is_published
UPDATE sub_course_videos 
SET status = CASE 
    WHEN is_published = true AND is_active = true THEN 'PUBLISHED'
    WHEN is_active = false THEN 'INACTIVE'
    ELSE 'DRAFT'
END;

-- Drop is_active column from sub_course_videos (keep is_published for publish_date tracking)
ALTER TABLE sub_course_videos DROP COLUMN IF EXISTS is_active;

-- Add status column to practices
ALTER TABLE practices 
ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'DRAFT'
CHECK (status IN ('DRAFT', 'PUBLISHED', 'INACTIVE', 'ARCHIVED'));

-- Migrate existing data based on is_active
UPDATE practices 
SET status = CASE 
    WHEN is_active = true THEN 'PUBLISHED'
    ELSE 'INACTIVE'
END;

-- Drop is_active column from practices
ALTER TABLE practices DROP COLUMN IF EXISTS is_active;

-- Create indexes for status queries
CREATE INDEX IF NOT EXISTS idx_sub_course_videos_status ON sub_course_videos(status);
CREATE INDEX IF NOT EXISTS idx_practices_status ON practices(status);
