import { JournalArt } from '@/site/components/art/journal-art';
import { Reveal } from '@/site/components/motion/reveal';
import { Tilt } from '@/site/components/motion/tilt';
import { CardGrid } from '@/site/components/sections/card-grid';
import { CtaBand } from '@/site/components/sections/cta-band';
import { PageHero, sheet } from '@/site/components/sections/page-hero';
import {
    PostCard,
    PostCover,
    PostMeta,
} from '@/site/components/sections/post-card';
import { ArrowRight } from '@/site/components/ui/icons';
import { SiteLink } from '@/site/components/ui/link';
import { useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';
import type { Pagination, PostCard as PostCardData } from '@/site/types';

type BlogIndexProps = {
    posts: PostCardData[];
    pagination: Pagination;
};

/**
 * Blog index: the newest post as a large feature, the others as cards.
 * Later pages show cards only.
 */
export default function BlogIndex({ posts, pagination }: BlogIndexProps) {
    const t = useT();
    const firstPage = pagination.current === 1;
    const [feature, ...rest] = firstPage ? posts : [];
    const cards = firstPage ? rest : posts;

    return (
        <>
            <PageHero
                eyebrow={t.blog.eyebrow}
                title={t.blog.title}
                lead={t.blog.lead}
                art={<JournalArt />}
            />

            <section className={cx('stage-light section-y-sm', sheet)}>
                <div className="shell">
                    {posts.length === 0 ? (
                        <p className="text-soft text-lead">{t.blog.empty}</p>
                    ) : null}

                    {feature ? (
                        <Reveal>
                            <article
                                data-accent={feature.category?.accent}
                                className="group border-line bg-stage-2 ease-out-expo hover:border-accent-fg/60 relative isolate grid overflow-hidden rounded-xl border transition-colors duration-500 lg:grid-cols-2"
                            >
                                <PostCover
                                    post={feature}
                                    priority
                                    sizes="(min-width: 64rem) 50vw, 100vw"
                                    className="aspect-[16/10] lg:aspect-auto lg:min-h-[26rem]"
                                />
                                <div className="flex flex-col justify-center p-8 md:p-12 lg:p-14">
                                    <p className="text-eyebrow text-accent-fg font-semibold uppercase">
                                        <span
                                            lang={
                                                feature.category?.lang ??
                                                undefined
                                            }
                                        >
                                            {feature.category?.name ??
                                                t.blog.eyebrow}
                                        </span>
                                    </p>
                                    <h2 className="font-display text-display-3 mt-5">
                                        <SiteLink
                                            href={feature.url}
                                            className="after:absolute after:inset-0"
                                        >
                                            {feature.title}
                                        </SiteLink>
                                    </h2>
                                    <p className="text-soft mt-5 text-lg">
                                        {feature.excerpt}
                                    </p>
                                    <PostMeta post={feature} className="mt-6" />
                                    <span
                                        aria-hidden="true"
                                        className="text-accent-fg mt-8 inline-flex items-center gap-2 font-medium"
                                    >
                                        {t.blog.read}
                                        <ArrowRight className="ease-out-expo size-4 transition-transform duration-500 group-hover:translate-x-1" />
                                    </span>
                                </div>
                            </article>
                        </Reveal>
                    ) : null}

                    {cards.length > 0 ? (
                        <CardGrid
                            items={cards}
                            itemKey={(post) => post.key}
                            className={feature ? 'mt-5' : undefined}
                        >
                            {(post) => (
                                <Tilt className="rounded-lg">
                                    <PostCard post={post} as="h2" />
                                </Tilt>
                            )}
                        </CardGrid>
                    ) : null}

                    {pagination.last > 1 ? (
                        <nav
                            aria-label={t.blog.pages}
                            className="border-line mt-14 flex items-center justify-between gap-6 border-t pt-8"
                        >
                            {pagination.previousUrl ? (
                                <SiteLink
                                    href={pagination.previousUrl}
                                    rel="prev"
                                    className="link-underline font-medium"
                                >
                                    ← {t.blog.newer}
                                </SiteLink>
                            ) : (
                                <span />
                            )}
                            <p className="text-soft text-sm tabular-nums">
                                {t.blog.page(
                                    pagination.current,
                                    pagination.last,
                                )}
                            </p>
                            {pagination.nextUrl ? (
                                <SiteLink
                                    href={pagination.nextUrl}
                                    rel="next"
                                    className="link-underline font-medium"
                                >
                                    {t.blog.older} →
                                </SiteLink>
                            ) : (
                                <span />
                            )}
                        </nav>
                    ) : null}
                </div>
            </section>

            <CtaBand />
        </>
    );
}
