-- ============================================================================
-- EasyStream - Initial Settings for Docker Deployment
-- ============================================================================
-- This file inserts default configuration settings into the database
-- Loaded automatically after table creation by docker-entrypoint-initdb.d
-- ============================================================================

USE `easystream`;

-- Insert default site settings
INSERT INTO `db_settings` (`setting_name`, `setting_value`, `setting_type`, `setting_category`) VALUES
('site_name', 'EasyStream', 'text', 'general'),
('site_description', 'Video Streaming Platform', 'text', 'general'),
('site_keywords', 'video, streaming, live, upload', 'text', 'general'),
('site_url', 'http://localhost:8083', 'text', 'general'),
('site_email', 'admin@easystream.local', 'email', 'general'),
('site_timezone', 'UTC', 'text', 'general'),
('site_language', 'en_US', 'text', 'general'),
('site_logo', '', 'text', 'branding'),
('site_favicon', '', 'text', 'branding'),

-- User settings
('user_registration_enabled', '1', 'boolean', 'users'),
('user_email_verification', '1', 'boolean', 'users'),
('user_default_role', 'user', 'text', 'users'),
('user_upload_limit', '2048', 'number', 'users'),
('user_storage_limit', '10240', 'number', 'users'),

-- Video settings
('video_max_filesize', '2048', 'number', 'video'),
('video_allowed_formats', 'mp4,avi,mov,wmv,flv,mkv', 'text', 'video'),
('video_auto_process', '1', 'boolean', 'video'),
('video_default_privacy', 'public', 'text', 'video'),
('video_enable_comments', '1', 'boolean', 'video'),
('video_enable_likes', '1', 'boolean', 'video'),
('video_enable_download', '0', 'boolean', 'video'),

-- Streaming settings
('streaming_enabled', '1', 'boolean', 'streaming'),
('streaming_rtmp_url', 'rtmp://localhost:1935/live', 'text', 'streaming'),
('streaming_hls_enabled', '1', 'boolean', 'streaming'),
('streaming_record_enabled', '1', 'boolean', 'streaming'),

-- Security settings
('security_captcha_enabled', '0', 'boolean', 'security'),
('security_rate_limit', '100', 'number', 'security'),
('security_session_timeout', '3600', 'number', 'security'),
('security_password_min_length', '8', 'number', 'security'),
('security_2fa_enabled', '0', 'boolean', 'security'),

-- Email settings
('email_enabled', '0', 'boolean', 'email'),
('email_from_name', 'EasyStream', 'text', 'email'),
('email_from_address', 'noreply@easystream.local', 'email', 'email'),
('email_smtp_host', '', 'text', 'email'),
('email_smtp_port', '587', 'number', 'email'),
('email_smtp_secure', 'tls', 'text', 'email'),
('email_smtp_username', '', 'text', 'email'),
('email_smtp_password', '', 'password', 'email'),

-- Storage settings
('storage_driver', 'local', 'text', 'storage'),
('storage_local_path', '/srv/easystream/f_data', 'text', 'storage'),
('storage_s3_enabled', '0', 'boolean', 'storage'),
('storage_cdn_enabled', '0', 'boolean', 'storage'),

-- Monetization settings
('monetization_enabled', '0', 'boolean', 'monetization'),
('monetization_currency', 'USD', 'text', 'monetization'),
('monetization_payment_gateway', 'stripe', 'text', 'monetization'),

-- Analytics settings
('analytics_enabled', '1', 'boolean', 'analytics'),
('analytics_track_views', '1', 'boolean', 'analytics'),
('analytics_track_downloads', '1', 'boolean', 'analytics'),
('analytics_retention_days', '90', 'number', 'analytics'),

-- API settings
('api_enabled', '1', 'boolean', 'api'),
('api_rate_limit', '1000', 'number', 'api'),
('api_version', 'v1', 'text', 'api'),

