<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin();

require_once __DIR__ . '/../../config/database.php';

$company = $_SESSION['company_id'] ?? 0;

if(!$company){
    die("Company context missing.");
}

/* ===============================
   TOTAL INVOICES
=================================*/

$stmt = $pdo->prepare("
SELECT COUNT(*)
FROM invoices
WHERE company_id = ?
");
$stmt->execute([$company]);
$totalInvoices = (int)$stmt->fetchColumn();

/* ===============================
   TOTAL PAID
=================================*/

$stmt = $pdo->prepare("
SELECT COALESCE(SUM(p.amount),0)
FROM invoice_payments p
JOIN invoices i ON i.id = p.invoice_id
WHERE i.company_id = ?
");
$stmt->execute([$company]);
$totalPaid = (float)$stmt->fetchColumn();

/* ===============================
   TOTAL INVOICED
=================================*/

$stmt = $pdo->prepare("
SELECT COALESCE(SUM(total),0)
FROM invoices
WHERE company_id = ?
");
$stmt->execute([$company]);
$totalInvoiced = (float)$stmt->fetchColumn();

/* ===============================
   OUTSTANDING
=================================*/

$totalOutstanding = max(0, $totalInvoiced - $totalPaid);

/* ===============================
   OVERDUE OUTSTANDING
=================================*/

$stmt = $pdo->prepare("
SELECT
COALESCE(SUM(i.total - COALESCE(p.paid,0)),0)
FROM invoices i
LEFT JOIN (
    SELECT invoice_id, SUM(amount) AS paid
    FROM invoice_payments
    GROUP BY invoice_id
) p ON p.invoice_id = i.id
WHERE i.company_id = ?
AND i.status != 'PAID'
AND i.due_date < CURDATE()
AND (i.total - COALESCE(p.paid,0)) > 0
");

$stmt->execute([$company]);
$totalOverdue = (float)$stmt->fetchColumn();

require_once __DIR__ . '/../../includes/header.php';
require_once __DIR__ . '/../../includes/sidebar.php';
?>

<div class="main-content">

<div class="breadcrumb" style="margin-bottom:15px;">
<a href="/core/public/dashboard.php">Dashboard</a> /
<span>Billing Dashboard</span>
</div>

<div class="page-header" style="display:flex;justify-content:space-between;align-items:flex-end;gap:12px;flex-wrap:wrap;">

<div>
<h2>Billing Dashboard</h2>
<div class="page-subtitle">Tenant financial overview</div>
</div>

<div style="display:flex;gap:10px;flex-wrap:wrap;">
<a class="btn btn-primary" href="invoices/invoices.php">View Invoices</a>
<a class="btn" href="billing_runs/index.php">New Calculation</a>
</div>

</div>

<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;">

<div class="stat-card">
<div class="page-subtitle">Total Invoices</div>
<div class="number"><?= number_format($totalInvoices) ?></div>
</div>

<div class="stat-card">
<div class="page-subtitle">Total Paid</div>
<div class="number" style="color:#16a34a;">
R <?= number_format($totalPaid,2) ?>
</div>
</div>

<div class="stat-card">
<div class="page-subtitle">Outstanding</div>
<div class="number" style="color:#f59e0b;">
R <?= number_format($totalOutstanding,2) ?>
</div>
</div>

<div class="stat-card">
<div class="page-subtitle">Overdue</div>
<div class="number" style="color:#dc2626;">
R <?= number_format($totalOverdue,2) ?>
</div>
</div>

</div>

</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>