<?php
header('Content-Type: application/json');
session_start();
require_once __DIR__ . '/../db/config.php';

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'error' => 'Unauthorized']);
    exit;
}

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

$allowed_personalities = ['funny', 'serious', 'expert'];
if (!in_array($personality, $allowed_personalities)) {
    $personality = 'funny';
}

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