<div class="container py-4">
    <!-- Header -->
    <div class="text-center mb-5">
        <span class="badge bg-success-subtle text-success rounded-pill px-3 py-2 mb-2 fw-semibold font-outfit text-uppercase">FINANCIAL AID HUB</span>
        <h2 class="fw-bold font-outfit text-dark display-6">Scholarship Intelligence Center</h2>
        <p class="text-muted mx-auto" style="max-width: 540px;">Search local government awards, university waivers, and international foundations to sponsor your undergraduate degree.</p>
    </div>

    <div class="row g-4">
        <!-- Main Search & Results -->
        <div class="col-lg-8">
            <!-- Filter Card -->
            <div class="card border-0 shadow-sm mb-4 p-4 bg-glass">
                <form action="<?= APP_URL ?>/scholarships" method="GET">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label text-xs fw-semibold text-muted">SEARCH BY TITLE / PROVIDER</label>
                            <input type="text" class="form-control bg-light border-0 rounded-3" name="q" value="<?= htmlspecialchars($filters['q'] ?? '') ?>" placeholder="e.g. Mastercard Foundation...">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label text-xs fw-semibold text-muted">CATEGORY</label>
                            <select class="form-select bg-light border-0" name="category">
                                <option value="">All Categories</option>
                                <option value="general" <?= ($filters['category'] ?? '') === 'general' ? 'selected' : '' ?>>General Arts/Science</option>
                                <option value="stem" <?= ($filters['category'] ?? '') === 'stem' ? 'selected' : '' ?>>STEM Specialization</option>
                                <option value="need-based" <?= ($filters['category'] ?? '') === 'need-based' ? 'selected' : '' ?>>Need-Based Support</option>
                                <option value="merit-based" <?= ($filters['category'] ?? '') === 'merit-based' ? 'selected' : '' ?>>Academic Merit</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label text-xs fw-semibold text-muted">TYPE</label>
                            <select class="form-select bg-light border-0" name="type">
                                <option value="">All Types</option>
                                <option value="full" <?= ($filters['type'] ?? '') === 'full' ? 'selected' : '' ?>>Full Scholarship</option>
                                <option value="partial" <?= ($filters['type'] ?? '') === 'partial' ? 'selected' : '' ?>>Partial Tuition</option>
                                <option value="grant" <?= ($filters['type'] ?? '') === 'grant' ? 'selected' : '' ?>>One-time Grant</option>
                            </select>
                        </div>
                        <div class="col-12 mt-3 d-flex justify-content-end">
                            <button type="submit" class="btn btn-success rounded-pill px-4 shadow-sm font-outfit fw-bold"><i class="fa-solid fa-magnifying-glass me-2"></i>Filter Scholarships</button>
                        </div>
                    </div>
                </form>
            </div>

            <!-- Results Listing -->
            <div class="row g-3">
                <?php if (!empty($scholarships)): ?>
                    <?php foreach ($scholarships as $sch): ?>
                        <div class="col-12">
                            <div class="card border-0 shadow-sm h-100 card-hover-effect border-start border-4 border-success">
                                <div class="card-body p-4">
                                    <div class="d-flex justify-content-between align-items-start mb-2">
                                        <div>
                                            <span class="badge bg-success-subtle text-success text-uppercase text-3xs font-outfit mb-2"><?= htmlspecialchars($sch['scholarship_type']) ?> Funding</span>
                                            <h5 class="fw-bold font-outfit text-dark mb-1"><?= htmlspecialchars($sch['name']) ?></h5>
                                            <p class="text-muted text-xs mb-2">Offered by: <strong><?= htmlspecialchars($sch['provider']) ?></strong></p>
                                        </div>
                                        <?php if (Auth::check()): ?>
                                            <a href="<?= APP_URL ?>/scholarships/<?= htmlspecialchars($sch['slug']) ?>" class="btn btn-xs btn-outline-success rounded-pill px-3">View & Predict</a>
                                        <?php else: ?>
                                            <a href="<?= APP_URL ?>/scholarships/<?= htmlspecialchars($sch['slug']) ?>" class="btn btn-xs btn-outline-success rounded-pill px-3">Details</a>
                                        <?php endif; ?>
                                    </div>
                                    
                                    <p class="text-muted text-sm"><?= truncate(htmlspecialchars($sch['description']), 150) ?></p>
                                    
                                    <div class="d-flex flex-wrap gap-4 text-xs text-muted border-top pt-3 mt-3">
                                        <div><i class="fa-solid fa-sack-dollar text-success me-1"></i> Coverage: <strong class="text-dark"><?= htmlspecialchars($sch['coverage']) ?></strong></div>
                                        <div><i class="fa-solid fa-graduation-cap text-success me-1"></i> Min Grade: <strong class="text-dark">Agg. <?= $sch['min_aggregate'] ?: 'Any' ?></strong></div>
                                        <div class="ms-auto text-danger fw-bold"><i class="fa-regular fa-clock me-1"></i> Deadline: <?= formatDate($sch['application_deadline']) ?></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div class="col-12 text-center py-5">
                        <i class="fa-solid fa-award display-4 text-muted mb-3"></i>
                        <h5 class="fw-bold text-muted">No Scholarships Found</h5>
                        <p class="text-muted text-sm">No records match your selected criteria. Try checking again with broad category options.</p>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Pagination -->
            <div class="mt-4">
                <?= paginationLinks($pagination, APP_URL . '/scholarships') ?>
            </div>
        </div>

        <!-- Sidebar Section -->
        <div class="col-lg-4">
            <!-- AI Recommendation Box for Students -->
            <?php if (Auth::isStudent()): ?>
                <div class="card border-0 shadow-sm mb-4 p-4 text-white position-relative overflow-hidden" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%);">
                    <h5 class="fw-bold font-outfit mb-2"><i class="fa-solid fa-wand-magic-sparkles me-2"></i>Personalized Matches</h5>
                    <p class="text-white-50 text-xs mb-3">Our AI Matching Engine matches scholarships to your WASSCE grades and financial background. Generate your matches now!</p>
                    <a href="<?= APP_URL ?>/scholarships/recommended" class="btn btn-light text-success rounded-pill px-4 btn-sm font-outfit fw-bold shadow-sm">View Recommendations</a>
                </div>
            <?php endif; ?>

            <!-- Upcoming Deadlines -->
            <div class="card border-0 shadow-sm p-4">
                <h5 class="fw-bold font-outfit text-dark mb-3"><i class="fa-regular fa-clock text-danger me-2"></i>Closing Soon</h5>
                <?php if (!empty($upcoming)): ?>
                    <div class="list-group list-group-flush">
                        <?php foreach ($upcoming as $u): ?>
                            <div class="list-group-item border-0 py-3 px-0">
                                <h6 class="text-sm fw-bold text-dark mb-1 text-truncate" title="<?= htmlspecialchars($u['name']) ?>"><a href="<?= APP_URL ?>/scholarships/<?= htmlspecialchars($u['slug']) ?>" class="text-decoration-none text-dark"><?= htmlspecialchars($u['name']) ?></a></h6>
                                <div class="d-flex justify-content-between text-xs mt-1">
                                    <span class="text-muted">By: <?= htmlspecialchars($u['provider']) ?></span>
                                    <span class="text-danger fw-bold"><?= formatDate($u['application_deadline']) ?></span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <p class="text-muted text-xs mb-0">No upcoming scholarship deadlines listed.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
