import { Link } from '@inertiajs/react';
import { useState } from 'react';
import Shell from '../layouts/Shell';
import { displayDate, type PostSummary } from '../types/journal';

function PostCard({ post }: { post: PostSummary }) {
    return (
        <article className="post-card">
            <Link className="card-link" href={`/posts/${post.id}`}>
                {post.cover ? (
                    <img className="card-cover" src={post.cover} alt="" loading="lazy" />
                ) : (
                    <div className="card-cover text-cover" aria-hidden="true">
                        P.
                    </div>
                )}
                <div className="card-meta">
                    <span className="kicker">{post.category}</span>
                    <time dateTime={post.published_at}>{displayDate(post.published_at)}</time>
                </div>
                <h3>{post.title}</h3>
                <p>{post.excerpt}</p>
                <span className="card-read">
                    Read story <span className="arrow" aria-hidden="true" />
                </span>
            </Link>
        </article>
    );
}

export default function Journal({ posts }: { posts: PostSummary[] }) {
    const [category, setCategory] = useState<string | null>(null);
    const featured = posts.find((post) => post.cover !== null) ?? posts[0];
    const categories = [...new Set(posts.map((post) => post.category))];
    const selected = category !== null && categories.includes(category) ? category : null;
    const visible =
        selected === null
            ? [...posts.filter((post) => post.id !== featured?.id), ...(featured ? [featured] : [])]
            : posts.filter((post) => post.category === selected);

    return (
        <Shell active="journal">
            <div className="wrap">
                <section className="hero" aria-labelledby="journal-title">
                    <div className="hero-copy">
                        <p className="kicker hero-kicker">
                            <span /> An independent journal
                        </p>
                        <h1 id="journal-title">
                            Stories worth
                            <br />
                            <em>slowing down for.</em>
                        </h1>
                        <p className="hero-description">
                            A place for thoughtful writing on nature, culture, and the small things we notice along the
                            way.
                        </p>
                        <a className="solid" href="#stories">
                            Explore the journal <span className="arrow arrow-down-right" aria-hidden="true" />
                        </a>
                    </div>
                    {featured && (
                        <Link
                            className="feature"
                            href={`/posts/${featured.id}`}
                            aria-label={`Featured story: ${featured.title}`}
                        >
                            {featured.cover && <img src={featured.cover} alt="" fetchPriority="high" />}
                            <div className="feature-copy">
                                <p className="kicker">Featured story</p>
                                <div>
                                    <h2>{featured.title}</h2>
                                    <span className="feature-arrow" aria-hidden="true">
                                        <span className="arrow arrow-up-right" />
                                    </span>
                                </div>
                            </div>
                        </Link>
                    )}
                </section>

                <section className="stories" id="stories" aria-labelledby="stories-title">
                    <div className="section-head">
                        <div>
                            <p className="kicker">From the journal</p>
                            <h2 id="stories-title">Latest stories</h2>
                        </div>
                        {categories.length > 0 && (
                            <div className="filters" aria-label="Filter stories">
                                <button
                                    type="button"
                                    aria-pressed={selected === null}
                                    onClick={() => setCategory(null)}
                                >
                                    All stories
                                </button>
                                {categories.map((item) => (
                                    <button
                                        type="button"
                                        key={item}
                                        aria-pressed={selected === item}
                                        onClick={() => setCategory(item)}
                                    >
                                        {item}
                                    </button>
                                ))}
                            </div>
                        )}
                    </div>
                    {visible.length > 0 ? (
                        <div className="post-grid">
                            {visible.map((post) => (
                                <PostCard key={post.id} post={post} />
                            ))}
                        </div>
                    ) : (
                        <p className="empty">
                            The journal is waiting for its first story. <Link href="/register">Start writing</Link>.
                        </p>
                    )}
                </section>
            </div>
        </Shell>
    );
}
