-- EasyStream Advanced Features Database Schema
-- Version: 2.0
-- This file adds support for:
-- 1. API & OAuth system
-- 2. Advanced analytics
-- 3. Monetization features
-- 4. CDN integration
-- 5. Advanced search
-- 6. Collaborative features
-- 7. AI features
-- 8. Advanced moderation
-- 9. Email notifications
-- 10. Mobile app support

-- =====================================================
-- 1. API & OAuth System
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_api_keys` (
  `key_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `api_key` VARCHAR(64) NOT NULL UNIQUE,
  `api_secret` VARCHAR(64) NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `scopes` JSON DEFAULT NULL COMMENT 'Permission scopes: videos.read, videos.write, etc.',
  `is_active` TINYINT(1) DEFAULT 1,
  `request_count` INT UNSIGNED DEFAULT 0,
  `last_used_at` DATETIME DEFAULT NULL,
  `expires_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  `revoked_at` DATETIME DEFAULT NULL,
  INDEX `idx_api_key` (`api_key`),
  INDEX `idx_usr_id` (`usr_id`),
  INDEX `idx_active` (`is_active`, `expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_oauth_tokens` (
  `token_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `client_id` VARCHAR(64) NOT NULL,
  `access_token` VARCHAR(255) NOT NULL UNIQUE,
  `refresh_token` VARCHAR(255) DEFAULT NULL UNIQUE,
  `token_type` VARCHAR(20) DEFAULT 'Bearer',
  `scopes` JSON DEFAULT NULL,
  `expires_at` DATETIME NOT NULL,
  `refresh_expires_at` DATETIME DEFAULT NULL,
  `is_revoked` TINYINT(1) DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_access_token` (`access_token`),
  INDEX `idx_refresh_token` (`refresh_token`),
  INDEX `idx_usr_id` (`usr_id`),
  INDEX `idx_expires` (`expires_at`, `is_revoked`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_api_logs` (
  `log_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED DEFAULT NULL,
  `endpoint` VARCHAR(255) NOT NULL,
  `method` VARCHAR(10) NOT NULL,
  `status` SMALLINT UNSIGNED NOT NULL,
  `duration` FLOAT DEFAULT 0 COMMENT 'Request duration in seconds',
  `ip_address` VARCHAR(45) DEFAULT NULL,
  `user_agent` TEXT DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr_id` (`usr_id`),
  INDEX `idx_endpoint` (`endpoint`),
  INDEX `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_webhooks` (
  `webhook_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `url` VARCHAR(500) NOT NULL,
  `events` JSON NOT NULL COMMENT 'Events to trigger: video.upload, comment.new, etc.',
  `secret` VARCHAR(64) NOT NULL,
  `is_active` TINYINT(1) DEFAULT 1,
  `last_triggered_at` DATETIME DEFAULT NULL,
  `failure_count` INT DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr_id` (`usr_id`),
  INDEX `idx_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 2. Advanced Analytics System
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_analytics_events` (
  `event_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED DEFAULT NULL,
  `session_id` VARCHAR(64) DEFAULT NULL,
  `event_type` VARCHAR(50) NOT NULL COMMENT 'view, play, pause, seek, like, comment, share, etc.',
  `file_key` VARCHAR(20) DEFAULT NULL,
  `file_type` ENUM('video', 'short', 'live', 'image', 'audio', 'doc', 'blog') DEFAULT NULL,
  `event_data` JSON DEFAULT NULL COMMENT 'Additional event data',
  `timestamp_sec` INT UNSIGNED DEFAULT NULL COMMENT 'Video timestamp in seconds',
  `ip_address` VARCHAR(45) DEFAULT NULL,
  `user_agent` TEXT DEFAULT NULL,
  `referrer` VARCHAR(500) DEFAULT NULL,
  `country` VARCHAR(2) DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_file_key` (`file_key`),
  INDEX `idx_event_type` (`event_type`),
  INDEX `idx_usr_id` (`usr_id`),
  INDEX `idx_session` (`session_id`),
  INDEX `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_analytics_retention` (
  `retention_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `timestamp_sec` INT UNSIGNED NOT NULL COMMENT 'Second in the video',
  `viewers` INT UNSIGNED DEFAULT 0 COMMENT 'Number of viewers at this second',
  `completed` INT UNSIGNED DEFAULT 0 COMMENT 'Number who completed from here',
  `dropped` INT UNSIGNED DEFAULT 0 COMMENT 'Number who dropped at this second',
  `updated_at` DATETIME NOT NULL,
  UNIQUE KEY `unique_retention` (`file_key`, `timestamp_sec`),
  INDEX `idx_file_key` (`file_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_analytics_heatmaps` (
  `heatmap_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `x_coord` FLOAT NOT NULL COMMENT 'X coordinate (0-1)',
  `y_coord` FLOAT NOT NULL COMMENT 'Y coordinate (0-1)',
  `clicks` INT UNSIGNED DEFAULT 0,
  `hovers` INT UNSIGNED DEFAULT 0,
  `date` DATE NOT NULL,
  INDEX `idx_file_key` (`file_key`, `date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_analytics_traffic` (
  `traffic_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `source_type` ENUM('direct', 'search', 'social', 'external', 'internal', 'suggested') NOT NULL,
  `source_name` VARCHAR(255) DEFAULT NULL COMMENT 'Google, Facebook, etc.',
  `referrer_url` VARCHAR(500) DEFAULT NULL,
  `visits` INT UNSIGNED DEFAULT 1,
  `date` DATE NOT NULL,
  INDEX `idx_file_key` (`file_key`, `date`),
  INDEX `idx_source` (`source_type`, `date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_analytics_demographics` (
  `demo_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `age_range` VARCHAR(20) DEFAULT NULL COMMENT '18-24, 25-34, etc.',
  `gender` VARCHAR(20) DEFAULT NULL,
  `country` VARCHAR(2) DEFAULT NULL,
  `views` INT UNSIGNED DEFAULT 1,
  `watch_time` INT UNSIGNED DEFAULT 0 COMMENT 'Total watch time in seconds',
  `date` DATE NOT NULL,
  INDEX `idx_file_key` (`file_key`, `date`),
  INDEX `idx_country` (`country`, `date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 3. Monetization Features
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_membership_tiers` (
  `tier_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL COMMENT 'Channel owner',
  `name` VARCHAR(100) NOT NULL,
  `description` TEXT DEFAULT NULL,
  `price_monthly` DECIMAL(10,2) NOT NULL,
  `currency` VARCHAR(3) DEFAULT 'USD',
  `perks` JSON DEFAULT NULL COMMENT 'List of membership perks',
  `badge_url` VARCHAR(500) DEFAULT NULL,
  `is_active` TINYINT(1) DEFAULT 1,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr_id` (`usr_id`),
  INDEX `idx_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_memberships` (
  `membership_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `tier_id` INT UNSIGNED NOT NULL,
  `subscriber_id` INT UNSIGNED NOT NULL,
  `channel_owner_id` INT UNSIGNED NOT NULL,
  `status` ENUM('active', 'cancelled', 'expired', 'paused') DEFAULT 'active',
  `started_at` DATETIME NOT NULL,
  `expires_at` DATETIME DEFAULT NULL,
  `cancelled_at` DATETIME DEFAULT NULL,
  `payment_method` VARCHAR(50) DEFAULT NULL,
  `stripe_subscription_id` VARCHAR(255) DEFAULT NULL,
  INDEX `idx_subscriber` (`subscriber_id`, `status`),
  INDEX `idx_channel` (`channel_owner_id`, `status`),
  INDEX `idx_tier` (`tier_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_super_chats` (
  `super_chat_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL COMMENT 'Sender',
  `recipient_id` INT UNSIGNED NOT NULL COMMENT 'Receiver (channel owner)',
  `file_key` VARCHAR(20) DEFAULT NULL COMMENT 'Associated video/live stream',
  `amount` DECIMAL(10,2) NOT NULL,
  `currency` VARCHAR(3) DEFAULT 'USD',
  `message` VARCHAR(500) DEFAULT NULL,
  `type` ENUM('super_chat', 'super_thanks', 'tip') DEFAULT 'super_chat',
  `payment_status` ENUM('pending', 'completed', 'refunded', 'failed') DEFAULT 'pending',
  `stripe_payment_id` VARCHAR(255) DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_recipient` (`recipient_id`, `created_at`),
  INDEX `idx_file_key` (`file_key`),
  INDEX `idx_status` (`payment_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_revenue_shares` (
  `share_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `period_start` DATE NOT NULL,
  `period_end` DATE NOT NULL,
  `ad_revenue` DECIMAL(10,2) DEFAULT 0,
  `membership_revenue` DECIMAL(10,2) DEFAULT 0,
  `super_chat_revenue` DECIMAL(10,2) DEFAULT 0,
  `total_revenue` DECIMAL(10,2) DEFAULT 0,
  `platform_fee` DECIMAL(10,2) DEFAULT 0,
  `payout_amount` DECIMAL(10,2) DEFAULT 0,
  `payout_status` ENUM('pending', 'processing', 'paid', 'failed') DEFAULT 'pending',
  `payout_date` DATE DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr_id` (`usr_id`, `period_start`),
  INDEX `idx_status` (`payout_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_ad_campaigns` (
  `campaign_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `advertiser_id` INT UNSIGNED NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `description` TEXT DEFAULT NULL,
  `ad_type` ENUM('pre_roll', 'mid_roll', 'post_roll', 'banner', 'overlay') NOT NULL,
  `target_categories` JSON DEFAULT NULL,
  `target_demographics` JSON DEFAULT NULL,
  `budget_daily` DECIMAL(10,2) DEFAULT NULL,
  `budget_total` DECIMAL(10,2) DEFAULT NULL,
  `spent` DECIMAL(10,2) DEFAULT 0,
  `cpm` DECIMAL(10,2) DEFAULT NULL COMMENT 'Cost per 1000 impressions',
  `impressions` INT UNSIGNED DEFAULT 0,
  `clicks` INT UNSIGNED DEFAULT 0,
  `start_date` DATE NOT NULL,
  `end_date` DATE DEFAULT NULL,
  `status` ENUM('draft', 'active', 'paused', 'completed') DEFAULT 'draft',
  `created_at` DATETIME NOT NULL,
  INDEX `idx_advertiser` (`advertiser_id`),
  INDEX `idx_status` (`status`, `start_date`, `end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_transactions` (
  `transaction_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `type` ENUM('membership', 'super_chat', 'super_thanks', 'ad_payout', 'tip', 'refund') NOT NULL,
  `amount` DECIMAL(10,2) NOT NULL,
  `currency` VARCHAR(3) DEFAULT 'USD',
  `description` TEXT DEFAULT NULL,
  `reference_id` VARCHAR(255) DEFAULT NULL COMMENT 'External payment ID',
  `payment_method` VARCHAR(50) DEFAULT NULL,
  `status` ENUM('pending', 'completed', 'failed', 'refunded') DEFAULT 'pending',
  `metadata` JSON DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr_id` (`usr_id`, `created_at`),
  INDEX `idx_type` (`type`, `status`),
  INDEX `idx_reference` (`reference_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 4. CDN Integration
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_cdn_stats` (
  `stat_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `cdn_provider` VARCHAR(50) NOT NULL COMMENT 'cloudflare, aws, bunny, etc.',
  `region` VARCHAR(50) DEFAULT NULL,
  `bandwidth_mb` DECIMAL(12,2) DEFAULT 0,
  `requests` INT UNSIGNED DEFAULT 0,
  `cache_hits` INT UNSIGNED DEFAULT 0,
  `cache_misses` INT UNSIGNED DEFAULT 0,
  `date` DATE NOT NULL,
  INDEX `idx_file_key` (`file_key`, `date`),
  INDEX `idx_provider` (`cdn_provider`, `date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_cdn_config` (
  `config_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `provider` VARCHAR(50) NOT NULL,
  `name` VARCHAR(100) NOT NULL,
  `api_key` VARCHAR(255) DEFAULT NULL,
  `api_secret` VARCHAR(255) DEFAULT NULL,
  `zone_id` VARCHAR(255) DEFAULT NULL,
  `base_url` VARCHAR(500) DEFAULT NULL,
  `is_active` TINYINT(1) DEFAULT 1,
  `priority` TINYINT DEFAULT 0 COMMENT 'Failover priority',
  `config_data` JSON DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_active` (`is_active`, `priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 5. Advanced Search
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_search_history` (
  `history_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED DEFAULT NULL,
  `session_id` VARCHAR(64) DEFAULT NULL,
  `query` VARCHAR(500) NOT NULL,
  `filters` JSON DEFAULT NULL,
  `results_count` INT UNSIGNED DEFAULT 0,
  `clicked_file_key` VARCHAR(20) DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr_id` (`usr_id`, `created_at`),
  INDEX `idx_session` (`session_id`),
  INDEX `idx_query` (`query`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_search_suggestions` (
  `suggestion_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `query` VARCHAR(255) NOT NULL UNIQUE,
  `search_count` INT UNSIGNED DEFAULT 1,
  `last_searched` DATETIME NOT NULL,
  `is_trending` TINYINT(1) DEFAULT 0,
  INDEX `idx_count` (`search_count` DESC),
  INDEX `idx_trending` (`is_trending`, `last_searched`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_search_analytics` (
  `analytics_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `query` VARCHAR(500) NOT NULL,
  `results_count` INT UNSIGNED DEFAULT 0,
  `avg_click_position` FLOAT DEFAULT NULL,
  `searches` INT UNSIGNED DEFAULT 1,
  `clicks` INT UNSIGNED DEFAULT 0,
  `date` DATE NOT NULL,
  INDEX `idx_query` (`query`(255), `date`),
  INDEX `idx_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 6. Collaborative Features
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_watch_parties` (
  `party_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `host_id` INT UNSIGNED NOT NULL,
  `file_key` VARCHAR(20) NOT NULL,
  `name` VARCHAR(255) DEFAULT NULL,
  `description` TEXT DEFAULT NULL,
  `invite_code` VARCHAR(20) NOT NULL UNIQUE,
  `max_participants` INT DEFAULT 50,
  `current_timestamp` INT UNSIGNED DEFAULT 0 COMMENT 'Current playback position in seconds',
  `is_playing` TINYINT(1) DEFAULT 0,
  `is_public` TINYINT(1) DEFAULT 1,
  `status` ENUM('waiting', 'active', 'ended') DEFAULT 'waiting',
  `started_at` DATETIME DEFAULT NULL,
  `ended_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_host` (`host_id`),
  INDEX `idx_invite` (`invite_code`),
  INDEX `idx_status` (`status`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_watch_party_participants` (
  `participant_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `party_id` INT UNSIGNED NOT NULL,
  `usr_id` INT UNSIGNED NOT NULL,
  `joined_at` DATETIME NOT NULL,
  `left_at` DATETIME DEFAULT NULL,
  `is_active` TINYINT(1) DEFAULT 1,
  INDEX `idx_party` (`party_id`, `is_active`),
  INDEX `idx_usr` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_playlist_collaborators` (
  `collaborator_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `pl_id` INT UNSIGNED NOT NULL,
  `usr_id` INT UNSIGNED NOT NULL,
  `permission` ENUM('view', 'add', 'edit', 'admin') DEFAULT 'add',
  `invited_by` INT UNSIGNED NOT NULL,
  `invited_at` DATETIME NOT NULL,
  `accepted_at` DATETIME DEFAULT NULL,
  `status` ENUM('pending', 'accepted', 'declined', 'removed') DEFAULT 'pending',
  INDEX `idx_playlist` (`pl_id`, `status`),
  INDEX `idx_usr` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_video_annotations` (
  `annotation_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `usr_id` INT UNSIGNED NOT NULL,
  `timestamp_start` INT UNSIGNED NOT NULL COMMENT 'Start time in seconds',
  `timestamp_end` INT UNSIGNED DEFAULT NULL COMMENT 'End time in seconds (optional)',
  `type` ENUM('note', 'link', 'chapter', 'highlight') DEFAULT 'note',
  `content` TEXT DEFAULT NULL,
  `url` VARCHAR(500) DEFAULT NULL COMMENT 'For link annotations',
  `position_x` FLOAT DEFAULT NULL COMMENT 'X coordinate (0-1)',
  `position_y` FLOAT DEFAULT NULL COMMENT 'Y coordinate (0-1)',
  `is_public` TINYINT(1) DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_file` (`file_key`, `is_public`),
  INDEX `idx_usr` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 7. AI Features
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_ai_captions` (
  `caption_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `language` VARCHAR(10) NOT NULL COMMENT 'en, es, fr, etc.',
  `provider` VARCHAR(50) DEFAULT NULL COMMENT 'whisper, google, aws',
  `status` ENUM('pending', 'processing', 'completed', 'failed') DEFAULT 'pending',
  `vtt_file` VARCHAR(500) DEFAULT NULL,
  `srt_file` VARCHAR(500) DEFAULT NULL,
  `confidence_score` FLOAT DEFAULT NULL,
  `processing_time` FLOAT DEFAULT NULL COMMENT 'Seconds',
  `error_message` TEXT DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  `completed_at` DATETIME DEFAULT NULL,
  INDEX `idx_file` (`file_key`, `language`),
  INDEX `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_ai_moderation` (
  `moderation_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `content_type` ENUM('video', 'image', 'audio', 'text') NOT NULL,
  `provider` VARCHAR(50) DEFAULT NULL COMMENT 'openai, google, aws',
  `nsfw_score` FLOAT DEFAULT NULL COMMENT '0-1',
  `violence_score` FLOAT DEFAULT NULL,
  `hate_speech_score` FLOAT DEFAULT NULL,
  `spam_score` FLOAT DEFAULT NULL,
  `copyright_match` TINYINT(1) DEFAULT 0,
  `flags` JSON DEFAULT NULL COMMENT 'Detailed flags',
  `action_taken` ENUM('none', 'flagged', 'removed', 'age_restricted') DEFAULT 'none',
  `reviewed_by` INT UNSIGNED DEFAULT NULL COMMENT 'Human moderator',
  `reviewed_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_file` (`file_key`),
  INDEX `idx_action` (`action_taken`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_ai_thumbnails` (
  `thumbnail_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `frame_timestamp` INT UNSIGNED NOT NULL COMMENT 'Second in video',
  `image_path` VARCHAR(500) NOT NULL,
  `ai_score` FLOAT DEFAULT NULL COMMENT 'AI quality score 0-1',
  `is_selected` TINYINT(1) DEFAULT 0,
  `click_rate` FLOAT DEFAULT NULL COMMENT 'CTR if used',
  `impressions` INT UNSIGNED DEFAULT 0,
  `clicks` INT UNSIGNED DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_file` (`file_key`),
  INDEX `idx_score` (`ai_score` DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_ai_tags` (
  `tag_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `file_key` VARCHAR(20) NOT NULL,
  `tag` VARCHAR(100) NOT NULL,
  `confidence` FLOAT DEFAULT NULL,
  `source` ENUM('ai', 'user', 'hybrid') DEFAULT 'ai',
  `is_approved` TINYINT(1) DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_file` (`file_key`),
  INDEX `idx_tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 8. Advanced Moderation Tools
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_moderation_rules` (
  `rule_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(255) NOT NULL,
  `type` ENUM('keyword', 'pattern', 'ai', 'copyright', 'spam') NOT NULL,
  `pattern` TEXT DEFAULT NULL COMMENT 'Regex or keyword list',
  `action` ENUM('flag', 'remove', 'warn', 'ban') DEFAULT 'flag',
  `severity` ENUM('low', 'medium', 'high', 'critical') DEFAULT 'medium',
  `is_active` TINYINT(1) DEFAULT 1,
  `created_by` INT UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_active` (`is_active`, `type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_moderation_actions` (
  `action_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `target_type` ENUM('video', 'comment', 'user', 'live', 'post') NOT NULL,
  `target_id` VARCHAR(50) NOT NULL,
  `rule_id` INT UNSIGNED DEFAULT NULL,
  `moderator_id` INT UNSIGNED DEFAULT NULL COMMENT 'NULL if automated',
  `action` ENUM('warned', 'removed', 'age_restricted', 'demonetized', 'banned') NOT NULL,
  `reason` TEXT DEFAULT NULL,
  `is_automated` TINYINT(1) DEFAULT 0,
  `is_appealed` TINYINT(1) DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_target` (`target_type`, `target_id`),
  INDEX `idx_moderator` (`moderator_id`),
  INDEX `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_moderation_appeals` (
  `appeal_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `action_id` INT UNSIGNED NOT NULL,
  `usr_id` INT UNSIGNED NOT NULL,
  `reason` TEXT NOT NULL,
  `evidence` JSON DEFAULT NULL COMMENT 'Additional evidence/URLs',
  `status` ENUM('pending', 'reviewing', 'approved', 'rejected') DEFAULT 'pending',
  `reviewed_by` INT UNSIGNED DEFAULT NULL,
  `review_notes` TEXT DEFAULT NULL,
  `reviewed_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_action` (`action_id`),
  INDEX `idx_usr` (`usr_id`),
  INDEX `idx_status` (`status`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_moderation_queue` (
  `queue_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `target_type` ENUM('video', 'comment', 'user', 'live', 'post') NOT NULL,
  `target_id` VARCHAR(50) NOT NULL,
  `reporter_id` INT UNSIGNED DEFAULT NULL,
  `reason` VARCHAR(255) DEFAULT NULL,
  `priority` ENUM('low', 'medium', 'high', 'urgent') DEFAULT 'medium',
  `status` ENUM('pending', 'in_review', 'resolved', 'dismissed') DEFAULT 'pending',
  `assigned_to` INT UNSIGNED DEFAULT NULL,
  `resolved_by` INT UNSIGNED DEFAULT NULL,
  `resolution` TEXT DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  `resolved_at` DATETIME DEFAULT NULL,
  INDEX `idx_status` (`status`, `priority`, `created_at`),
  INDEX `idx_assigned` (`assigned_to`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_user_strikes` (
  `strike_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `action_id` INT UNSIGNED NOT NULL,
  `type` ENUM('warning', 'strike', 'suspension', 'ban') NOT NULL,
  `reason` TEXT NOT NULL,
  `expires_at` DATETIME DEFAULT NULL COMMENT 'For temporary strikes',
  `is_active` TINYINT(1) DEFAULT 1,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr` (`usr_id`, `is_active`),
  INDEX `idx_type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 9. Email Notification System
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_email_queue` (
  `email_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED DEFAULT NULL,
  `to_email` VARCHAR(255) NOT NULL,
  `to_name` VARCHAR(255) DEFAULT NULL,
  `from_email` VARCHAR(255) DEFAULT NULL,
  `from_name` VARCHAR(255) DEFAULT NULL,
  `subject` VARCHAR(500) NOT NULL,
  `body_html` LONGTEXT DEFAULT NULL,
  `body_text` TEXT DEFAULT NULL,
  `template_name` VARCHAR(100) DEFAULT NULL,
  `template_data` JSON DEFAULT NULL,
  `priority` TINYINT DEFAULT 5 COMMENT '1=highest, 10=lowest',
  `status` ENUM('pending', 'sending', 'sent', 'failed', 'bounced') DEFAULT 'pending',
  `attempts` TINYINT DEFAULT 0,
  `error_message` TEXT DEFAULT NULL,
  `send_at` DATETIME DEFAULT NULL COMMENT 'Scheduled send time',
  `sent_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_status` (`status`, `send_at`),
  INDEX `idx_usr` (`usr_id`),
  INDEX `idx_priority` (`priority`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_email_templates` (
  `template_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(100) NOT NULL UNIQUE,
  `subject` VARCHAR(500) NOT NULL,
  `body_html` LONGTEXT NOT NULL,
  `body_text` TEXT DEFAULT NULL,
  `variables` JSON DEFAULT NULL COMMENT 'Available variables: {name}, {url}, etc.',
  `category` VARCHAR(50) DEFAULT NULL COMMENT 'digest, alert, marketing, transactional',
  `is_active` TINYINT(1) DEFAULT 1,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME DEFAULT NULL,
  INDEX `idx_name` (`name`),
  INDEX `idx_category` (`category`, `is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_email_preferences` (
  `preference_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL UNIQUE,
  `digest_frequency` ENUM('none', 'daily', 'weekly', 'monthly') DEFAULT 'weekly',
  `notify_comments` TINYINT(1) DEFAULT 1,
  `notify_replies` TINYINT(1) DEFAULT 1,
  `notify_likes` TINYINT(1) DEFAULT 1,
  `notify_subscribers` TINYINT(1) DEFAULT 1,
  `notify_uploads` TINYINT(1) DEFAULT 1 COMMENT 'From subscriptions',
  `notify_live_streams` TINYINT(1) DEFAULT 1,
  `notify_mentions` TINYINT(1) DEFAULT 1,
  `notify_milestones` TINYINT(1) DEFAULT 1,
  `marketing_emails` TINYINT(1) DEFAULT 1,
  `updated_at` DATETIME DEFAULT NULL,
  INDEX `idx_usr` (`usr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_email_logs` (
  `log_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `email_id` BIGINT UNSIGNED DEFAULT NULL,
  `usr_id` INT UNSIGNED DEFAULT NULL,
  `to_email` VARCHAR(255) NOT NULL,
  `subject` VARCHAR(500) DEFAULT NULL,
  `status` ENUM('sent', 'delivered', 'opened', 'clicked', 'bounced', 'complained') NOT NULL,
  `provider_id` VARCHAR(255) DEFAULT NULL COMMENT 'SendGrid message ID',
  `event_data` JSON DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_email` (`email_id`),
  INDEX `idx_usr` (`usr_id`, `created_at`),
  INDEX `idx_status` (`status`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 10. Mobile App Support
-- =====================================================

CREATE TABLE IF NOT EXISTS `db_device_tokens` (
  `token_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `device_token` VARCHAR(255) NOT NULL UNIQUE,
  `device_type` ENUM('ios', 'android', 'web') NOT NULL,
  `device_name` VARCHAR(255) DEFAULT NULL,
  `app_version` VARCHAR(20) DEFAULT NULL,
  `os_version` VARCHAR(50) DEFAULT NULL,
  `is_active` TINYINT(1) DEFAULT 1,
  `last_used_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  INDEX `idx_usr` (`usr_id`, `is_active`),
  INDEX `idx_token` (`device_token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_push_notifications` (
  `notification_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED DEFAULT NULL,
  `title` VARCHAR(255) NOT NULL,
  `body` TEXT NOT NULL,
  `image_url` VARCHAR(500) DEFAULT NULL,
  `click_action` VARCHAR(500) DEFAULT NULL COMMENT 'Deep link URL',
  `data` JSON DEFAULT NULL COMMENT 'Additional data payload',
  `priority` ENUM('normal', 'high') DEFAULT 'normal',
  `status` ENUM('pending', 'sent', 'failed') DEFAULT 'pending',
  `sent_count` INT DEFAULT 0,
  `delivered_count` INT DEFAULT 0,
  `clicked_count` INT DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  `sent_at` DATETIME DEFAULT NULL,
  INDEX `idx_usr` (`usr_id`),
  INDEX `idx_status` (`status`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `db_offline_downloads` (
  `download_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `usr_id` INT UNSIGNED NOT NULL,
  `file_key` VARCHAR(20) NOT NULL,
  `quality` VARCHAR(20) DEFAULT NULL COMMENT '720p, 1080p, etc.',
  `file_size` BIGINT UNSIGNED DEFAULT NULL,
  `expires_at` DATETIME DEFAULT NULL COMMENT 'Download expiry',
  `downloaded_at` DATETIME NOT NULL,
  INDEX `idx_usr` (`usr_id`),
  INDEX `idx_file` (`file_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

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

-- Add composite indexes for common queries
ALTER TABLE `db_analytics_events` ADD INDEX `idx_composite_1` (`file_key`, `event_type`, `created_at`);
ALTER TABLE `db_transactions` ADD INDEX `idx_composite_1` (`usr_id`, `type`, `status`, `created_at`);

-- =====================================================
-- Initial Data / Default Settings
-- =====================================================

-- Insert default email templates
INSERT INTO `db_email_templates` (`name`, `subject`, `body_html`, `body_text`, `category`, `created_at`) VALUES
('welcome', 'Welcome to EasyStream!', '<h1>Welcome {name}!</h1><p>Thank you for joining EasyStream...</p>', 'Welcome {name}! Thank you for joining EasyStream...', 'transactional', NOW()),
('new_subscriber', 'You have a new subscriber!', '<h2>Great news!</h2><p>{subscriber_name} subscribed to your channel.</p>', '{subscriber_name} subscribed to your channel.', 'alert', NOW()),
('new_comment', 'New comment on your video', '<p>{commenter_name} commented: "{comment}"</p>', '{commenter_name} commented: "{comment}"', 'alert', NOW()),
('weekly_digest', 'Your weekly EasyStream digest', '<h2>Here is what happened this week...</h2>', 'Here is what happened this week...', 'digest', NOW());

-- Insert default moderation rules
INSERT INTO `db_moderation_rules` (`name`, `type`, `pattern`, `action`, `severity`, `is_active`, `created_by`, `created_at`) VALUES
('Spam Keywords', 'keyword', 'spam,scam,phishing,free money', 'flag', 'medium', 1, 1, NOW()),
('Hate Speech', 'keyword', 'offensive,hate,slur', 'remove', 'high', 1, 1, NOW());

-- =====================================================
-- Done!
-- =====================================================
