-- Tesla Coil Spark Course - Database Schema
-- SQLite database for progress tracking, exercises, and user data

-- User profiles and preferences
CREATE TABLE IF NOT EXISTS users (
    user_id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT UNIQUE NOT NULL,
    email TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_login TIMESTAMP,
    current_learning_path TEXT DEFAULT 'intermediate',
    theme_preference TEXT DEFAULT 'light',
    font_size INTEGER DEFAULT 14,
    auto_save_enabled BOOLEAN DEFAULT 1,
    show_hints BOOLEAN DEFAULT 1
);

-- Lesson progress tracking
CREATE TABLE IF NOT EXISTS lesson_progress (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    lesson_id TEXT NOT NULL,
    status TEXT CHECK(status IN ('not_started', 'in_progress', 'completed')) DEFAULT 'not_started',
    first_opened TIMESTAMP,
    last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    time_spent INTEGER DEFAULT 0,  -- Total seconds spent
    scroll_position FLOAT DEFAULT 0.0,  -- 0.0 to 1.0
    completion_percentage INTEGER DEFAULT 0,  -- 0 to 100
    completed_at TIMESTAMP,
    notes TEXT,
    bookmarked BOOLEAN DEFAULT 0,
    FOREIGN KEY (user_id) REFERENCES users(user_id),
    UNIQUE(user_id, lesson_id)
);

-- Exercise attempts (all attempts recorded)
CREATE TABLE IF NOT EXISTS exercise_attempts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    exercise_id TEXT NOT NULL,
    lesson_id TEXT,
    attempt_number INTEGER NOT NULL,
    user_answer TEXT,
    is_correct BOOLEAN NOT NULL,
    points_earned INTEGER DEFAULT 0,
    points_possible INTEGER NOT NULL,
    hints_used INTEGER DEFAULT 0,
    time_taken INTEGER,  -- Seconds
    attempted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(user_id)
);

-- Exercise completion (best performance)
CREATE TABLE IF NOT EXISTS exercise_completion (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    exercise_id TEXT NOT NULL,
    best_score INTEGER NOT NULL,  -- Points earned
    max_possible INTEGER NOT NULL,  -- Total points available
    total_attempts INTEGER DEFAULT 1,
    first_attempted TIMESTAMP,
    first_completed TIMESTAMP,
    last_attempted TIMESTAMP,
    average_time INTEGER,  -- Average seconds per attempt
    FOREIGN KEY (user_id) REFERENCES users(user_id),
    UNIQUE(user_id, exercise_id)
);

-- Study sessions for streak tracking
CREATE TABLE IF NOT EXISTS study_sessions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    session_date DATE NOT NULL,  -- Just the date (YYYY-MM-DD)
    session_start TIMESTAMP NOT NULL,
    session_end TIMESTAMP,
    lessons_viewed INTEGER DEFAULT 0,
    lessons_completed INTEGER DEFAULT 0,
    exercises_attempted INTEGER DEFAULT 0,
    exercises_completed INTEGER DEFAULT 0,
    points_earned INTEGER DEFAULT 0,
    time_active INTEGER DEFAULT 0,  -- Seconds
    FOREIGN KEY (user_id) REFERENCES users(user_id),
    UNIQUE(user_id, session_date)
);

-- Achievements and badges
CREATE TABLE IF NOT EXISTS achievements (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    achievement_id TEXT NOT NULL,  -- e.g., 'quick_learner', 'streak_master'
    achievement_name TEXT NOT NULL,
    achievement_description TEXT,
    earned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    details TEXT,  -- JSON with achievement specifics
    FOREIGN KEY (user_id) REFERENCES users(user_id),
    UNIQUE(user_id, achievement_id)
);

-- Bookmarks and notes
CREATE TABLE IF NOT EXISTS bookmarks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    resource_type TEXT CHECK(resource_type IN ('lesson', 'example', 'reference', 'exercise')) NOT NULL,
    resource_id TEXT NOT NULL,
    title TEXT,
    note TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(user_id)
);

-- Learning path progress
CREATE TABLE IF NOT EXISTS learning_path_progress (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    path_id TEXT NOT NULL,  -- 'beginner', 'intermediate', etc.
    lessons_completed INTEGER DEFAULT 0,
    total_lessons INTEGER NOT NULL,
    last_lesson_id TEXT,
    started_at TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(user_id),
    UNIQUE(user_id, path_id)
);

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_lesson_progress_user ON lesson_progress(user_id);
CREATE INDEX IF NOT EXISTS idx_lesson_progress_lesson ON lesson_progress(lesson_id);
CREATE INDEX IF NOT EXISTS idx_lesson_progress_status ON lesson_progress(status);
CREATE INDEX IF NOT EXISTS idx_exercise_attempts_user ON exercise_attempts(user_id);
CREATE INDEX IF NOT EXISTS idx_exercise_attempts_exercise ON exercise_attempts(exercise_id);
CREATE INDEX IF NOT EXISTS idx_study_sessions_user_date ON study_sessions(user_id, session_date);
CREATE INDEX IF NOT EXISTS idx_achievements_user ON achievements(user_id);
CREATE INDEX IF NOT EXISTS idx_bookmarks_user ON bookmarks(user_id);

-- Insert default user (for single-user desktop app)
INSERT OR IGNORE INTO users (user_id, username, email)
VALUES (1, 'Student', 'student@teslacourse.local');
