<?php
session_start();
require_once dirname(__DIR__) . '/includes/functions.php';

billing_require_auth();

$user = billing_current_user();
$organization = billing_current_organization();

if (!$user) {
    billing_redirect('signin.php');
}

if (!$organization || empty($organization['onboarding_completed_at'])) {
    billing_redirect('onboarding.php?step=1');
}

$stats = billing_dashboard_stats((int) $user['id']);
$customers = billing_get_customers((int) $user['id']);
$invoices = billing_fetch_all(
    'SELECT invoice_number, customer_name, total_amount, status_label
     FROM invoices
     WHERE user_id = ? AND COALESCE(is_deleted, 0) = 0
     ORDER BY id DESC LIMIT 5',
    'i',
    [(int) $user['id']]
);

$billingActiveNav = 'home';
$billingTopbarOrganizationName = (string) ($organization['organization_name'] ?? 'Billing');
$billingTopbarUserLabel = (string) ($user['email'] ?? '');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title>Billing Dashboard</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;700;800&display=swap">
<link rel="stylesheet" href="Payslip_Project/assets/fontawesome/css/all.min.css">
<link rel="stylesheet" href="assets/billing.css">
</head>
<body>
<div class="dashboard-shell">
    <?php include dirname(__DIR__) . '/includes/app_sidebar.php'; ?>
    <div class="dashboard-main">
        <?php include dirname(__DIR__) . '/includes/topbar.php'; ?>
        <main class="dashboard-content">
            <section class="hello-card">
                <div class="hello-card-top">
                    <div class="hello-user">
                        <div class="hello-avatar"><i class="fa-regular fa-id-badge"></i></div>
                        <div class="hello-copy">
                            <h1>Hello, <?php echo billing_escape((string) ($user['business_name'] ?? $user['email'] ?? 'User')); ?></h1>
                            <p><?php echo billing_escape((string) ($organization['organization_name'] ?? 'Your organization')); ?></p>
                        </div>
                    </div>
                    <div class="hello-support">
                        <strong>Billing Overview</strong>
                        <span>Restored dashboard route with working request URLs.</span>
                    </div>
                </div>
                <nav class="hello-links">
                    <a class="is-active" href="dashboard.php?tab=dashboard">Dashboard</a>
                    <a href="customers.php">Customers</a>
                    <a href="reports.php">Reports</a>
                </nav>
            </section>

            <section class="summary-panels">
                <article class="summary-card">
                    <span>Customers</span>
                    <strong><?php echo (int) ($stats['customers'] ?? 0); ?></strong>
                    <small>Active records in your account</small>
                </article>
                <article class="summary-card">
                    <span>Items</span>
                    <strong><?php echo (int) ($stats['items'] ?? 0); ?></strong>
                    <small>Catalog entries ready for billing</small>
                </article>
                <article class="summary-card">
                    <span>Invoices</span>
                    <strong><?php echo (int) ($stats['invoices'] ?? 0); ?></strong>
                    <small>Total invoices created</small>
                </article>
                <article class="summary-card">
                    <span>Receivables</span>
                    <strong>&#8377;<?php echo billing_escape(number_format((float) ($stats['receivable'] ?? 0), 2)); ?></strong>
                    <small>Outstanding invoice amount</small>
                </article>
            </section>

            <section class="content-grid">
                <article class="panel">
                    <div class="panel-header">
                        <h2>Recent Customers</h2>
                        <p class="panel-subtitle">Latest customer records with quick access to customer pages.</p>
                    </div>
                    <div class="checklist-wrap">
                        <div class="checklist-list">
                            <?php if ($customers === []): ?>
                                <div class="checklist-item">No customers yet. <a href="customer_new.php">Create your first customer</a>.</div>
                            <?php else: ?>
                                <?php foreach (array_slice($customers, 0, 5) as $customer): ?>
                                    <div class="task-row">
                                        <span><?php echo billing_escape((string) ($customer['customer_name'] ?? '')); ?></span>
                                        <a href="customer_new.php?id=<?php echo (int) ($customer['id'] ?? 0); ?>">Open</a>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </article>

                <article class="panel">
                    <div class="panel-header">
                        <h2>Recent Invoices</h2>
                        <p class="panel-subtitle">Quick view of latest invoice activity.</p>
                    </div>
                    <div class="checklist-wrap">
                        <div class="checklist-list">
                            <?php if ($invoices === []): ?>
                                <div class="checklist-item">No invoices yet. <a href="invoice_new.php">Create invoice</a>.</div>
                            <?php else: ?>
                                <?php foreach ($invoices as $invoice): ?>
                                    <div class="task-row">
                                        <span><?php echo billing_escape((string) ($invoice['invoice_number'] ?? '')); ?> - <?php echo billing_escape((string) ($invoice['customer_name'] ?? '')); ?></span>
                                        <a href="invoices.php"><?php echo billing_escape((string) ($invoice['status_label'] ?? 'View')); ?></a>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </article>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>
