<?php
require_once __DIR__ . '/dbconfig.php';

// Fetch data
$stmt = $pdo->query("SELECT id, nama, url, kategori, skill_count, target_agents, deskripsi
                     FROM skill_marketplace
                     ORDER BY kategori DESC, urutan ASC");
$rows = $stmt->fetchAll();

$byKategori = ['website' => [], 'github' => []];
foreach ($rows as $r) {
    $byKategori[$r['kategori']][] = $r;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Skills Marketplace &middot; k4il.web.id</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: linear-gradient(135deg, #070b1a, #0f1a3a, #1a0a2e);
    color: #e0e0e0;
    min-height: 100vh;
    padding: 2rem 1rem;
    line-height: 1.6;
}

.wrap { max-width: 920px; margin: 0 auto; }

header {
    text-align: center;
    margin-bottom: 2.4rem;
    padding: 1.5rem;
    background: rgba(10, 15, 40, 0.5);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border-radius: 20px;
    border: 1px solid rgba(37, 99, 235, 0.15);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
}
header h1 {
    font-size: 1.7rem;
    font-weight: 800;
    background: linear-gradient(135deg, #60a5fa, #a78bfa);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 0.4rem;
}
header p { color: #94a3b8; font-size: 0.9rem; }
header a.back {
    display: inline-block;
    margin-top: 0.8rem;
    color: #93c5fd;
    text-decoration: none;
    font-size: 0.85rem;
    padding: 0.35rem 0.9rem;
    border: 1px solid rgba(96, 165, 250, 0.3);
    border-radius: 8px;
    transition: all 0.2s;
}
header a.back:hover { background: rgba(96, 165, 250, 0.1); }

section { margin-bottom: 2.2rem; }
section h2 {
    font-size: 1.05rem;
    color: #93c5fd;
    margin-bottom: 0.9rem;
    padding-left: 0.6rem;
    border-left: 3px solid #2563eb;
    font-weight: 600;
    letter-spacing: 0.02em;
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 0.9rem;
}

.card {
    background: rgba(10, 15, 40, 0.5);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 14px;
    padding: 1.1rem;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    text-decoration: none;
    color: inherit;
}
.card:hover {
    border-color: rgba(96, 165, 250, 0.4);
    background: rgba(96, 165, 250, 0.05);
    transform: translateY(-3px);
    box-shadow: 0 8px 24px rgba(37, 99, 235, 0.15);
}

.card-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.5rem;
}
.card-name {
    font-weight: 700;
    color: #e2e8f0;
    font-size: 0.95rem;
    word-break: break-word;
}
.card-count {
    flex-shrink: 0;
    font-size: 0.72rem;
    padding: 0.15rem 0.5rem;
    background: rgba(37, 99, 235, 0.2);
    color: #93c5fd;
    border-radius: 6px;
    font-weight: 600;
    white-space: nowrap;
}
.card-target {
    font-size: 0.74rem;
    color: #94a3b8;
}
.card-desc {
    font-size: 0.8rem;
    color: #8b8fa3;
    line-height: 1.5;
    margin-top: 0.2rem;
}

.gh-badge {
    display: inline-block;
    font-size: 0.7rem;
    padding: 0.1rem 0.4rem;
    background: rgba(124, 58, 237, 0.2);
    color: #c4b5fd;
    border-radius: 5px;
    margin-left: 0.5rem;
    vertical-align: middle;
}

footer {
    text-align: center;
    margin-top: 2.5rem;
    padding-top: 1.2rem;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    font-size: 0.72rem;
    color: #4a4d5e;
}

@media (max-width: 480px) {
    body { padding: 1rem 0.7rem; }
    .grid { grid-template-columns: 1fr; }
    header h1 { font-size: 1.3rem; }
}
</style>
</head>
<body>

<div class="wrap">

<header>
    <h1>🧩 Skills Marketplace</h1>
    <p>Direktori skill untuk AI Agent (Claude Code, Codex CLI, ChatGPT, dll)</p>
    <a class="back" href="/">&larr; Kembali ke Beranda</a>
</header>

<?php if (empty($rows)): ?>
<div style="text-align:center;padding:3rem;color:#64748b">
    <p>Belum ada data.</p>
</div>
<?php else: ?>

<?php if (!empty($byKategori['website'])): ?>
<section>
    <h2>🌐 Web Marketplace / Directory</h2>
    <div class="grid">
        <?php foreach ($byKategori['website'] as $r): ?>
        <a class="card" href="<?= htmlspecialchars($r['url']) ?>" target="_blank" rel="noopener">
            <div class="card-head">
                <span class="card-name"><?= htmlspecialchars($r['nama']) ?></span>
                <span class="card-count"><?= htmlspecialchars($r['skill_count']) ?></span>
            </div>
            <div class="card-target">🎯 <?= htmlspecialchars($r['target_agents']) ?></div>
            <div class="card-desc"><?= htmlspecialchars($r['deskripsi']) ?></div>
        </a>
        <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>

<?php if (!empty($byKategori['github'])): ?>
<section>
    <h2>📦 GitHub Collections <span class="gh-badge">Repositori</span></h2>
    <div class="grid">
        <?php foreach ($byKategori['github'] as $r): ?>
        <a class="card" href="<?= htmlspecialchars($r['url']) ?>" target="_blank" rel="noopener">
            <div class="card-head">
                <span class="card-name"><?= htmlspecialchars($r['nama']) ?></span>
                <span class="card-count">⭐ <?= htmlspecialchars($r['skill_count']) ?></span>
            </div>
            <div class="card-target">🎯 <?= htmlspecialchars($r['target_agents']) ?></div>
            <div class="card-desc"><?= htmlspecialchars($r['deskripsi']) ?></div>
        </a>
        <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>

<?php endif; ?>

<footer>&copy; 2026 &middot; k4il.web.id</footer>

</div>

</body>
</html>