<?php

declare(strict_types=1);

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

HyroAdminAuth::requireLogin();

$database = HyroDatabase::connection();

$currentAdminPage = 'leads';

$leadId = filter_input(
    INPUT_GET,
    'id',
    FILTER_VALIDATE_INT
);

if (!$leadId || $leadId < 1) {
    http_response_code(404);

    exit('Lead not found.');
}

$statement = $database->prepare(
    '
    SELECT
        id,
        lead_code,
        request_type,
        product_code,
        company_name,
        contact_name,
        email,
        phone,
        country_code,
        country_name,
        city,
        preferred_date,
        preferred_time,
        timezone,
        message,
        source_page,
        source_campaign,
        status,
        assigned_to,
        ip_address,
        user_agent,
        created_at,
        updated_at
    FROM hyro_leads
    WHERE id = :id
    LIMIT 1
    '
);

$statement->execute([
    'id' => $leadId,
]);

$lead = $statement->fetch();

if (!is_array($lead)) {
    http_response_code(404);

    exit('Lead not found.');
}

$activityRepository =
    new HyroLeadActivityRepository($database);

$activities =
    $activityRepository->allForLead($leadId);

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

$statusLabel = ucwords(
    str_replace('_', ' ', $status)
);

$productNames = [
    'SPA' => 'HYRO ONE SPA',
    'GYM' => 'HYRO ONE Gym',
    'ACCOUNTING' => 'HYRO ONE Accounting',
    'RETAIL' => 'HYRO ONE Retail',
    'CRM' => 'HYRO ONE CRM',
    'SCHOOL' => 'HYRO ONE School',
];

$productName =
    $productNames[$lead['product_code']]
    ?? $lead['product_code'];

