<?php
// File 4/6
// Path: api/sales/today_summary.php
// Table: sales
// column: sale_date (datetime), grand_total, due_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(grand_total), 0)    AS total_amount,
            COALESCE(SUM(due_amount),  0)    AS total_due
        FROM sales
        WHERE DATE(sale_date) = ?
    ");
    $stmt->execute([$date]);
    $row = $stmt->fetch();

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

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['ok' => false, 'msg' => 'Server error']);
}
