<?php
session_start();
require_once __DIR__ . '/../db/config.php';

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'error' => 'غير مصرح لك بالوصول']);
    exit;
}

$data = json_decode(file_get_contents('php://input'), true);
$theme = $data['theme'] ?? 'light';

// Validate theme
$allowed_themes = ['light', 'dark', 'midnight', 'forest', 'ocean', 'sunset', 'royal'];
if (!in_array($theme, $allowed_themes)) {
    echo json_encode(['success' => false, 'error' => 'مظهر غير صالح']);
    exit;
}

try {
    $stmt = db()->prepare("UPDATE users SET theme = ? WHERE id = ?");
    $stmt->execute([$theme, $_SESSION['user_id']]);
    echo json_encode(['success' => true]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}