<?php
require_once __DIR__ . '/backend/config.php';
require_once __DIR__ . '/backend/Structs.php';
require_once __DIR__ . '/backend/CoinhubDB.php';
require_once __DIR__ . '/backend/AuthManager.php';
require_once __DIR__ . '/backend/shared.php';

$coinhubDB = new CoinhubDB($pdo);
$auth = new AuthManager($coinhubDB);

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TheCoinHub.com - Pioneer Gold Coin Reference</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <style>
        /* --- Google Fonts --- */
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Poppins:wght@600;700&display=swap');

        /* --- NEW: "Digital Archive" Theme --- */
        :root {
            --primary-color: #1d2d35;
            /* Dark Charcoal */
            --accent-color: #ffc107;
            /* Modern Amber */
            --background-color: #f9f9f9;
            /* Clean Off-White */
            --text-color: #333333;
            /* Dark Slate Gray */
            --card-bg-color: #ffffff;
            /* Pure White */
        }

        /* --- General Styling --- */
        html {
            height: 100%;
        }

        body {
            background-color: var(--background-color);
            color: var(--text-color);
            font-family: 'Inter', sans-serif;
            display: flex;
            flex-direction: column;
            min-height: 100%;
        }

        main {
            flex-grow: 1;
        }

        h1,
        h2,
        h3,
        h4,
        h5,
        h6,
        .navbar-brand {
            font-family: 'Poppins', sans-serif;
            color: var(--primary-color);
        }

        a {
            color: var(--accent-color);
            text-decoration: none;
            font-weight: bold;
        }

        a:hover {
            color: #e0a800;
        }

        /* --- Header & Navbar --- */
        .navbar-custom {
            background-color: var(--primary-color);
            padding: 1rem 0;
        }

        .navbar-custom .navbar-brand {
            color: #fff;
            font-size: 1.8rem;
        }

        .navbar-custom .navbar-brand strong {
            color: var(--accent-color);
        }

        .navbar-custom .nav-link {
            color: rgba(255, 255, 255, 0.8);
            font-weight: 600;
        }

        .navbar-custom .nav-link.active,
        .navbar-custom .nav-link:hover {
            color: var(--accent-color);
        }

        /* --- Buttons --- */
        .btn {
            border-radius: 0.25rem;
            font-weight: 700;
            padding: 0.75rem 1.5rem;
            transition: all 0.2s ease-in-out;
        }

        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }

        .btn-primary:hover {
            background-color: #111b20;
            border-color: #111b20;
        }

        .btn-accent {
            background-color: var(--accent-color);
            border-color: var(--accent-color);
            color: var(--primary-color);
        }

        .btn-accent:hover {
            background-color: #e0a800;
            border-color: #e0a800;
            color: var(--primary-color);
        }

        /* --- Hero Section --- */
        .hero-section {
            background-color: var(--primary-color);
            color: #fff;
            padding: 5rem 0;
            text-align: center;
        }

        .hero-section h1 {
            color: #fff;
            font-size: 3.5rem;
            font-weight: 700;
        }

        .hero-section .lead {
            color: var(--accent-color);
            font-size: 1.25rem;
            max-width: 700px;
            margin: 0 auto;
        }

        /* --- Card & Content Styling --- */
        .content-section {
            padding: 3rem 0;
        }

        .card-custom {
            border: 1px solid #e0e0e0;
            background-color: var(--card-bg-color);
            border-top: 4px solid var(--accent-color);
            border-radius: 0.25rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.04);
        }

        .card-custom .card-header {
            background-color: transparent;
            border-bottom: 1px solid #e0e0e0;
            padding: 1.25rem;
            font-family: 'Poppins', sans-serif;
            font-size: 1.25rem;
            font-weight: 600;
        }

        .card-custom .card-footer {
            background-color: transparent;
            border-top: 1px solid #e0e0e0;
            color: #6c757d;
        }

        /* --- Footer --- */
        .footer-custom {
            background-color: var(--primary-color);
            color: rgba(255, 255, 255, 0.7);
            padding: 2rem 0;
        }

        .footer-custom a {
            color: var(--accent-color);
        }

        .footer-custom a:hover {
            color: #fff;
        }
    </style>
