-- ============================================================================
-- EasyStream Subtitle/Caption System Installation
-- ============================================================================
-- This SQL file adds complete subtitle/caption support to EasyStream
--
-- Features:
-- - Multiple subtitle tracks per video (different languages)
-- - Support for .srt and .vtt formats
-- - Auto-generated captions integration
-- - Default language selection
-- - Subtitle enable/disable per video
--
-- Installation:
-- docker exec -i easystream-db mysql -u easystream -peasystream easystream < __install/add_subtitles_system.sql
-- ============================================================================

-- Create subtitles table
CREATE TABLE IF NOT EXISTS `db_subtitles` (
  `sub_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `file_id` int(10) unsigned NOT NULL COMMENT 'Foreign key to video/audio/live file',
  `file_type` varchar(10) NOT NULL DEFAULT 'video' COMMENT 'video, audio, live, short',
  `usr_id` int(10) unsigned NOT NULL COMMENT 'Owner user ID',
  `sub_language` varchar(10) NOT NULL DEFAULT 'en' COMMENT 'Language code (en, es, fr, etc.)',
  `sub_label` varchar(100) NOT NULL DEFAULT 'English' COMMENT 'Display label (English, Spanish, etc.)',
  `sub_filename` varchar(255) NOT NULL COMMENT 'Filename on disk',
  `sub_format` varchar(10) NOT NULL DEFAULT 'vtt' COMMENT 'vtt or srt',
  `sub_kind` varchar(20) NOT NULL DEFAULT 'subtitles' COMMENT 'subtitles, captions, descriptions',
  `sub_default` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Is this the default track?',
  `sub_auto_generated` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Auto-generated via speech-to-text',
  `sub_filesize` int(10) unsigned NOT NULL DEFAULT 0 COMMENT 'File size in bytes',
  `upload_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`sub_id`),
  KEY `idx_file_type` (`file_id`, `file_type`),
  KEY `idx_usr_id` (`usr_id`),
  KEY `idx_language` (`sub_language`),
  KEY `idx_active` (`active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Subtitle/caption tracks for videos';

-- Add subtitle settings to config table
INSERT INTO `db_settings` (`setting_name`, `setting_value`, `setting_type`, `setting_category`, `setting_description`, `setting_default`) VALUES
('subtitles_enabled', '1', 'boolean', 'video', 'Enable subtitle/caption uploads for videos', '1'),
('subtitles_max_size', '1048576', 'number', 'video', 'Maximum subtitle file size in bytes (default 1MB)', '1048576'),
('subtitles_allowed_formats', 'vtt,srt', 'text', 'video', 'Allowed subtitle formats (comma-separated)', 'vtt,srt'),
('subtitles_auto_convert', '1', 'boolean', 'video', 'Auto-convert SRT to VTT format', '1'),
('subtitles_max_per_video', '10', 'number', 'video', 'Maximum subtitle tracks per video', '10'),
('subtitles_require_approval', '0', 'boolean', 'video', 'Require admin approval for user-uploaded subtitles', '0')
ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value);

-- Add captions_enabled field to video files if it doesn't exist
ALTER TABLE `db_videofiles`
ADD COLUMN IF NOT EXISTS `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this video';

ALTER TABLE `db_shortfiles`
ADD COLUMN IF NOT EXISTS `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this short';

ALTER TABLE `db_livefiles`
ADD COLUMN IF NOT EXISTS `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this stream';

ALTER TABLE `db_audiofiles`
ADD COLUMN IF NOT EXISTS `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this audio';

-- Add subtitle count to files
ALTER TABLE `db_videofiles`
ADD COLUMN IF NOT EXISTS `subtitle_count` int(10) unsigned DEFAULT 0 COMMENT 'Number of subtitle tracks';

ALTER TABLE `db_shortfiles`
ADD COLUMN IF NOT EXISTS `subtitle_count` int(10) unsigned DEFAULT 0 COMMENT 'Number of subtitle tracks';

ALTER TABLE `db_livefiles`
ADD COLUMN IF NOT EXISTS `subtitle_count` int(10) unsigned DEFAULT 0 COMMENT 'Number of subtitle tracks';

ALTER TABLE `db_audiofiles`
ADD COLUMN IF NOT EXISTS `subtitle_count` int(10) unsigned DEFAULT 0 COMMENT 'Number of subtitle tracks';

-- ============================================================================
-- Sample Data (optional - for testing)
-- ============================================================================

-- Uncomment below to add sample subtitle track
-- INSERT INTO `db_subtitles` (`file_id`, `file_type`, `usr_id`, `sub_language`, `sub_label`, `sub_filename`, `sub_format`, `sub_kind`, `sub_default`, `sub_auto_generated`)
-- VALUES (1, 'video', 1, 'en', 'English', 'sample_video_en.vtt', 'vtt', 'subtitles', 1, 0);

-- ============================================================================
-- Indexes for Performance
-- ============================================================================

-- Ensure fast subtitle lookup by file
CREATE INDEX IF NOT EXISTS idx_subtitles_lookup ON `db_subtitles` (`file_id`, `file_type`, `active`, `sub_default`);

-- ============================================================================
-- END OF INSTALLATION
-- ============================================================================
