<?php
// File 5/6
// Path: api/purchases/today_summary.php
// Table: purchases
// column: purchase_date, total_amount

header('Content-Type: application/json');
require_once __DIR__ . '/../../config/db.php';
require_once __DIR__ . '/../auth/check_session.php';

$date = $_GET['date'] ?? date('Y-m-d');

try {
    $stmt = $pdo->prepare("
        SELECT
            COUNT(*)                          AS count,
            COALESCE(SUM(total_amount), 0)    AS total_amount
        FROM purchases
        WHERE purchase_date = ?
          AND deleted_at IS NULL
    ");
    $stmt->execute([$date]);
    $row = $stmt->fetch();

    echo json_encode([
        'ok'   => true,
        'data' => [
            'count'        => (int)   $row['count'],
            'total_amount' => (float) $row['total_amount'],
        ]
    ]);

} catch (PDOException $e) {
    // table সমস্যা হলেও dashboard ভাঙবে না
    echo json_encode([
        'ok'   => true,
        'data' => ['count' => 0, 'total_amount' => 0]
    ]);
}