?>
<!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>
        <?= hyro_escape($lead['lead_code']) ?>
        | 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__)
        . '/components/admin-header.php';
    ?>

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

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

        <section class="hyro-lead-profile-header">

            <div class="hyro-lead-profile-heading">

                <div class="hyro-lead-profile-icon">
                    <?= hyro_escape(
                        strtoupper(
                            substr(
                                (string) $lead['company_name'],
                                0,
                                1
                            )
                        )
                    ) ?>
                </div>

                <div>

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

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

                        ·

                        <?= hyro_escape($productName) ?>
                    </p>

                </div>

            </div>

            <div class="hyro-lead-profile-actions">

                <a
                    href="/experience/admin/leads/index.php"
                    class="hyro-admin-small-button"
                >
                    ← Back to Leads
                </a>

                <a
                    href="/experience/admin/leads/edit.php?id=<?= (int) $lead['id'] ?>"
                    class="hyro-admin-small-button primary"
                >
                    Edit Lead
                </a>

            </div>

        </section>

        <div class="hyro-lead-profile-grid">

            <div class="hyro-lead-profile-main">

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Company & Contact Information</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <div class="hyro-lead-detail-grid">

                            <div class="hyro-lead-detail-item">

                                <span>Company Name</span>

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

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Contact Person</span>

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

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Email Address</span>

                                <a href="mailto:<?= hyro_escape(
                                    $lead['email']
                                ) ?>">
                                    <?= hyro_escape(
                                        $lead['email']
                                    ) ?>
                                </a>

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Phone / WhatsApp</span>

                                <a href="tel:<?= hyro_escape(
                                    $lead['phone']
                                ) ?>">
                                    <?= hyro_escape(
                                        $lead['phone']
                                    ) ?>
                                </a>

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Business Country</span>

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

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>City</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['city'] ?: 'Not provided'
                                    ) ?>
                                </strong>

                            </div>

                        </div>

                    </div>

                </section>

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Demo Request</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <div class="hyro-lead-detail-grid">

                            <div class="hyro-lead-detail-item">

                                <span>Product</span>

                                <strong>
                                    <?= hyro_escape($productName) ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Request Type</span>

                                <strong>
                                    <?= hyro_escape(
                                        ucwords(
                                            str_replace(
                                                '_',
                                                ' ',
                                                $lead['request_type']
                                            )
                                        )
                                    ) ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Preferred Date</span>

                                <strong>
                                    <?= !empty($lead['preferred_date'])
                                        ? hyro_escape(
                                            date(
                                                'l, d F Y',
                                                strtotime(
                                                    $lead[
                                                        'preferred_date'
                                                    ]
                                                )
                                            )
                                        )
                                        : 'Not selected' ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Preferred Time</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['preferred_time']
                                        ?: 'Not selected'
                                    ) ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-detail-item">

                                <span>Timezone</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['timezone'] ?: 'UTC'
                                    ) ?>
                                </strong>

                            </div>

                        </div>

                    </div>

                </section>

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Customer Requirements</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <p class="hyro-lead-message">
                            <?= hyro_escape(
                                $lead['message']
                                ?: 'No additional requirements were provided.'
                            ) ?>
                        </p>

                    </div>

                </section>

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Activity Timeline</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <div class="hyro-lead-timeline">

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

                                <?php foreach (
                                    $activities as $activity
                                ): ?>

                                    <article
                                        class="hyro-lead-timeline-item"
                                    >

                                        <div
                                            class="hyro-lead-timeline-dot"
                                        ></div>

                                        <strong>
                                            <?= hyro_escape(
                                                $activity[
                                                    'activity_title'
                                                ]
                                            ) ?>
                                        </strong>

                                        <?php if (
                                            !empty(
                                                $activity[
                                                    'activity_description'
                                                ]
                                            )
                                        ): ?>

                                            <p>
                                                <?= hyro_escape(
                                                    $activity[
                                                        'activity_description'
                                                    ]
                                                ) ?>
                                            </p>

                                        <?php endif; ?>

                                        <time>
                                            <?= hyro_escape(
                                                date(
                                                    'd M Y, h:i A',
                                                    strtotime(
                                                        $activity[
                                                            'created_at'
                                                        ]
                                                    )
                                                )
                                            ) ?>

                                            <?php if (
                                                !empty(
                                                    $activity[
                                                        'created_by_name'
                                                    ]
                                                )
                                            ): ?>

                                                ·

                                                <?= hyro_escape(
                                                    $activity[
                                                        'created_by_name'
                                                    ]
                                                ) ?>

                                            <?php endif; ?>
                                        </time>

                                    </article>

                                <?php endforeach; ?>

                            <?php endif; ?>

                            <article
                                class="hyro-lead-timeline-item"
                            >

                                <div
                                    class="hyro-lead-timeline-dot"
                                ></div>

                                <strong>Lead Created</strong>

                                <p>
                                    The customer submitted a
                                    HYRO ONE demo request.
                                </p>

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

                            </article>

                        </div>

                    </div>

                </section>

            </div>

            <aside class="hyro-lead-profile-side">

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Lead Status</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <div class="hyro-lead-status-large">

                            <span
                                class="hyro-lead-status <?= hyro_escape(
                                    $status
                                ) ?>"
                            >
                                <?= hyro_escape($statusLabel) ?>
                            </span>

                        </div>

                    </div>

                </section>

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Ownership</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <div class="hyro-lead-side-list">

                            <div class="hyro-lead-side-row">

                                <span>Assigned To</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['assigned_to']
                                        ?: 'Not assigned'
                                    ) ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-side-row">

                                <span>Created</span>

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

                            </div>

                            <div class="hyro-lead-side-row">

                                <span>Last Updated</span>

                                <strong>
                                    <?= hyro_escape(
                                        date(
                                            'd M Y, h:i A',
                                            strtotime(
                                                $lead['updated_at']
                                            )
                                        )
                                    ) ?>
                                </strong>

                            </div>

                        </div>

                    </div>

                </section>

                <section class="hyro-lead-info-card">

                    <div class="hyro-lead-info-card-header">

                        <h2>Lead Source</h2>

                    </div>

                    <div class="hyro-lead-info-card-body">

                        <div class="hyro-lead-side-list">

                            <div class="hyro-lead-side-row">

                                <span>Source Page</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['source_page']
                                        ?: 'Direct'
                                    ) ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-side-row">

                                <span>Campaign</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['source_campaign']
                                        ?: 'Not specified'
                                    ) ?>
                                </strong>

                            </div>

                            <div class="hyro-lead-side-row">

                                <span>IP Address</span>

                                <strong>
                                    <?= hyro_escape(
                                        $lead['ip_address']
                                        ?: 'Unavailable'
                                    ) ?>
                                </strong>

                            </div>

                        </div>

                    </div>

                </section>

            </aside>

        </div>

    </main>

</div>

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

</body>
</html>