<?php
/**
 * Chatbot History
 * GET /v1/chatbot/history
 * Returns user's recent chatbot conversations.
 */

use App\Core\Database;
use App\Middleware\AuthMiddleware;

$decoded = AuthMiddleware::check();
$db = Database::getInstance();

$pagination = paginate_params(20, 50);

$countStmt = $db->prepare("SELECT COUNT(*) FROM chatbot_history WHERE user_id = ?");
$countStmt->execute([$decoded['user_id']]);
$total = (int)$countStmt->fetchColumn();

$stmt = $db->prepare("
    SELECT id, question, answer, created_at 
    FROM chatbot_history 
    WHERE user_id = ?
    ORDER BY created_at DESC
    LIMIT {$pagination['limit']} OFFSET {$pagination['offset']}
");
$stmt->execute([$decoded['user_id']]);

json_paginated($stmt->fetchAll(), $total, $pagination);
