-- Fase 6: Extras y Diferenciadores

-- Wall of Fame
CREATE TABLE IF NOT EXISTS wall_of_fame_entries (
    id TEXT PRIMARY KEY,
    tournament_id TEXT,
    title TEXT NOT NULL,
    description TEXT,
    winners TEXT NOT NULL, -- JSON array
    category TEXT NOT NULL DEFAULT 'TOURNAMENT',
    image_url TEXT,
    event_date DATETIME NOT NULL,
    is_active BOOLEAN DEFAULT 1,
    featured BOOLEAN DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tournament_id) REFERENCES tournaments(id) ON DELETE SET NULL
);

-- Achievements (Logros)
CREATE TABLE IF NOT EXISTS achievements (
    id TEXT PRIMARY KEY,
    code TEXT UNIQUE NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    category TEXT NOT NULL DEFAULT 'GAMES',
    icon TEXT,
    color TEXT DEFAULT '#16a34a',
    requirement_type TEXT NOT NULL,
    requirement_value INTEGER NOT NULL,
    points_reward INTEGER DEFAULT 0,
    is_active BOOLEAN DEFAULT 1
);

-- User Achievements
CREATE TABLE IF NOT EXISTS user_achievements (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL,
    achievement_id TEXT NOT NULL,
    unlocked_at DATETIME,
    progress INTEGER DEFAULT 0,
    is_completed BOOLEAN DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (achievement_id) REFERENCES achievements(id) ON DELETE CASCADE,
    UNIQUE(user_id, achievement_id)
);

-- Challenges (Retos)
CREATE TABLE IF NOT EXISTS challenges (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    type TEXT NOT NULL DEFAULT 'WEEKLY',
    requirement_type TEXT NOT NULL,
    requirement_value INTEGER NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    reward_points INTEGER DEFAULT 0,
    participants TEXT DEFAULT '[]', -- JSON array
    winners TEXT DEFAULT '[]', -- JSON array
    is_active BOOLEAN DEFAULT 1
);

-- User Challenges
CREATE TABLE IF NOT EXISTS user_challenges (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL,
    challenge_id TEXT NOT NULL,
    progress INTEGER DEFAULT 0,
    is_completed BOOLEAN DEFAULT 0,
    completed_at DATETIME,
    reward_claimed BOOLEAN DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (challenge_id) REFERENCES challenges(id) ON DELETE CASCADE,
    UNIQUE(user_id, challenge_id)
);

-- QR Codes
CREATE TABLE IF NOT EXISTS qr_codes (
    id TEXT PRIMARY KEY,
    code TEXT UNIQUE NOT NULL,
    type TEXT NOT NULL DEFAULT 'BOOKING_CHECKIN',
    reference_id TEXT NOT NULL,
    expires_at DATETIME NOT NULL,
    used_at DATETIME,
    used_by TEXT,
    is_active BOOLEAN DEFAULT 1,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (used_by) REFERENCES users(id) ON DELETE SET NULL
);

-- Check-ins
CREATE TABLE IF NOT EXISTS check_ins (
    id TEXT PRIMARY KEY,
    booking_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    qr_code_id TEXT,
    check_in_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    check_out_time DATETIME,
    method TEXT DEFAULT 'QR',
    verified_by TEXT,
    notes TEXT,
    FOREIGN KEY (booking_id) REFERENCES bookings(id) ON DELETE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (qr_code_id) REFERENCES qr_codes(id) ON DELETE SET NULL
);

-- Equipment Items
CREATE TABLE IF NOT EXISTS equipment_items (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT,
    category TEXT NOT NULL DEFAULT 'RACKET',
    brand TEXT,
    model TEXT,
    size TEXT,
    condition TEXT DEFAULT 'GOOD',
    hourly_rate INTEGER,
    daily_rate INTEGER,
    deposit_required INTEGER DEFAULT 0,
    quantity_total INTEGER DEFAULT 1,
    quantity_available INTEGER DEFAULT 1,
    image_url TEXT,
    is_active BOOLEAN DEFAULT 1
);

-- Equipment Rentals
CREATE TABLE IF NOT EXISTS equipment_rentals (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL,
    items TEXT NOT NULL, -- JSON array
    booking_id TEXT,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    total_cost INTEGER NOT NULL,
    deposit_amount INTEGER DEFAULT 0,
    status TEXT DEFAULT 'RESERVED',
    picked_up_at DATETIME,
    returned_at DATETIME,
    payment_id TEXT,
    notes TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (booking_id) REFERENCES bookings(id) ON DELETE SET NULL
);

-- Menu Items
CREATE TABLE IF NOT EXISTS menu_items (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT,
    category TEXT NOT NULL DEFAULT 'DRINK',
    price INTEGER NOT NULL,
    image_url TEXT,
    is_available BOOLEAN DEFAULT 1,
    preparation_time INTEGER,
    is_active BOOLEAN DEFAULT 1
);

-- Orders
CREATE TABLE IF NOT EXISTS orders (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL,
    booking_id TEXT NOT NULL,
    court_id TEXT NOT NULL,
    items TEXT NOT NULL, -- JSON array
    status TEXT DEFAULT 'PENDING',
    total_amount INTEGER NOT NULL,
    payment_status TEXT DEFAULT 'PENDING',
    payment_id TEXT,
    notes TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (booking_id) REFERENCES bookings(id) ON DELETE CASCADE,
    FOREIGN KEY (court_id) REFERENCES courts(id) ON DELETE CASCADE
);

-- Notifications
CREATE TABLE IF NOT EXISTS notifications (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL,
    type TEXT NOT NULL,
    title TEXT NOT NULL,
    message TEXT NOT NULL,
    data TEXT, -- JSON
    is_read BOOLEAN DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- User Activity (Wearables)
CREATE TABLE IF NOT EXISTS user_activities (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL,
    source TEXT DEFAULT 'MANUAL',
    activity_type TEXT DEFAULT 'PADEL_GAME',
    start_time DATETIME NOT NULL,
    end_time DATETIME NOT NULL,
    duration INTEGER NOT NULL,
    calories_burned INTEGER,
    heart_rate_avg INTEGER,
    heart_rate_max INTEGER,
    steps INTEGER,
    distance REAL,
    metadata TEXT, -- JSON
    booking_id TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (booking_id) REFERENCES bookings(id) ON DELETE SET NULL
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_wof_event_date ON wall_of_fame_entries(event_date);
CREATE INDEX IF NOT EXISTS idx_wof_featured ON wall_of_fame_entries(featured);
CREATE INDEX IF NOT EXISTS idx_achievements_category ON achievements(category);
CREATE INDEX IF NOT EXISTS idx_user_achievements_user ON user_achievements(user_id);
CREATE INDEX IF NOT EXISTS idx_challenges_dates ON challenges(start_date, end_date);
CREATE INDEX IF NOT EXISTS idx_qr_codes ON qr_codes(code);
CREATE INDEX IF NOT EXISTS idx_check_ins_booking ON check_ins(booking_id);
CREATE INDEX IF NOT EXISTS idx_check_ins_time ON check_ins(check_in_time);
CREATE INDEX IF NOT EXISTS idx_equipment_category ON equipment_items(category);
CREATE INDEX IF NOT EXISTS idx_rentals_user ON equipment_rentals(user_id);
CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status);
CREATE INDEX IF NOT EXISTS idx_notifications_user ON notifications(user_id);
CREATE INDEX IF NOT EXISTS idx_activities_user ON user_activities(user_id);
