import { ContourArt } from '@/site/components/art/contour-art';
import { MediaSlot } from '@/site/components/media/media-slot';
import { SiteLink } from '@/site/components/ui/link';
import { useShared } from '@/site/hooks/use-shared';
import { formatDate, useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';
import type { PostCard as PostCardData } from '@/site/types';

/**
 * Cover of a blog post: its photo once one is added to the media library
 * (media/blog/{key}.*), generative artwork in the post's accent until then.
 */
export function PostCover({
    post,
    priority,
    className,
}: {
    post: PostCardData;
    priority?: boolean;
    className?: string;
}) {
    return (
        <div
            data-accent={post.category?.accent}
            className={cx(
                'stage-dark relative isolate overflow-hidden',
                className,
            )}
        >
            <MediaSlot
                media={post.media}
                priority={priority}
                className="absolute inset-0"
                fallback={
                    <>
                        <div
                            aria-hidden="true"
                            className="absolute inset-0 -z-10 bg-[radial-gradient(120%_90%_at_85%_15%,var(--accent-2),transparent_60%)] opacity-60"
                        />
                        <ContourArt
                            seed={post.key}
                            className="ease-out-expo absolute -end-[12%] top-1/2 size-auto h-[165%] -translate-y-1/2 opacity-90 transition-transform duration-[1.2s] group-hover:scale-110 group-hover:rotate-6"
                        />
                    </>
                }
            />
        </div>
    );
}

/** Date and reading time of a post. */
export function PostMeta({
    post,
    className,
}: {
    post: PostCardData;
    className?: string;
}) {
    const { locale } = useShared();
    const t = useT();

    return (
        <p
            className={cx(
                'text-soft flex flex-wrap gap-x-3 text-sm',
                className,
            )}
        >
            {post.publishedAt ? (
                <time dateTime={post.publishedAt}>
                    {formatDate(post.publishedAt, locale)}
                </time>
            ) : null}
            <span aria-hidden="true">·</span>
            <span>{t.blog.minutes(post.readingMinutes)}</span>
        </p>
    );
}

type PostCardProps = {
    post: PostCardData;
    /** Heading level of the title within its section. */
    as?: 'h2' | 'h3';
    className?: string;
};

/**
 * Teaser of a blog post. The whole card is one link (the title's link is
 * stretched over it).
 */
export function PostCard({ post, as: Title = 'h3', className }: PostCardProps) {
    const t = useT();

    return (
        <article
            data-accent={post.category?.accent}
            className={cx(
                'group border-line bg-stage-2 ease-out-expo hover:border-accent-fg/60 relative isolate flex h-full flex-col overflow-hidden rounded-lg border transition-[border-color,transform] duration-500 hover:-translate-y-1',
                className,
            )}
        >
            <PostCover post={post} className="aspect-[16/10]" />

            <div className="flex grow flex-col p-7 md:p-8">
                <p className="text-eyebrow text-accent-fg font-semibold uppercase">
                    <span lang={post.category?.lang ?? undefined}>
                        {post.category?.name ?? t.blog.eyebrow}
                    </span>
                </p>
                <Title className="font-display mt-4 text-[1.625rem] leading-[1.15]">
                    <SiteLink
                        href={post.url}
                        className="after:absolute after:inset-0"
                    >
                        {post.title}
                    </SiteLink>
                </Title>
                <p className="text-soft mt-3 line-clamp-3 text-[0.9375rem]">
                    {post.excerpt}
                </p>
                <PostMeta post={post} className="mt-auto pt-6" />
            </div>
        </article>
    );
}
