<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/core/bootstrap.php';

HyroAdminAuth::requireLogin();

$database = HyroDatabase::connection();

$currentAdminPage = 'dashboard';

$summary = [
    'total' => 0,
    'new' => 0,
    'demo_scheduled' => 0,
    'trial_started' => 0,
    'customer' => 0,
];

$summaryStatement = $database->query(
    "
    SELECT
        COUNT(*) AS total,
        SUM(status = 'new') AS new_count,
        SUM(status = 'demo_scheduled') AS demo_scheduled_count,
        SUM(status = 'trial_started') AS trial_started_count,
        SUM(status = 'customer') AS customer_count
    FROM hyro_leads
    "
);

$summaryRow = $summaryStatement->fetch();

if (is_array($summaryRow)) {
    $summary = [
        'total' => (int) ($summaryRow['total'] ?? 0),
        'new' => (int) ($summaryRow['new_count'] ?? 0),
        'demo_scheduled' => (int) (
            $summaryRow['demo_scheduled_count'] ?? 0
        ),
        'trial_started' => (int) (
            $summaryRow['trial_started_count'] ?? 0
        ),
        'customer' => (int) (
            $summaryRow['customer_count'] ?? 0
        ),
    ];
}

$latestStatement = $database->query(
    "
    SELECT
        id,
        lead_code,
        product_code,
        company_name,
        contact_name,
        email,
        country_name,
        status,
        preferred_date,
        preferred_time,
        created_at
    FROM hyro_leads
    ORDER BY id DESC
    LIMIT 10
    "
);

$latestLeads = $latestStatement->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <meta
        name="robots"
        content="noindex, nofollow, noarchive"
    >

    <title>Dashboard | HYRO ONE Admin</title>

    <link
        rel="icon"
        href="/experience/assets/icons/favicon.svg"
        type="image/svg+xml"
    >

    <link
        rel="stylesheet"
        href="/experience/assets/css/site.css?v=<?= time() ?>"
    >

    <link
        rel="stylesheet"
        href="/experience/assets/css/admin.css?v=<?= time() ?>"
    >

</head>

<body class="hyro-admin-body">

<div class="hyro-admin-shell">

    <?php
    require dirname(__DIR__)
        . '/admin/components/admin-header.php';
    ?>

    <?php
    require dirname(__DIR__)
        . '/admin/components/admin-sidebar.php';
    ?>

    <main class="hyro-admin-main">

        <div class="hyro-admin-page-heading">

            <div>

                <h1>Dashboard</h1>

                <p>
                    Monitor incoming enquiries and sales activity.
                </p>

            </div>

            <div class="hyro-admin-date">
                <?= hyro_escape(
                    date('l, d F Y')
                ) ?>
            </div>

        </div>

        <section class="hyro-admin-stat-grid">

            <article class="hyro-admin-stat-card">

                <div class="hyro-admin-stat-card-top">

                    <span>Total Leads</span>

                    <div class="hyro-admin-stat-icon">
                        ◎
                    </div>

                </div>

                <strong>
                    <?= number_format($summary['total']) ?>
                </strong>

            </article>

            <article class="hyro-admin-stat-card">

                <div class="hyro-admin-stat-card-top">

                    <span>New Leads</span>

                    <div class="hyro-admin-stat-icon">
                        ✦
                    </div>

                </div>

                <strong>
                    <?= number_format($summary['new']) ?>
                </strong>

            </article>

            <article class="hyro-admin-stat-card">

                <div class="hyro-admin-stat-card-top">

                    <span>Demo Scheduled</span>

                    <div class="hyro-admin-stat-icon">
                        ◷
                    </div>

                </div>

                <strong>
                    <?= number_format(
                        $summary['demo_scheduled']
                    ) ?>
                </strong>

            </article>

            <article class="hyro-admin-stat-card">

                <div class="hyro-admin-stat-card-top">

                    <span>Trial Started</span>

                    <div class="hyro-admin-stat-icon">
                        ⚡
                    </div>

                </div>

                <strong>
                    <?= number_format(
                        $summary['trial_started']
                    ) ?>
                </strong>

            </article>

            <article class="hyro-admin-stat-card">

                <div class="hyro-admin-stat-card-top">

                    <span>Customers</span>

                    <div class="hyro-admin-stat-icon">
                        ✓
                    </div>

                </div>

                <strong>
                    <?= number_format($summary['customer']) ?>
                </strong>

            </article>

        </section>

        <section class="hyro-admin-panel">

            <div class="hyro-admin-panel-header">

                <h2>Latest Leads</h2>

                <a href="/experience/admin/leads/index.php">
                    View All Leads →
                </a>

            </div>

            <?php if ($latestLeads === []): ?>

                <div class="hyro-admin-empty">
                    No lead requests are available yet.
                </div>

            <?php else: ?>

                <div class="hyro-admin-table-wrap">

                    <table class="hyro-admin-table">

                        <thead>

                            <tr>
                                <th>Lead</th>
                                <th>Company</th>
                                <th>Product</th>
                                <th>Country</th>
                                <th>Preferred Demo</th>
                                <th>Status</th>
                                <th>Created</th>
                            </tr>

                        </thead>

                        <tbody>

                        <?php foreach ($latestLeads as $lead): ?>

                            <?php
                            $status = strtolower(
                                (string) ($lead['status'] ?? 'new')
                            );
                            ?>

                            <tr>

                                <td>

                                    <strong>
                                        <?= hyro_escape(
                                            $lead['lead_code']
                                        ) ?>
                                    </strong>

                                    <small>
                                        <?= hyro_escape(
                                            $lead['email']
                                        ) ?>
                                    </small>

                                </td>

                                <td>

                                    <strong>
                                        <?= hyro_escape(
                                            $lead['company_name']
                                        ) ?>
                                    </strong>

                                    <small>
                                        <?= hyro_escape(
                                            $lead['contact_name']
                                        ) ?>
                                    </small>

                                </td>

                                <td>
                                    <?= hyro_escape(
                                        $lead['product_code']
                                    ) ?>
                                </td>

                                <td>
                                    <?= hyro_escape(
                                        $lead['country_name']
                                    ) ?>
                                </td>

                                <td>

                                    <?php if (
                                        !empty($lead['preferred_date'])
                                    ): ?>

                                        <?= hyro_escape(
                                            date(
                                                'd M Y',
                                                strtotime(
                                                    $lead[
                                                        'preferred_date'
                                                    ]
                                                )
                                            )
                                        ) ?>

                                        <small>
                                            <?= hyro_escape(
                                                $lead[
                                                    'preferred_time'
                                                ] ?? ''
                                            ) ?>
                                        </small>

                                    <?php else: ?>

                                        —

                                    <?php endif; ?>

                                </td>

                                <td>

                                    <span
                                        class="hyro-lead-status <?= hyro_escape(
                                            $status
                                        ) ?>"
                                    >
                                        <?= hyro_escape(
                                            str_replace(
                                                '_',
                                                ' ',
                                                $status
                                            )
                                        ) ?>
                                    </span>

                                </td>

                                <td>
                                    <?= hyro_escape(
                                        date(
                                            'd M Y H:i',
                                            strtotime(
                                                $lead['created_at']
                                            )
                                        )
                                    ) ?>
                                </td>

                            </tr>

                        <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

            <?php endif; ?>

        </section>

    </main>

</div>

<script
    src="/experience/assets/js/admin.js?v=<?= time() ?>"
></script>

</body>
</html>