-- Mobile App Schema Update
-- Version: 1.0

-- 1. Mobile Triage Results (The "Game" Scoreboard)
CREATE TABLE IF NOT EXISTS mobile_triage_results (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    score INT DEFAULT 0,            -- Points earned in this session
    correct_count INT DEFAULT 0,    -- Number of correct swipes
    total_cards INT DEFAULT 10,     -- Total cards played in session
    duration_seconds INT,           -- Speed bonus calculation
    played_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- 2. Device Tokens (For Push Notifications)
CREATE TABLE IF NOT EXISTS mobile_device_tokens (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    token VARCHAR(255) NOT NULL,    -- ExpoPushToken[...]
    platform ENUM('ios', 'android', 'web') DEFAULT 'ios',
    device_name VARCHAR(100),       -- e.g. "iPhone 15 Pro"
    is_active TINYINT(1) DEFAULT 1,
    last_used_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    UNIQUE KEY unique_user_token (user_id, token)
);

-- 3. Mobile Settings (Preferences)
CREATE TABLE IF NOT EXISTS mobile_user_settings (
    user_id INT PRIMARY KEY,
    theme_preference ENUM('system', 'light', 'dark') DEFAULT 'system',
    enable_haptics BOOLEAN DEFAULT TRUE,
    daily_reminder_time TIME DEFAULT '18:00:00',
    notify_on_new_mission BOOLEAN DEFAULT TRUE,
    notify_on_streak_risk BOOLEAN DEFAULT TRUE,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- 4. Analytics / Adoption Tracking
ALTER TABLE users ADD COLUMN mobile_last_login DATETIME NULL;
