<?php
/**
 * MJPOS -> MJ ERP Hub: Cash & Bank Accounts Endpoint
 * Drop at: api/erp/accounts-summary.php inside MJPOS project root
 *
 * GET /api/erp/accounts-summary.php
 * Returns a full CURRENT snapshot of all active accounts (not date-filtered -
 * account balances are a point-in-time state, not a historical series).
 *
 * Response:
 * { "status": "success", "data": [
 *     { "id": 1, "account_name": "Cash Drawer", "account_type": "cash", "bank_name": null,
 *       "account_number": null, "branch": null, "current_balance": 21075.00, "status": "active" }
 * ]}
 */

require_once __DIR__ . '/erp_auth.php';
require_once __DIR__ . '/../../mjpos/config/config.php';

erp_require_auth();

try {
    // Confirmed against MJPOS schema: `finance_accounts` table
    $stmt = erp_pdo()->query(
        "SELECT id, account_name, account_type, bank_name, account_number, branch, current_balance, status
         FROM finance_accounts
         WHERE status = 'active'
         ORDER BY account_type, account_name"
    );
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    erp_json_success($rows);

} catch (Exception $e) {
    erp_json_error('Query failed: ' . $e->getMessage(), 500);
}
