<?php
require_once __DIR__ . '/../ai/LocalAIApi.php';

header('Content-Type: application/json');

$response = [
    'message' => 'Sorry, I am having trouble understanding you right now.'
];

if (isset($_POST['message'])) {
    $userMessage = trim($_POST['message']);

    $ai_response = LocalAIApi::createResponse([
        'input' => [
            ['role' => 'system', 'content' => 'You are a helpful assistant for a winter resort website. Your name is WinterBot. You are friendly and helpful. You should help users plan their winter vacation.'],
            ['role' => 'user', 'content' => $userMessage],
        ],
    ]);

    if (!empty($ai_response['success']) && !empty($ai_response['data'])) {
        $text = LocalAIApi::extractText($ai_response);
        if ($text !== '') {
            $response['message'] = $text;
        }
    }
}

echo json_encode($response);