</head>

<body>
    <?php echo Shared::PageNavBlock($auth, true, false, false, false, false, false); ?>

    <main>
        <section class="hero-section">
            <div class="container">
                <h1>Your Guide to Pioneer Gold</h1>
                <p class="lead">Explore the rich history of private gold from the American frontier.</p>
            </div>
        </section>

        <section class="content-section">
            <div class="container">
                <div class="row justify-content-center mb-5">
                    <div class="col-md-8">
                        <div class="card shadow-sm">
                            <div class="card-body text-center p-4 p-md-5">
                                <h3 class="mb-3">Look Up an Assessment</h3>
                                <p>Enter your Coin Hub Assessment Number to view your expert analysis.</p>
                                <form action="/lookup/" method="GET" class="d-flex justify-content-center">
                                    <input type="text" name="q" class="form-control form-control-lg w-75 me-2" placeholder="e.g., 183100" required>
                                    <button type="submit" class="btn btn-accent">Lookup <i class="fas fa-search"></i></button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <h2 class="text-center mb-4">Featured Pioneer Coin Series</h2>
                <div class="row">
                    <?php
                    $activeCoins = $coinhubDB->getActiveCoinClasses();
                    shuffle($activeCoins);
                    foreach (array_slice($activeCoins, 0, 3) as $activeCoinClass) {
                        $catInfo = $coinhubDB->getNumisSummaryByItem($activeCoinClass->id, true, false, false);
                        $subCatInfo = $coinhubDB->getActiveCoinSubclassesByParent($activeCoinClass->id);
                        $hasSubCats = (count($subCatInfo) > 0);
                        [$clean_name, $date_range] = preg_match('/^(.*)\s\((\d{4}(?:-\d{4})?)\)$/', $activeCoinClass->name, $m) ? [$m[1], $m[2]] : [$activeCoinClass->name, null];
                    ?>

                        <div class="col-md-4 mb-4">
                            <div class="card card-custom h-100">
                                <div class="card-header"><?php echo $clean_name; ?></div>
                                <div class="card-body d-flex flex-column">
                                    <div class="row mb-3">
                                        <div class="col-6 d-flex justify-content-center">
                                            <?php if (!isset($activeCoinClass->obverse_image_url) || trim($activeCoinClass->obverse_image_url) === '') { ?>
                                                <div class="coin-image-placeholder">No Image Available</div>
                                            <?php
                                            } else {
                                                echo '<img src="/img/uploads/' . $activeCoinClass->obverse_image_url . '" style="max-width:180px;">';
                                            }
                                            ?>
                                        </div>
                                        <div class="col-6 d-flex justify-content-center">
                                            <?php if (!isset($activeCoinClass->reverse_image_url) || trim($activeCoinClass->reverse_image_url) === '') { ?>
                                                <div class="coin-image-placeholder">No Image Available</div>
                                            <?php
                                            } else {
                                                echo '<img src="/img/uploads/' . $activeCoinClass->reverse_image_url . '" style="max-width:180px;">';
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <p class="card-text"><?php echo (mb_strlen($catInfo->histc_comp_history) > 100) ? mb_substr($catInfo->histc_comp_history, 0, 97) . '...' : $catInfo->histc_comp_history; ?></p>
                                    <?php
                                    if ($hasSubCats) {
                                        echo '<a href="' . Shared::BuildSubCategoryLink($activeCoinClass->id, $clean_name) . '" class="btn btn-primary mt-auto">Explore Series</a>';
                                    } else {
                                        echo '<a href="' . Shared::BuildCategoryLink($activeCoinClass->id, $clean_name) . '" class="btn btn-primary mt-auto">Explore Series</a>';
                                    }
                                    ?>
                                </div>
                                <div class="card-footer"><?php if ($date_range != null) {
                                                                echo $date_range;
                                                            } else {
                                                                echo  $catInfo->core_minting_period;
                                                            } ?></div>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            </div>
        </section>
    </main>

    <?php echo Shared::PageFooterBlock(); ?>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>