-- Template Builder settings
('templatebuilder_enabled', '1', 'boolean', 'templatebuilder'),
('templatebuilder_autosave_interval', '3', 'number', 'templatebuilder'),
('templatebuilder_max_versions', '50', 'number', 'templatebuilder'),

-- Maintenance settings
('maintenance_mode', '0', 'boolean', 'maintenance'),
('maintenance_message', 'Site is under maintenance. Please check back later.', 'text', 'maintenance')

ON DUPLICATE KEY UPDATE
    `setting_value` = VALUES(`setting_value`),
    `updated_at` = CURRENT_TIMESTAMP;

-- Insert default admin user (username: admin, password: admin123 - CHANGE THIS!)
-- Password hash for "admin123" using PHP password_hash with BCRYPT
INSERT INTO `db_accountuser` (
    `usr_key`,
    `usr_user`,
    `usr_password`,
    `usr_email`,
    `usr_status`,
    `usr_role`,
    `usr_dname`,
    `usr_created`,
    `usr_verified`
) VALUES (
    1,
    'admin',
    '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', -- admin123
    'admin@easystream.local',
    'active',
    'admin',
    'Administrator',
    NOW(),
    1
) ON DUPLICATE KEY UPDATE `usr_key` = VALUES(`usr_key`);

-- Insert default categories
INSERT INTO `db_categories` (`ct_name`, `ct_slug`, `ct_type`, `ct_active`, `ct_order`) VALUES
('Entertainment', 'entertainment', 'video', 1, 1),
('Music', 'music', 'video', 1, 2),
('Gaming', 'gaming', 'video', 1, 3),
('Education', 'education', 'video', 1, 4),
('News', 'news', 'video', 1, 5),
('Sports', 'sports', 'video', 1, 6),
('Technology', 'technology', 'video', 1, 7),
('Travel', 'travel', 'video', 1, 8),
('Food', 'food', 'video', 1, 9),
('Comedy', 'comedy', 'video', 1, 10)
ON DUPLICATE KEY UPDATE `ct_name` = VALUES(`ct_name`);

-- Insert default template builder components (if not already present)
INSERT INTO `db_templatebuilder_components` (
    `component_name`,
    `component_type`,
    `component_category`,
    `component_html`,
    `component_css`,
    `component_thumbnail`,
    `is_active`
) VALUES
('Hero Section', 'section', 'hero', '<section class="hero"><div class="container"><h1>{{title}}</h1><p>{{subtitle}}</p></div></section>', '.hero { padding: 80px 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; text-align: center; }', '', 1),
('Video Grid', 'grid', 'content', '<div class="video-grid">{{videos}}</div>', '.video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }', '', 1),
('Navigation Bar', 'header', 'navigation', '<nav class="navbar"><div class="container"><a href="/" class="logo">{{site_name}}</a><ul class="nav-menu">{{menu_items}}</ul></div></nav>', '.navbar { background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 1rem 0; }', '', 1),
('Footer', 'footer', 'footer', '<footer class="site-footer"><div class="container"><p>&copy; 2025 {{site_name}}. All rights reserved.</p></div></footer>', '.site-footer { background: #333; color: #fff; padding: 2rem 0; text-align: center; }', '', 1),
('Call to Action', 'section', 'cta', '<section class="cta"><div class="container"><h2>{{heading}}</h2><p>{{description}}</p><a href="{{link}}" class="btn btn-primary">{{button_text}}</a></div></section>', '.cta { background: #f8f9fa; padding: 60px 0; text-align: center; }', '', 1),
('Feature Cards', 'grid', 'features', '<div class="features-grid">{{features}}</div>', '.features-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 30px; }', '', 1),
('Sidebar', 'aside', 'sidebar', '<aside class="sidebar">{{widgets}}</aside>', '.sidebar { background: #f8f9fa; padding: 20px; border-radius: 8px; }', '', 1)
ON DUPLICATE KEY UPDATE `component_name` = VALUES(`component_name`);

-- Grant necessary permissions
FLUSH PRIVILEGES;

-- Confirm initialization
SELECT 'EasyStream database initialized successfully!' AS status;
