-- ============================================================================
-- EasyStream - All New Features Installation
-- ============================================================================
-- This SQL file installs ALL new features in one go:
-- 1. Subtitle/Caption System
-- 2. Upload Progress Tracking
-- 3. Live Chat System
-- 4. Community Posts
-- 5. Polls
-- 6. Content Moderation/Reports
--
-- Installation:
-- docker exec -i easystream-db mysql -u easystream -peasystream easystream < __install/add_all_new_features.sql
-- ============================================================================

-- ============================================================================
-- 1. SUBTITLE/CAPTION SYSTEM
-- ============================================================================

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`),
  KEY `idx_subtitles_lookup` (`file_id`, `file_type`, `active`, `sub_default`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Subtitle/caption tracks for videos';

ALTER TABLE `db_videofiles`
ADD COLUMN IF NOT EXISTS `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this video',
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 `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this short',
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 `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this stream',
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 `captions_enabled` tinyint(1) DEFAULT 1 COMMENT 'Enable captions for this audio',
ADD COLUMN IF NOT EXISTS `subtitle_count` int(10) unsigned DEFAULT 0 COMMENT 'Number of subtitle tracks';

-- ============================================================================
-- 2. UPLOAD PROGRESS TRACKING
-- ============================================================================

CREATE TABLE IF NOT EXISTS `db_upload_progress` (
  `db_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `upload_id` varchar(64) NOT NULL COMMENT 'Unique upload identifier',
  `usr_id` int(10) unsigned NOT NULL COMMENT 'User ID',
  `filename` varchar(255) NOT NULL COMMENT 'Original filename',
  `file_type` varchar(20) NOT NULL COMMENT 'video, audio, image, document, etc.',
  `file_size` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT 'Total file size in bytes',
  `uploaded_bytes` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT 'Bytes uploaded so far',
  `upload_percent` decimal(5,2) NOT NULL DEFAULT 0.00 COMMENT 'Upload percentage (0-100)',
  `status` varchar(20) NOT NULL DEFAULT 'uploading' COMMENT 'uploading, processing, encoding, completed, failed, cancelled',
  `processing_step` varchar(100) DEFAULT NULL COMMENT 'Current processing step description',
  `error_message` text DEFAULT NULL COMMENT 'Error message if failed',
  `file_key` varchar(32) DEFAULT NULL COMMENT 'File key after successful upload',
  `started_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `completed_at` datetime DEFAULT NULL,
  PRIMARY KEY (`db_id`),
  UNIQUE KEY `idx_upload_id` (`upload_id`),
  KEY `idx_usr_status` (`usr_id`, `status`),
  KEY `idx_started_at` (`started_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Upload progress tracking';

-- ============================================================================
-- 3. LIVE CHAT SYSTEM
-- ============================================================================

CREATE TABLE IF NOT EXISTS `db_live_chat_messages` (
  `msg_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `stream_key` varchar(32) NOT NULL COMMENT 'Live stream file key',
  `usr_id` int(10) unsigned NOT NULL COMMENT 'User who sent message',
  `message` text NOT NULL COMMENT 'Chat message content',
  `message_type` varchar(20) NOT NULL DEFAULT 'chat' COMMENT 'chat, super_chat, system',
  `super_chat_amount` decimal(10,2) DEFAULT NULL COMMENT 'Amount if super chat',
  `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT 0,
  `deleted_by` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`msg_id`),
  KEY `idx_stream_timestamp` (`stream_key`, `timestamp`),
  KEY `idx_usr_id` (`usr_id`),
  KEY `idx_deleted` (`deleted`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Live chat messages';

CREATE TABLE IF NOT EXISTS `db_live_chat_moderation` (
  `mod_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `stream_key` varchar(32) NOT NULL COMMENT 'Live stream file key',
  `usr_id` int(10) unsigned NOT NULL COMMENT 'User being moderated',
  `action` varchar(20) NOT NULL COMMENT 'timeout, ban, unban',
  `duration` int(10) unsigned DEFAULT NULL COMMENT 'Duration in seconds for timeout',
  `reason` varchar(255) DEFAULT NULL,
  `moderator_id` int(10) unsigned NOT NULL COMMENT 'User who performed action',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `expires_at` datetime DEFAULT NULL,
  PRIMARY KEY (`mod_id`),
  KEY `idx_stream_user` (`stream_key`, `usr_id`),
  KEY `idx_expires` (`expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Chat moderation actions';

CREATE TABLE IF NOT EXISTS `db_live_chat_moderators` (
  `mod_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `stream_key` varchar(32) NOT NULL COMMENT 'Live stream file key',
  `usr_id` int(10) unsigned NOT NULL COMMENT 'User who is a moderator',
  `assigned_by` int(10) unsigned NOT NULL COMMENT 'User who assigned moderator',
  `assigned_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`mod_id`),
  UNIQUE KEY `idx_stream_user` (`stream_key`, `usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Chat moderators per stream';

CREATE TABLE IF NOT EXISTS `db_live_chat_settings` (
  `setting_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `stream_key` varchar(32) NOT NULL COMMENT 'Live stream file key',
  `chat_enabled` tinyint(1) NOT NULL DEFAULT 1,
  `slow_mode` int(10) unsigned DEFAULT NULL COMMENT 'Seconds between messages',
  `subscriber_only` tinyint(1) NOT NULL DEFAULT 0,
  `follower_only` tinyint(1) NOT NULL DEFAULT 0,
  `emotes_enabled` tinyint(1) NOT NULL DEFAULT 1,
  `links_allowed` tinyint(1) NOT NULL DEFAULT 0,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`setting_id`),
  UNIQUE KEY `idx_stream_key` (`stream_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Chat settings per stream';

-- ============================================================================
-- 4. COMMUNITY POSTS
-- ============================================================================

CREATE TABLE IF NOT EXISTS `db_community_posts` (
  `post_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `usr_id` int(10) unsigned NOT NULL COMMENT 'Creator user ID',
  `post_type` varchar(20) NOT NULL DEFAULT 'text' COMMENT 'text, image, video, poll',
  `content` text NOT NULL COMMENT 'Post content',
  `media_file_key` varchar(32) DEFAULT NULL COMMENT 'File key if image/video post',
  `poll_id` int(10) unsigned DEFAULT NULL COMMENT 'Poll ID if poll post',
  `likes` int(10) unsigned NOT NULL DEFAULT 0,
  `comments` int(10) unsigned NOT NULL DEFAULT 0,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`post_id`),
  KEY `idx_usr_id` (`usr_id`),
  KEY `idx_created_at` (`created_at`),
  KEY `idx_deleted` (`deleted`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Community posts from creators';

CREATE TABLE IF NOT EXISTS `db_community_post_likes` (
  `like_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` int(10) unsigned NOT NULL,
  `usr_id` int(10) unsigned NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`like_id`),
  UNIQUE KEY `idx_post_user` (`post_id`, `usr_id`),
  KEY `idx_usr_id` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Likes on community posts';

CREATE TABLE IF NOT EXISTS `db_community_post_comments` (
  `comment_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` int(10) unsigned NOT NULL,
  `usr_id` int(10) unsigned NOT NULL,
  `comment` text NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`comment_id`),
  KEY `idx_post_id` (`post_id`),
  KEY `idx_usr_id` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Comments on community posts';

-- ============================================================================
-- 5. POLLS
-- ============================================================================

CREATE TABLE IF NOT EXISTS `db_polls` (
  `poll_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `usr_id` int(10) unsigned NOT NULL COMMENT 'Creator user ID',
  `question` varchar(255) NOT NULL,
  `total_votes` int(10) unsigned NOT NULL DEFAULT 0,
  `expires_at` datetime DEFAULT NULL COMMENT 'Poll expiration time',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `closed` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`poll_id`),
  KEY `idx_usr_id` (`usr_id`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Polls created by users';

CREATE TABLE IF NOT EXISTS `db_poll_options` (
  `option_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `poll_id` int(10) unsigned NOT NULL,
  `option_text` varchar(255) NOT NULL,
  `votes` int(10) unsigned NOT NULL DEFAULT 0,
  `display_order` int(10) unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`option_id`),
  KEY `idx_poll_id` (`poll_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Poll answer options';

CREATE TABLE IF NOT EXISTS `db_poll_votes` (
  `vote_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `poll_id` int(10) unsigned NOT NULL,
  `option_id` int(10) unsigned NOT NULL,
  `usr_id` int(10) unsigned NOT NULL,
  `voted_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`vote_id`),
  UNIQUE KEY `idx_poll_user` (`poll_id`, `usr_id`),
  KEY `idx_option_id` (`option_id`),
  KEY `idx_usr_id` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User poll votes';

-- ============================================================================
-- 6. CONTENT MODERATION / REPORTS
-- ============================================================================

CREATE TABLE IF NOT EXISTS `db_reports` (
  `report_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `reporter_id` int(10) unsigned NOT NULL COMMENT 'User who reported',
  `reported_type` varchar(20) NOT NULL COMMENT 'video, user, comment, post',
  `reported_id` varchar(32) NOT NULL COMMENT 'ID/key of reported item',
  `report_reason` varchar(50) NOT NULL COMMENT 'spam, harassment, copyright, etc.',
  `report_details` text DEFAULT NULL,
  `status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT 'pending, reviewing, resolved, dismissed',
  `reviewed_by` int(10) unsigned DEFAULT NULL,
  `resolution` text DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `resolved_at` datetime DEFAULT NULL,
  PRIMARY KEY (`report_id`),
  KEY `idx_reporter` (`reporter_id`),
  KEY `idx_reported` (`reported_type`, `reported_id`),
  KEY `idx_status` (`status`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User reports for moderation';

-- ============================================================================
-- SETTINGS
-- ============================================================================

INSERT INTO `db_settings` (`setting_name`, `setting_value`, `setting_type`, `setting_category`, `setting_description`, `setting_default`) VALUES
-- Subtitles
('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'),

-- Live Chat
('live_chat_enabled', '1', 'boolean', 'live', 'Enable live chat for streams', '1'),
('live_chat_max_length', '500', 'number', 'live', 'Maximum chat message length', '500'),
('live_chat_rate_limit', '2', 'number', 'live', 'Minimum seconds between messages', '2'),
('live_chat_super_chat_enabled', '1', 'boolean', 'live', 'Enable Super Chat donations', '1'),
('live_chat_super_chat_min', '1.00', 'text', 'live', 'Minimum Super Chat amount', '1.00'),
('live_chat_replay_enabled', '1', 'boolean', 'live', 'Enable chat replay for VOD', '1'),

-- Community Posts
('community_posts_enabled', '1', 'boolean', 'community', 'Enable community posts feature', '1'),
('community_posts_subscriber_count', '1000', 'number', 'community', 'Minimum subscribers to post', '1000'),
('community_posts_max_length', '5000', 'number', 'community', 'Maximum post character length', '5000'),

-- Polls
('polls_enabled', '1', 'boolean', 'community', 'Enable polls feature', '1'),
('polls_max_options', '10', 'number', 'community', 'Maximum poll options', '10'),
('polls_max_duration', '7', 'number', 'community', 'Maximum poll duration in days', '7'),

-- Content Moderation
('reporting_enabled', '1', 'boolean', 'moderation', 'Enable content reporting', '1'),
('reporting_reasons', 'spam,harassment,copyright,inappropriate,misleading', 'text', 'moderation', 'Report reason options (comma-separated)', 'spam,harassment,copyright,inappropriate,misleading')

ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value);

-- ============================================================================
-- CLEANUP EVENTS
-- ============================================================================

CREATE EVENT IF NOT EXISTS cleanup_upload_progress
ON SCHEDULE EVERY 1 DAY
DO
  DELETE FROM `db_upload_progress`
  WHERE `status` IN ('completed', 'failed', 'cancelled')
  AND `completed_at` < DATE_SUB(NOW(), INTERVAL 7 DAY);

CREATE EVENT IF NOT EXISTS cleanup_live_chat
ON SCHEDULE EVERY 1 DAY
DO
  DELETE FROM `db_live_chat_messages`
  WHERE `timestamp` < DATE_SUB(NOW(), INTERVAL 30 DAY);

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