import { Head, Link } from '@inertiajs/react';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import type { BlogPost } from './types';

type PageProps = {
    post: Required<Pick<BlogPost, 'id' | 'title' | 'slug' | 'content'>> &
        BlogPost;
    recentPosts: BlogPost[];
};

export default function BlogPost({ post, recentPosts }: PageProps) {
    return (
        <>
            <Head title={post.title} />

            <article className="border-b bg-muted/20">
                <div className="mx-auto max-w-4xl space-y-5 px-4 py-12">
                    <div className="flex flex-wrap gap-2">
                        {post.categories?.map((category) => (
                            <Badge key={category.id} variant="secondary">
                                {category.name}
                            </Badge>
                        ))}
                    </div>
                    <h1 className="text-4xl font-semibold tracking-normal sm:text-5xl">
                        {post.title}
                    </h1>
                    <div className="text-sm text-muted-foreground">
                        {post.author?.name ?? 'Shop team'}
                        {post.published_at &&
                            ` · ${new Date(post.published_at).toLocaleDateString()}`}
                    </div>
                    {post.excerpt && (
                        <p className="text-lg text-muted-foreground">
                            {post.excerpt}
                        </p>
                    )}
                </div>
            </article>

            <div className="mx-auto grid max-w-7xl gap-10 px-4 py-12 lg:grid-cols-[1fr_320px]">
                <div className="space-y-8">
                    {post.feature_image_url && (
                        <img
                            src={post.feature_image_url}
                            alt={post.title}
                            className="aspect-video w-full rounded-xl border object-cover"
                        />
                    )}
                    <div
                        className="prose dark:prose-invert max-w-none"
                        dangerouslySetInnerHTML={{ __html: post.content ?? '' }}
                    />
                </div>

                <aside className="space-y-4">
                    <div className="rounded-lg border p-5">
                        <h2 className="font-semibold">Tags</h2>
                        <div className="mt-3 flex flex-wrap gap-2">
                            {post.tags?.length ? (
                                post.tags.map((tag) => (
                                    <Badge key={tag.id} variant="outline">
                                        {tag.name}
                                    </Badge>
                                ))
                            ) : (
                                <span className="text-sm text-muted-foreground">
                                    No tags
                                </span>
                            )}
                        </div>
                    </div>

                    <div className="rounded-lg border p-5">
                        <h2 className="font-semibold">Recent posts</h2>
                        <div className="mt-3 space-y-3">
                            {recentPosts.map((recentPost) => (
                                <Link
                                    key={recentPost.id}
                                    href={`/blog/${recentPost.slug}`}
                                    className="block text-sm hover:underline"
                                >
                                    {recentPost.title}
                                </Link>
                            ))}
                        </div>
                    </div>

                    <Button variant="outline" asChild>
                        <Link href="/blog">Back to blog</Link>
                    </Button>
                </aside>
            </div>
        </>
    );
}
