<?php
/**
 * Ledger Controller — Account ledger with running balance
 */

class LedgerController {
    private PDO $db;
    private object $user;

    public function __construct(PDO $db, object $user) {
        $this->db = $db;
        $this->user = $user;
    }

    /** GET /ledger/{account_id}?from_date=2026-03-01&to_date=2026-03-31 */
    public function show(int $accountId, array $query): void {
        $fromDate = $query['from_date'] ?? date('Y-m-01');
        $toDate = $query['to_date'] ?? date('Y-m-d');
        $page = max(1, (int)($query['page'] ?? 1));
        $limit = min(10000, max(10, (int)($query['limit'] ?? 5000)));

        try {
            $result = AccountingService::getLedger($this->db, $accountId, $fromDate, $toDate, $page, $limit, $this->user);
            Response::success($result);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /** GET /ledger/{account_id}/balance */
    public function balance(int $accountId): void {
        try {
            $balance = AccountingService::getBalance($this->db, $accountId, $this->user);
            Response::success(['balance' => $balance]);
        } catch (Exception $e) {
            throw $e;
        }
    }
}
