<?php

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../app/customers.php';

$user = sparrow_require_backyard_login();

$customers = sparrow_get_customers_summary();

sparrow_backyard_header('Customers');
?>

<?php if (($_GET['notice'] ?? '') === 'customer_merged'): ?>
    <div class="notice-success">
        Customer histories merged.
    </div>
<?php endif; ?>

<section class="card">
    <h1>Customers</h1>

    <p class="muted">
        Customer history is grouped from existing Backyard orders. This is the first simple version before adding saved customer preferences.
    </p>
</section>

<section class="card">
    <h2>Customer List</h2>

    <?php if (!$customers): ?>
        <p class="muted">No customers found yet.</p>
    <?php else: ?>
        <div class="table-wrap">
            <table class="customers-table">
                <thead>
                    <tr>
                        <th>Customer</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>Orders</th>
                        <th>Active</th>
                        <th>Delivered</th>
                        <th>Total Ordered</th>
                        <th>Paid</th>
                        <th>Balance</th>
                        <th>Blessings</th>
                        <th>Last Order</th>
                        <th>History</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($customers as $customer): ?>
                        <tr>
                            <td>
                                <strong>
                                    <?= sparrow_escape($customer['customer_name'] ?: 'Unknown Customer') ?>
                                </strong>
                            </td>

                            <td>
                                <?= sparrow_escape(sparrow_format_phone_number($customer['customer_phone'] ?? '')) ?>
                            </td>

                            <td>
                                <?= sparrow_escape($customer['customer_email'] ?? '') ?>
                            </td>

                            <td>
                                <?= number_format((int) $customer['order_count']) ?>
                            </td>

                            <td>
                                <?= number_format((int) $customer['active_orders']) ?>
                            </td>

                            <td>
                                <?= number_format((int) $customer['delivered_orders']) ?>
                            </td>

                            <td>
                                $<?= number_format((float) $customer['total_ordered'], 2) ?>
                            </td>

                            <td>
                                $<?= number_format((float) $customer['total_paid'], 2) ?>
                            </td>

                            <td>
                                $<?= number_format((float) $customer['total_balance'], 2) ?>
                            </td>

                            <td>
                                $<?= number_format((float) $customer['total_blessings'], 2) ?>
                            </td>

                            <td>
                                <?= sparrow_escape($customer['last_order_date']) ?>
                            </td>

                            <td>
                                <a
                                    class="button button-small"
                                    href="<?= sparrow_escape(sparrow_url('/backyard/customer-view.php?key=' . urlencode($customer['customer_key']))) ?>"
                                >
                                    View
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>

<?php sparrow_backyard_footer(); ?>