---
import BaseLayout from "../layouts/BaseLayout.astro";
import projects from "../data/projects.json";

type Project = {
    name: string;
    description: string;
    tags: string[];
    status: "stable" | "wip" | "concept";
    links: {
        site?: string;
        gitea?: string;
        github?: string;
    };
};

const statusColors: Record<string, string> = {
    stable: "var(--color-green)",
    wip: "var(--color-peach)",
    concept: "var(--color-text-dim)",
};

const typedProjects = projects as Project[];

function getPrimaryLink(project: Project): string | undefined {
    return project.links.site || project.links.gitea || project.links.github;
}
---

<BaseLayout
    title="Projects — Hidden Den Cafe"
    description="Things Latte has built — bots, tools, self-hosted services, and experiments from the den."
>
    <div class="matrix-bg" aria-hidden="true"></div>

    <main class="main">
        <div class="container">
            <header class="header fade-in">
                <h1 class="title">Projects</h1>
                <div class="divider">══════════════════════════════</div>
            </header>

            <section class="section fade-in">
                <ul class="project-list">
                    {typedProjects.map((project) => (
                        <li class="project-item">
                            {getPrimaryLink(project) && (
                                <a
                                    class="project-card-link"
                                    href={getPrimaryLink(project)}
                                    target="_blank"
                                    rel="noopener noreferrer"
                                    aria-label={project.name}
                                ></a>
                            )}
                            <div class="project-header">
                                <h3 class="project-name">{project.name}</h3>
                                <span
                                    class="project-status"
                                    style={`color: ${statusColors[project.status]}`}
                                >
                                    [{project.status}]
                                </span>
                            </div>
                            <p class="project-desc">{project.description}</p>
                            <div class="project-meta">
                                <div class="project-tags">
                                    {project.tags.map((tag) => (
                                        <span class="tag">{tag}</span>
                                    ))}
                                </div>
                                <div class="project-links">
                                    {project.links.site && (
                                        <a class="project-link-badge" href={project.links.site} target="_blank" rel="noopener noreferrer">site</a>
                                    )}
                                    {project.links.gitea && (
                                        <a class="project-link-badge" href={project.links.gitea} target="_blank" rel="noopener noreferrer">gitea</a>
                                    )}
                                    {project.links.github && (
                                        <a class="project-link-badge" href={project.links.github} target="_blank" rel="noopener noreferrer">github</a>
                                    )}
                                </div>
                            </div>
                        </li>
                    ))}
                </ul>
            </section>

            <footer class="footer fade-in">
                <p>Made with love by Latte</p>
            </footer>
        </div>
    </main>
</BaseLayout>

<style>
    .matrix-bg {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: -1;
        opacity: 0.03;
        background:
            linear-gradient(var(--color-accent) 1px, transparent 1px),
            linear-gradient(90deg, var(--color-accent) 1px, transparent 1px);
        background-size: 50px 50px;
        animation: grid-move 20s linear infinite;
    }

    @keyframes grid-move {
        0% { transform: translate(0, 0); }
        100% { transform: translate(50px, 50px); }
    }

    .main {
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: var(--space-lg) var(--space-md);
        padding-top: calc(var(--space-lg) + 3rem);
    }

    .container {
        max-width: 700px;
        width: 100%;
        background: var(--color-glass);
        backdrop-filter: blur(10px);
        border: 1px solid var(--color-surface);
        border-radius: 8px;
        padding: var(--space-xl);
    }

    .header {
        text-align: center;
        margin-bottom: var(--space-lg);
    }

    .title {
        font-size: 2rem;
        font-weight: 700;
        letter-spacing: 2px;
    }

    .divider {
        color: var(--color-surface);
        text-align: center;
        font-size: 0.75rem;
        margin: var(--space-md) 0;
        user-select: none;
    }

    .section {
        margin: var(--space-lg) 0;
    }

    .project-list {
        list-style: none;
        display: flex;
        flex-direction: column;
        gap: var(--space-md);
    }

    .project-item {
        position: relative;
        border: 1px solid var(--color-surface);
        border-radius: 6px;
        padding: var(--space-md);
        background: var(--color-bg);
        transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
    }

    .project-item:hover {
        border-color: rgba(255, 255, 255, 0.2);
        box-shadow: 0 0 0 1px var(--color-accent);
        transform: translateY(-2px);
    }

    .project-item:hover .project-name {
        text-decoration: underline;
        text-underline-offset: 3px;
    }

    /* Stretched link covers the full card */
    .project-card-link {
        position: absolute;
        inset: 0;
        z-index: 0;
        border-radius: 6px;
    }

    .project-header {
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        gap: var(--space-sm);
        margin-bottom: var(--space-xs);
    }

    .project-name {
        font-size: 1rem;
        color: var(--color-accent-bright);
        font-weight: 700;
    }

    .project-status {
        font-size: 0.75rem;
        white-space: nowrap;
    }

    .project-desc {
        color: var(--color-text-dim);
        font-size: 0.85rem;
        line-height: 1.6;
        margin-bottom: var(--space-sm);
    }

    .project-meta {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--space-sm);
        flex-wrap: wrap;
    }

    .project-tags {
        display: flex;
        gap: var(--space-xs);
        flex-wrap: wrap;
    }

    .tag {
        font-size: 0.7rem;
        color: var(--color-accent);
        border: 1px solid var(--color-surface);
        padding: 1px 6px;
        border-radius: 3px;
    }

    .project-links {
        display: flex;
        gap: var(--space-sm);
        position: relative;
        z-index: 1;
    }

    .project-link-badge {
        color: var(--color-blue);
        font-size: 0.8rem;
    }

    .project-link-badge:hover {
        color: var(--color-accent-bright);
    }

    .footer {
        margin-top: var(--space-xl);
        text-align: center;
    }

    .footer p {
        color: var(--color-text-dim);
        font-size: 0.85rem;
    }

    .fade-in {
        animation: fadeIn 0.6s ease-out forwards;
        opacity: 0;
    }

    .fade-in:nth-child(1) { animation-delay: 0.1s; }
    .fade-in:nth-child(2) { animation-delay: 0.2s; }
    .fade-in:nth-child(3) { animation-delay: 0.3s; }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    @media (max-width: 600px) {
        .container {
            padding: var(--space-md);
        }

        .title {
            font-size: 1.5rem;
        }

        .divider {
            font-size: 0.6rem;
        }

        .project-header {
            flex-direction: column;
            gap: 2px;
        }

        .project-meta {
            flex-direction: column;
            align-items: flex-start;
        }
    }

    @media (prefers-reduced-motion: reduce) {
        .matrix-bg {
            animation: none;
        }

        .fade-in {
            animation: none;
            opacity: 1;
        }
    }
</style>
