<?php
session_start();
require_once 'db_connection.php';

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['theme_id' => 1]);
    exit();
}

$userId = $_SESSION['user_id'];

try {
    $query = $pdo->prepare("SELECT theme_id FROM emails_v WHERE id = :userId");
    $query->bindParam(':userId', $userId, PDO::PARAM_INT);
    $query->execute();
    $theme = $query->fetch(PDO::FETCH_ASSOC);
    echo json_encode(['theme_id' => $theme['theme_id'] ?? 1]);
} catch (PDOException $e) {
    error_log("Error fetching user theme: " . $e->getMessage());
    echo json_encode(['theme_id' => 1]);
}
?>
