-- ============================================================
-- MIGRAÇÃO: Sistema de White Label / Customização
-- Data: 2025-11
-- Sistema: Checkout Pro 2025
-- ============================================================

-- Criar tabela de configurações do sistema
CREATE TABLE IF NOT EXISTS `system_settings` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `setting_key` VARCHAR(100) NOT NULL,
    `setting_value` TEXT DEFAULT NULL,
    `setting_type` VARCHAR(50) DEFAULT 'text',
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `setting_key` (`setting_key`),
    KEY `idx_setting_type` (`setting_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Inserir configurações padrão de cores (apenas as que realmente são usadas no sistema)
INSERT INTO `system_settings` (`setting_key`, `setting_value`, `setting_type`) VALUES
-- Cores do Dashboard
('color_sidebar_bg', '#1e3a8a', 'color'),
('color_sidebar_text', '#e0e7ff', 'color'),
('color_header_bg', '#ffffff', 'color'),
('color_header_text', '#1f2937', 'color'),
('color_body_bg', '#f3f4f6', 'color'),
('color_card_bg', '#ffffff', 'color'),
('color_text_main', '#1f2937', 'color'),
('color_text_muted', '#6b7280', 'color'),
('color_border', '#e5e7eb', 'color'),
-- Cores do Marketplace
('color_marketplace_bg', '#0f0c29', 'color'),
('color_marketplace_title', '#667eea', 'color'),
('color_marketplace_header_bg', '#ffffff', 'color'),
('color_marketplace_header_text', '#e0e0e0', 'color'),
('color_marketplace_card_bg', '#ffffff', 'color'),
('color_marketplace_product_name', '#ffffff', 'color'),
('color_marketplace_category', '#667eea', 'color'),
('color_marketplace_price', '#4ade80', 'color'),
('color_marketplace_button_buy', '#667eea', 'color'),
('color_marketplace_button_affiliate', '#10b981', 'color'),
-- Cores de Botões e Ações
('color_button_primary', '#009846', 'color'),
('color_button_primary_hover', '#007635', 'color'),
('color_button_edit', '#2196F3', 'color'),
('color_button_view', '#17a2b8', 'color'),
('color_button_delete', '#dc3545', 'color'),
('color_button_save', '#28a745', 'color'),
-- Cores de Status
('color_status_success', '#28a745', 'color'),
('color_status_warning', '#ffc107', 'color'),
('color_status_danger', '#dc3545', 'color'),
('color_status_info', '#17a2b8', 'color'),
-- Cores de Tabelas
('color_table_header_bg', '#f8f9fa', 'color'),
('color_table_header_text', '#495057', 'color'),
('color_table_row_hover', '#f5f5f5', 'color'),
-- Cores de Links
('color_link', '#007bff', 'color'),
('color_link_hover', '#0056b3', 'color'),
-- Cores de Badges
('color_badge_success', '#28a745', 'color'),
('color_badge_danger', '#dc3545', 'color'),
('color_badge_info', '#17a2b8', 'color'),
('color_badge_warning', '#ffc107', 'color'),
-- Cores de Inputs
('color_input_focus', '#009846', 'color'),
('color_input_focus_shadow', 'rgba(0, 152, 70, 0.25)', 'color'),
-- Cores de Mensagens
('color_message_success_bg', '#d4edda', 'color'),
('color_message_success_text', '#155724', 'color'),
('color_message_error_bg', '#f8d7da', 'color'),
('color_message_error_text', '#721c24', 'color'),
-- Cores de Modais
('color_modal_bg', '#ffffff', 'color'),
('color_modal_header_bg', '#f8f9fa', 'color'),
('color_modal_header_text', '#333333', 'color'),
-- Cores Dark Mode (valores padrão otimizados)
('color_header_bg_dark', '#0f172a', 'color'),
('color_header_text_dark', '#ffffff', 'color'),
('color_body_bg_dark', '#020617', 'color'),
('color_card_bg_dark', '#0f172a', 'color'),
('color_text_main_dark', '#ffffff', 'color'),
('color_text_muted_dark', '#94a3b8', 'color'),
('color_border_dark', 'rgba(255,255,255,0.08)', 'color'),
('color_table_header_bg_dark', '#1e293b', 'color'),
('color_table_header_text_dark', '#e2e8f0', 'color'),
('color_table_row_hover_dark', '#334155', 'color'),
('color_message_success_bg_dark', 'rgba(34, 197, 94, 0.2)', 'color'),
('color_message_success_text_dark', '#86efac', 'color'),
('color_message_error_bg_dark', 'rgba(239, 68, 68, 0.2)', 'color'),
('color_message_error_text_dark', '#fca5a5', 'color'),
('color_modal_bg_dark', '#0f172a', 'color'),
('color_modal_header_bg_dark', '#1e293b', 'color'),
('color_modal_header_text_dark', '#ffffff', 'color'),
-- Cores Dark Mode Adicionais para Botões e Elementos
('color_button_primary_dark', '#22c55e', 'color'),
('color_button_primary_hover_dark', '#16a34a', 'color'),
('color_button_edit_dark', '#3b82f6', 'color'),
('color_button_view_dark', '#06b6d4', 'color'),
('color_button_delete_dark', '#ef4444', 'color'),
('color_button_save_dark', '#22c55e', 'color'),
('color_status_success_dark', '#4ade80', 'color'),
('color_status_warning_dark', '#fbbf24', 'color'),
('color_status_danger_dark', '#f87171', 'color'),
('color_status_info_dark', '#22d3ee', 'color'),
('color_link_dark', '#60a5fa', 'color'),
('color_link_hover_dark', '#93c5fd', 'color'),
('color_input_focus_dark', '#22c55e', 'color'),
('color_input_focus_shadow_dark', 'rgba(34, 197, 94, 0.3)', 'color')
ON DUPLICATE KEY UPDATE `setting_value` = VALUES(`setting_value`);

-- Inserir configurações padrão de branding (vazias inicialmente)
INSERT INTO `system_settings` (`setting_key`, `setting_value`, `setting_type`) VALUES
('logo_sidebar_admin', '', 'image'),
('logo_sidebar_user', '', 'image'),
('logo_favicon', '', 'image')
ON DUPLICATE KEY UPDATE `setting_value` = VALUES(`setting_value`);

-- Garantir que a coluna whatsapp existe na tabela usuarios
SET @column_exists = (
    SELECT COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = DATABASE() 
    AND TABLE_NAME = 'usuarios' 
    AND COLUMN_NAME = 'whatsapp'
);

SET @sql = IF(@column_exists = 0,
    'ALTER TABLE usuarios ADD COLUMN whatsapp VARCHAR(20) NULL DEFAULT NULL COMMENT ''WhatsApp do usuário'' AFTER email',
    'SELECT ''Coluna whatsapp já existe'' AS message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Adicionar índice para whatsapp se não existir
SET @index_exists = (
    SELECT COUNT(*) 
    FROM INFORMATION_SCHEMA.STATISTICS 
    WHERE TABLE_SCHEMA = DATABASE() 
    AND TABLE_NAME = 'usuarios' 
    AND INDEX_NAME = 'idx_whatsapp'
);

SET @sql_index = IF(@index_exists = 0,
    'ALTER TABLE usuarios ADD INDEX idx_whatsapp (whatsapp)',
    'SELECT ''Índice idx_whatsapp já existe'' AS message'
);

PREPARE stmt_index FROM @sql_index;
EXECUTE stmt_index;
DEALLOCATE PREPARE stmt_index;

-- Adicionar campo last_login na tabela usuarios se não existir
SET @last_login_exists = (
    SELECT COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = DATABASE() 
    AND TABLE_NAME = 'usuarios' 
    AND COLUMN_NAME = 'last_login'
);

SET @sql_last_login = IF(@last_login_exists = 0,
    'ALTER TABLE usuarios ADD COLUMN last_login DATETIME NULL DEFAULT NULL COMMENT ''Último login do usuário'' AFTER updated_at',
    'SELECT ''Coluna last_login já existe'' AS message'
);

PREPARE stmt_last_login FROM @sql_last_login;
EXECUTE stmt_last_login;
DEALLOCATE PREPARE stmt_last_login;

