-- Settings table for storing application configuration
CREATE TABLE IF NOT EXISTS `settings` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `setting_key` varchar(100) NOT NULL UNIQUE,
    `setting_value` text,
    `setting_type` enum('string','integer','boolean','json','text') DEFAULT 'string',
    `description` text,
    `is_public` boolean DEFAULT FALSE,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `setting_key` (`setting_key`),
    INDEX `idx_public` (`is_public`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Admin logs table for tracking setting changes
CREATE TABLE IF NOT EXISTS `admin_logs` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11),
    `action` varchar(100) NOT NULL,
    `table_name` varchar(50),
    `record_id` varchar(100),
    `details` text,
    `ip_address` varchar(45),
    `user_agent` text,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    INDEX `idx_user_id` (`user_id`),
    INDEX `idx_action` (`action`),
    INDEX `idx_table` (`table_name`),
    INDEX `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Insert default settings
INSERT INTO `settings` (`setting_key`, `setting_value`, `setting_type`, `description`, `is_public`) VALUES
('site_title', 'ParallelPx', 'string', 'Website title', TRUE),
('site_description', 'Gaming content and community', 'text', 'Website description', TRUE),
('site_url', 'http://localhost:8080', 'string', 'Base URL of the website', TRUE),
('admin_email', 'admin@parallelpx.com', 'string', 'Administrator email address', FALSE),
('maintenance_mode', '0', 'boolean', 'Enable maintenance mode', FALSE),
('max_upload_size', '10485760', 'integer', 'Maximum file upload size in bytes (10MB)', FALSE),
('cache_enabled', '1', 'boolean', 'Enable application caching', FALSE),
('debug_mode', '0', 'boolean', 'Enable debug mode', FALSE),
('analytics_tracking_id', '', 'string', 'Google Analytics tracking ID', FALSE),
('social_share_enabled', '1', 'boolean', 'Enable social media sharing', TRUE),
('comments_enabled', '1', 'boolean', 'Enable comments system', TRUE),
('user_registration_enabled', '1', 'boolean', 'Allow new user registrations', TRUE)
ON DUPLICATE KEY UPDATE 
    `setting_value` = VALUES(`setting_value`),
    `updated_at` = CURRENT_TIMESTAMP;