-- ============================================
-- INSTITUTE CONFIGURATION & TEMPLATE SYSTEM
-- ============================================
-- This migration adds comprehensive customization support for institutes

-- 1. CREATE institute_templates table - Pre-built website templates
CREATE TABLE IF NOT EXISTS institute_templates (
    id VARCHAR(50) PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    thumbnail_url VARCHAR(500),
    preview_url VARCHAR(500),
    category ENUM('modern', 'classic', 'minimal', 'bold', 'academic') DEFAULT 'modern',
    is_premium BOOLEAN DEFAULT FALSE,
    is_active BOOLEAN DEFAULT TRUE,
    default_config JSON COMMENT 'Default configuration for this template',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='Pre-built website templates for institutes';

-- Insert default templates
INSERT INTO institute_templates (id, name, description, category, thumbnail_url, is_premium, default_config) VALUES
('modern', 'Modern', 'Clean, professional design with subtle animations and gradients', 'modern', '/templates/modern.png', FALSE, 
 '{"header_style":"solid","hero_style":"full","footer_style":"detailed","primary_color":"#10B981","secondary_color":"#3B82F6"}'),
('classic', 'Classic', 'Traditional academic look with structured layout', 'classic', '/templates/classic.png', FALSE,
 '{"header_style":"solid","hero_style":"centered","footer_style":"detailed","primary_color":"#1E40AF","secondary_color":"#7C3AED"}'),
('minimal', 'Minimal', 'Simple, content-focused design with clean aesthetics', 'minimal', '/templates/minimal.png', FALSE,
 '{"header_style":"transparent","hero_style":"split","footer_style":"simple","primary_color":"#111827","secondary_color":"#6B7280"}'),
('bold', 'Bold', 'Vibrant colors and eye-catching elements for maximum impact', 'bold', '/templates/bold.png', FALSE,
 '{"header_style":"gradient","hero_style":"full","footer_style":"detailed","primary_color":"#DC2626","secondary_color":"#F59E0B"}'),
('academic', 'Academic', 'Formal design suitable for universities and colleges', 'academic', '/templates/academic.png', FALSE,
 '{"header_style":"solid","hero_style":"centered","footer_style":"detailed","primary_color":"#0F172A","secondary_color":"#334155"}');

-- 2. CREATE institute_config table - Comprehensive customization
CREATE TABLE IF NOT EXISTS institute_config (
    id INT AUTO_INCREMENT PRIMARY KEY,
    institute_id INT NOT NULL UNIQUE,
    
    -- Template System
    template_id VARCHAR(50) DEFAULT 'modern' COMMENT 'Selected template',
    template_variant VARCHAR(50) DEFAULT 'default' COMMENT 'light, dark, colorful',
    
    -- Theme Colors (CSS Variables)
    primary_color VARCHAR(9) DEFAULT '#10B981',
    secondary_color VARCHAR(9) DEFAULT '#3B82F6',
    accent_color VARCHAR(9) DEFAULT '#F59E0B',
    text_color VARCHAR(9) DEFAULT '#1F2937',
    text_muted_color VARCHAR(9) DEFAULT '#6B7280',
    background_color VARCHAR(9) DEFAULT '#FFFFFF',
    surface_color VARCHAR(9) DEFAULT '#F9FAFB',
    border_color VARCHAR(9) DEFAULT '#E5E7EB',
    
    -- Gradient Colors
    gradient_start VARCHAR(9) DEFAULT '#10B981',
    gradient_end VARCHAR(9) DEFAULT '#059669',
    gradient_direction VARCHAR(50) DEFAULT 'to right',
    
    -- Typography
    heading_font VARCHAR(100) DEFAULT 'Inter',
    body_font VARCHAR(100) DEFAULT 'Inter',
    font_size_base INT DEFAULT 16 COMMENT 'Base font size in pixels',
    
    -- Border & Shadows
    border_radius VARCHAR(20) DEFAULT '0.5rem',
    shadow_style ENUM('none', 'subtle', 'medium', 'strong') DEFAULT 'subtle',
    
    -- Header Configuration
    header_style ENUM('transparent', 'solid', 'gradient', 'sticky') DEFAULT 'solid',
    header_bg_color VARCHAR(9) DEFAULT '#FFFFFF',
    header_text_color VARCHAR(9) DEFAULT '#1F2937',
    header_height VARCHAR(20) DEFAULT '70px',
    show_top_bar BOOLEAN DEFAULT FALSE,
    top_bar_bg_color VARCHAR(9) DEFAULT '#1F2937',
    top_bar_text VARCHAR(500),
    header_cta_text VARCHAR(100),
    header_cta_link VARCHAR(255),
    
    -- Logo Configuration
    logo_position ENUM('left', 'center') DEFAULT 'left',
    logo_size VARCHAR(20) DEFAULT '40px',
    show_institute_name BOOLEAN DEFAULT TRUE,
    
    -- Hero Section Configuration
    hero_style ENUM('full', 'split', 'centered', 'video', 'slider', 'minimal') DEFAULT 'full',
    hero_height VARCHAR(20) DEFAULT '80vh',
    hero_title TEXT,
    hero_subtitle TEXT,
    hero_cta_primary_text VARCHAR(100) DEFAULT 'Explore Courses',
    hero_cta_primary_link VARCHAR(255) DEFAULT '/marketplace',
    hero_cta_secondary_text VARCHAR(100) DEFAULT 'Contact Us',
    hero_cta_secondary_link VARCHAR(255) DEFAULT '/contact',
    hero_image_url VARCHAR(500),
    hero_video_url VARCHAR(500),
    hero_overlay_enabled BOOLEAN DEFAULT TRUE,
    hero_overlay_color VARCHAR(9) DEFAULT '#00000080',
    hero_text_align ENUM('left', 'center', 'right') DEFAULT 'left',
    
    -- Sections Configuration (JSON with order and settings)
    sections_config JSON DEFAULT (JSON_OBJECT(
        'hero', JSON_OBJECT('enabled', TRUE, 'order', 1),
        'stats', JSON_OBJECT('enabled', TRUE, 'order', 2),
        'featured_courses', JSON_OBJECT('enabled', TRUE, 'order', 3, 'limit', 6),
        'categories', JSON_OBJECT('enabled', TRUE, 'order', 4),
        'instructors', JSON_OBJECT('enabled', TRUE, 'order', 5, 'limit', 4),
        'testimonials', JSON_OBJECT('enabled', FALSE, 'order', 6),
        'why_choose_us', JSON_OBJECT('enabled', TRUE, 'order', 7),
        'cta', JSON_OBJECT('enabled', TRUE, 'order', 8),
        'newsletter', JSON_OBJECT('enabled', FALSE, 'order', 9),
        'partners', JSON_OBJECT('enabled', FALSE, 'order', 10)
    )),
    
    -- Stats Section
    stats_style ENUM('simple', 'cards', 'animated') DEFAULT 'cards',
    custom_stats JSON COMMENT 'Custom statistics to display',
    
    -- Courses Section
    courses_layout ENUM('grid', 'carousel', 'list') DEFAULT 'grid',
    courses_per_row INT DEFAULT 3,
    show_course_price BOOLEAN DEFAULT TRUE,
    show_course_rating BOOLEAN DEFAULT TRUE,
    show_course_instructor BOOLEAN DEFAULT TRUE,
    
    -- Footer Configuration
    footer_style ENUM('simple', 'detailed', 'minimal', 'mega') DEFAULT 'detailed',
    footer_bg_color VARCHAR(9) DEFAULT '#1F2937',
    footer_text_color VARCHAR(9) DEFAULT '#D1D5DB',
    footer_columns INT DEFAULT 4,
    show_powered_by BOOLEAN DEFAULT TRUE,
    copyright_text VARCHAR(500),
    footer_links JSON COMMENT 'Custom footer links organized by columns',
    
    -- Navigation Configuration
    nav_style ENUM('horizontal', 'hamburger', 'mega') DEFAULT 'horizontal',
    nav_items JSON COMMENT 'Custom navigation menu items',
    show_search_in_nav BOOLEAN DEFAULT TRUE,
    show_auth_buttons BOOLEAN DEFAULT TRUE,
    
    -- Custom Content
    custom_css TEXT,
    custom_js TEXT,
    custom_head_code TEXT COMMENT 'Code to inject in <head>',
    custom_body_start_code TEXT,
    custom_body_end_code TEXT,
    
    -- SEO Settings
    meta_title VARCHAR(255),
    meta_description TEXT,
    meta_keywords TEXT,
    og_image_url VARCHAR(500),
    favicon_url VARCHAR(500),
    robots_txt TEXT,
    
    -- Feature Toggles
    enable_dark_mode BOOLEAN DEFAULT TRUE,
    enable_language_switcher BOOLEAN DEFAULT FALSE,
    enable_search BOOLEAN DEFAULT TRUE,
    enable_wishlist BOOLEAN DEFAULT TRUE,
    enable_reviews BOOLEAN DEFAULT TRUE,
    enable_certificates BOOLEAN DEFAULT TRUE,
    enable_discussions BOOLEAN DEFAULT TRUE,
    enable_live_classes BOOLEAN DEFAULT TRUE,
    enable_blog BOOLEAN DEFAULT FALSE,
    enable_cart BOOLEAN DEFAULT TRUE,
    
    -- Contact Widget
    show_floating_contact BOOLEAN DEFAULT FALSE,
    floating_contact_position ENUM('bottom-right', 'bottom-left') DEFAULT 'bottom-right',
    floating_contact_config JSON,
    
    -- Social Media Links (in addition to institute social_links)
    display_social_in_header BOOLEAN DEFAULT FALSE,
    display_social_in_footer BOOLEAN DEFAULT TRUE,
    
    -- Analytics
    google_analytics_id VARCHAR(50),
    google_tag_manager_id VARCHAR(50),
    facebook_pixel_id VARCHAR(50),
    custom_tracking_code TEXT,
    
    -- Mobile Configuration
    mobile_menu_style ENUM('slide', 'fullscreen', 'dropdown') DEFAULT 'slide',
    mobile_bottom_nav BOOLEAN DEFAULT FALSE,
    mobile_bottom_nav_items JSON,
    
    -- Loading & Animations
    loading_style ENUM('spinner', 'skeleton', 'progress', 'none') DEFAULT 'skeleton',
    animations_enabled BOOLEAN DEFAULT TRUE,
    animation_speed ENUM('slow', 'normal', 'fast') DEFAULT 'normal',
    
    -- Timestamps
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    CONSTRAINT fk_institute_config_institute FOREIGN KEY (institute_id) 
        REFERENCES institutes(id) ON DELETE CASCADE,
    CONSTRAINT fk_institute_config_template FOREIGN KEY (template_id)
        REFERENCES institute_templates(id) ON DELETE SET NULL,
    
    -- Indexes
    INDEX idx_config_template (template_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='Comprehensive institute customization settings';

-- 3. CREATE institute_widgets table - Dynamic content widgets
CREATE TABLE IF NOT EXISTS institute_widgets (
    id INT AUTO_INCREMENT PRIMARY KEY,
    institute_id INT NOT NULL,
    widget_type ENUM(
        'banner', 'announcement', 'countdown', 'promo',
        'featured_course', 'course_carousel',
        'testimonial', 'testimonial_slider',
        'stats', 'stats_animated',
        'cta', 'cta_split',
        'video', 'video_gallery',
        'faq', 'accordion',
        'team', 'team_carousel',
        'partners', 'partner_logos',
        'newsletter', 'newsletter_popup',
        'social_feed', 'instagram_feed',
        'custom_html', 'custom_section'
    ) NOT NULL,
    title VARCHAR(255),
    subtitle TEXT,
    config JSON NOT NULL COMMENT 'Widget-specific configuration',
    content JSON COMMENT 'Widget content data',
    position ENUM('header_above', 'header_below', 'hero_above', 'hero_below', 
                  'content', 'sidebar', 'footer_above', 'footer_below', 
                  'modal', 'popup', 'floating') DEFAULT 'content',
    page_target VARCHAR(100) DEFAULT 'all' COMMENT 'Which pages to show: all, home, courses, etc',
    display_order INT DEFAULT 0,
    is_active BOOLEAN DEFAULT TRUE,
    start_date DATETIME NULL,
    end_date DATETIME NULL,
    display_rules JSON COMMENT 'Conditional display rules',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    CONSTRAINT fk_widget_institute FOREIGN KEY (institute_id) 
        REFERENCES institutes(id) ON DELETE CASCADE,
    
    INDEX idx_widget_institute_position (institute_id, position, display_order),
    INDEX idx_widget_active (is_active, start_date, end_date),
    INDEX idx_widget_page (page_target)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='Dynamic content widgets for institutes';

-- 4. CREATE institute_pages table - Custom pages for institutes
CREATE TABLE IF NOT EXISTS institute_pages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    institute_id INT NOT NULL,
    slug VARCHAR(100) NOT NULL,
    title VARCHAR(255) NOT NULL,
    content LONGTEXT,
    page_type ENUM('static', 'landing', 'course_list', 'instructor_list', 'custom') DEFAULT 'static',
    template VARCHAR(50) DEFAULT 'default',
    meta_title VARCHAR(255),
    meta_description TEXT,
    is_published BOOLEAN DEFAULT FALSE,
    show_in_nav BOOLEAN DEFAULT FALSE,
    nav_order INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    CONSTRAINT fk_page_institute FOREIGN KEY (institute_id) 
        REFERENCES institutes(id) ON DELETE CASCADE,
    UNIQUE KEY unique_institute_page_slug (institute_id, slug),
    INDEX idx_page_published (is_published),
    INDEX idx_page_nav (show_in_nav, nav_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='Custom pages created by institutes';

-- 5. Insert default config for existing institutes
INSERT INTO institute_config (institute_id, template_id, primary_color, secondary_color)
SELECT id, 'modern', '#10B981', '#3B82F6' 
FROM institutes 
WHERE id NOT IN (SELECT institute_id FROM institute_config);

-- ============================================
-- VERIFICATION
-- ============================================
-- Run these to verify:
-- SHOW TABLES LIKE 'institute%';
-- DESCRIBE institute_config;
-- SELECT * FROM institute_templates;
