import { createFileRoute } from "@tanstack/react-router"
import { ArrowDown, ArrowUpRight, Code2, GitBranch } from "lucide-react"
import { useEffect, useState } from "react"
import { ProjectCard } from "@/components/portfolio/ProjectCard"
import { SectionHeading } from "@/components/portfolio/SectionHeading"
import { Win98Status } from "@/components/portfolio/Win98Status"

export const Route = createFileRoute("/_main/")({
    component: RouteComponent,
})

const sections = [
    { id: "top", number: "00", label: "Home" },
    { id: "work", number: "01", label: "Selected work" },
    { id: "workbench", number: "02", label: "Workbench" },
    { id: "about", number: "03", label: "About" },
    { id: "repositories", number: "04", label: "Repositories" },
] as const

function RouteComponent() {
    const [activeSection, setActiveSection] = useState("top")

    useEffect(() => {
        const observer = new IntersectionObserver(
            entries => {
                const visible = entries
                    .filter(entry => entry.isIntersecting)
                    .sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0]

                if (visible) setActiveSection(visible.target.id)
            },
            { rootMargin: "-18% 0px -62% 0px", threshold: [0, 0.15, 0.4] },
        )

        for (const section of sections) {
            const element = document.getElementById(section.id)
            if (element) observer.observe(element)
        }

        return () => observer.disconnect()
    }, [])

    const current = sections.find(section => section.id === activeSection) ?? sections[0]

    return (
        <main className="portfolio-page">
            <div className="site-shell">
                <aside className="site-rail" aria-label="Page index">
                    <a className="site-mark" href="#top" aria-label="Julien Valverde, back to top">
                        <span>JV</span>
                        <span>Julien<br />Valverde</span>
                    </a>

                    <nav aria-label="Main navigation">
                        <p>Index</p>
                        {sections.map(section => (
                            <a
                                key={section.id}
                                href={`#${section.id}`}
                                className={activeSection === section.id ? "is-active" : undefined}
                                aria-current={activeSection === section.id ? "location" : undefined}
                            >
                                <span>{section.number}</span>
                                {section.label}
                            </a>
                        ))}
                    </nav>

                    <div className="site-rail__meta">
                        <span className="availability-dot" aria-hidden="true" />
                        Available for select projects
                        <small>Chambéry, France</small>
                    </div>
                </aside>

                <div className="site-content">
                    <header className="site-header">
                        <p><span>julien.dev</span> / {current.label}</p>
                        <a href="mailto:hello@valverde.cloud">hello@valverde.cloud ↗</a>
                    </header>

                    <section className="hero" id="top">
                        <div className="hero__intro">
                            <p className="hero__kicker">Full-stack engineer · Product builder</p>
                            <h1>
                                Useful systems,
                                <span>built properly.</span>
                            </h1>
                            <p className="hero__summary">
                                I turn difficult domain problems into clear, durable software—from
                                open-source primitives to complete vertical products.
                            </p>
                            <a className="brutal-button" href="#work">
                                See the work
                                <ArrowDown aria-hidden="true" />
                            </a>
                        </div>

                        <aside className="hero__status" aria-label="Developer status">
                            <Win98Status />
                        </aside>
                    </section>

                    <ul className="capability-strip" aria-label="Core capabilities">
                        <li>TypeScript</li>
                        <li>Effect</li>
                        <li>React</li>
                        <li>Product engineering</li>
                        <li>Open source</li>
                    </ul>

                    <section className="work-section" id="work">
                        <SectionHeading
                            eyebrow="01 / Selected work"
                            title="Shipped and useful."
                            description="Libraries and products solving concrete problems. No concept pieces."
                        />

                        <div className="project-grid">
                            <ProjectCard
                                index="01"
                                title="effect-lens"
                                category="Open source / library"
                                description="Focus, subscribe to, and update nested state without losing typed failures or requirements."
                                tags={["Effect", "State", "TypeScript"]}
                                href="https://www.npmjs.com/package/effect-lens"
                                linkLabel="View on npm"
                                variant="dark"
                            />
                            <ProjectCard
                                index="02"
                                title="effect-view"
                                category="Open source / React"
                                description="A small bridge for writing React function components as composable Effect programs."
                                tags={["Effect", "React", "DX"]}
                                href="https://www.npmjs.com/package/effect-view"
                                linkLabel="View on npm"
                            />
                            <ProjectCard
                                index="03"
                                title="Comme le Chef"
                                category="SaaS / Food tech"
                                description="A recipe workspace for nutrition, allergens, costing, and production documents."
                                tags={["Product", "SaaS", "Food tech"]}
                                href="https://commedeschef.com/"
                                linkLabel="Visit website"
                                variant="outline"
                            />
                        </div>
                    </section>

                    <section className="upcoming-section" id="workbench">
                        <SectionHeading
                            eyebrow="02 / Workbench"
                            title="Now building."
                            description="Two systems in active development."
                        />

                        <div className="upcoming-grid">
                            <ProjectCard
                                index="04"
                                title="FLOWpti"
                                category="Upcoming / Logistics"
                                description="Route optimization that turns operational constraints into efficient delivery plans."
                                tags={["Routing", "Optimization", "B2B"]}
                            />
                            <ProjectCard
                                index="05"
                                title="Sentinel"
                                category="Upcoming / Open source"
                                description="Self-hosted OBD-II telemetry for vehicle diagnostics and remote insight."
                                tags={["OBD-II", "Telemetry", "Self-hosted"]}
                                variant="dark"
                            />
                        </div>
                    </section>

                    <section className="about-section" id="about">
                        <div className="about-section__label">
                            <span>03</span>
                            <span>About</span>
                        </div>
                        <div className="about-section__copy">
                            <p className="about-section__lead">
                                Full-stack engineer and entrepreneur in the French Alps.
                            </p>
                            <div className="about-section__columns">
                                <p>
                                    I work in the difficult middle, where domain problems, product
                                    decisions, interface design, and systems engineering meet.
                                </p>
                                <p>
                                    Current interests: typed functional programming, React,
                                    operational software, optimization, and self-hosted infrastructure.
                                </p>
                            </div>
                        </div>
                    </section>

                    <section className="repo-section" id="repositories">
                        <SectionHeading
                            eyebrow="04 / Repositories"
                            title="Follow the commits."
                            description="Source code, experiments, and work in progress."
                        />
                        <div className="repo-links">
                            <a href="https://git.valverde.cloud/" target="_blank" rel="noreferrer">
                                <GitBranch aria-hidden="true" />
                                <span><small>Primary forge</small>git.valverde.cloud</span>
                                <ArrowUpRight aria-hidden="true" />
                            </a>
                            <a href="https://github.com/Thiladev" target="_blank" rel="noreferrer">
                                <Code2 aria-hidden="true" />
                                <span><small>Public collaboration</small>github.com/Thiladev</span>
                                <ArrowUpRight aria-hidden="true" />
                            </a>
                        </div>
                    </section>

                    <footer className="site-footer">
                        <p>Build it well. Make it useful.</p>
                        <div>
                            <span>Julien Valverde © {new Date().getFullYear()}</span>
                            <a href="#top">Back to top ↑</a>
                        </div>
                    </footer>
                </div>
            </div>
        </main>
    )
}
