-- Advanced Dashboard Config v2.0 Schema Enhancement
-- Adds support for institute mode, theme settings, and layout presets

-- Add new columns to user_dashboard_config table
ALTER TABLE user_dashboard_config 
ADD COLUMN IF NOT EXISTS version VARCHAR(10) DEFAULT 'v2' AFTER config;

ALTER TABLE user_dashboard_config 
ADD COLUMN IF NOT EXISTS institute_mode_id INT NULL AFTER version;

ALTER TABLE user_dashboard_config 
ADD COLUMN IF NOT EXISTS theme_settings JSON NULL AFTER institute_mode_id;

ALTER TABLE user_dashboard_config 
ADD COLUMN IF NOT EXISTS layout_preset VARCHAR(50) DEFAULT 'default' AFTER theme_settings;

-- Add foreign key for institute mode (if institutes table exists)
-- ALTER TABLE user_dashboard_config 
-- ADD CONSTRAINT fk_dashboard_institute_mode 
-- FOREIGN KEY (institute_mode_id) REFERENCES institutes(id) ON DELETE SET NULL;

-- Create presets table
CREATE TABLE IF NOT EXISTS dashboard_presets (
    id INT PRIMARY KEY AUTO_INCREMENT,
    preset_id VARCHAR(50) UNIQUE NOT NULL,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    config JSON NOT NULL,
    is_system BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Insert default presets
INSERT INTO dashboard_presets (preset_id, name, description, config) VALUES
('default', 'Default', 'Standard dashboard with all widgets', '{
  "widgets": [
    {"id": "hero_slider", "enabled": true, "order": 0, "settings": {"autoPlay": true, "interval": 5000}},
    {"id": "quick_actions", "enabled": true, "order": 1, "settings": {"columns": 4, "showLabels": true}},
    {"id": "stats", "enabled": true, "order": 2, "settings": {"display": "cards"}},
    {"id": "enrolled_courses", "enabled": true, "order": 3, "settings": {"displayMode": "carousel", "maxItems": 6}},
    {"id": "superapp", "enabled": true, "order": 4, "settings": {"showAllApps": true}},
    {"id": "recent_activity", "enabled": true, "order": 5, "settings": {"maxItems": 5}}
  ],
  "theme": "default",
  "layout": "grid"
}'),
('student', 'Student Focus', 'Courses and learning first', '{
  "widgets": [
    {"id": "enrolled_courses", "enabled": true, "order": 0, "settings": {"displayMode": "list", "maxItems": 10}},
    {"id": "recent_activity", "enabled": true, "order": 1, "settings": {"maxItems": 8}},
    {"id": "stats", "enabled": true, "order": 2, "settings": {"display": "compact"}},
    {"id": "quick_actions", "enabled": true, "order": 3, "settings": {"columns": 3}},
    {"id": "hero_slider", "enabled": false, "order": 4},
    {"id": "superapp", "enabled": false, "order": 5}
  ],
  "theme": "default",
  "layout": "list"
}'),
('minimal', 'Minimal', 'Essential widgets only', '{
  "widgets": [
    {"id": "quick_actions", "enabled": true, "order": 0, "settings": {"columns": 4}},
    {"id": "enrolled_courses", "enabled": true, "order": 1, "settings": {"displayMode": "compact", "maxItems": 4}},
    {"id": "hero_slider", "enabled": false, "order": 2},
    {"id": "stats", "enabled": false, "order": 3},
    {"id": "superapp", "enabled": false, "order": 4},
    {"id": "recent_activity", "enabled": false, "order": 5}
  ],
  "theme": "default",
  "layout": "grid"
}'),
('institute', 'Institute Mode', 'Single institute focus', '{
  "widgets": [
    {"id": "hero_slider", "enabled": true, "order": 0, "settings": {"instituteBanner": true}},
    {"id": "enrolled_courses", "enabled": true, "order": 1, "settings": {"filterByInstitute": true}},
    {"id": "quick_actions", "enabled": true, "order": 2, "settings": {"columns": 4, "instituteActions": true}},
    {"id": "stats", "enabled": true, "order": 3},
    {"id": "recent_activity", "enabled": true, "order": 4},
    {"id": "superapp", "enabled": false, "order": 5}
  ],
  "theme": "institute",
  "layout": "grid",
  "instituteMode": true
}')
ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP;
