import AltivateLayout from '@/Layouts/AltivateLayout';
import { Head, Link } from '@inertiajs/react';
import { ArrowLeft, ArrowUpRight, Calendar, User, Facebook, Twitter, Linkedin, Link as LinkIcon } from 'lucide-react';

interface Post {
    id: number;
    title: string;
    slug: string;
    excerpt: string | null;
    content: string | null;
    published_at: string;
    category?: { name: string, slug: string };
    author?: { name: string };
    media?: { url: string };
}

export default function Show({ post, relatedPosts }: { post: Post, relatedPosts: Post[] }) {
    return (
        <AltivateLayout title={post.title} description={post.excerpt || undefined}>
            <Head title={post.title} />

            <div className="bg-white">
                {/* Article Header */}
                <header className="pt-32 pb-16 md:pt-44 md:pb-24">
                    <div className="container-x">
                        <Link 
                            href={route('blog.index')}
                            className="inline-flex items-center gap-2 text-sm text-[#8C92AC] hover:text-ink transition-colors mb-12"
                        >
                            <ArrowLeft className="h-4 w-4" />
                            Back to Insights
                        </Link>

                        <div className="max-w-4xl">
                            <div className="flex items-center gap-4 mb-8">
                                <span className="px-4 py-1.5 rounded-full border border-[#E8E6DF] text-[11px] font-bold uppercase tracking-widest text-accent bg-white">
                                    {post.category?.name || 'Insight'}
                                </span>
                                <div className="text-[11px] font-bold uppercase tracking-widest text-[#8C92AC]">
                                    {new Date(post.published_at).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}
                                </div>
                            </div>

                            <h1 className="font-display text-[clamp(2.5rem,6vw,5rem)] leading-[1.05] text-ink mb-10">
                                {post.title}
                            </h1>

                            <div className="flex items-center gap-4">
                                <div className="h-12 w-12 rounded-full bg-[#FAFAF8] border border-[#F0EDE8] flex items-center justify-center text-ink font-display text-xl">
                                    {post.author?.name?.charAt(0) || 'A'}
                                </div>
                                <div>
                                    <p className="text-[10px] font-bold uppercase tracking-widest text-[#8C92AC]">Written by</p>
                                    <p className="text-sm font-semibold text-ink">{post.author?.name || 'Altivate System'}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </header>

                {/* Article Content */}
                <section className="container-x pb-32">
                    <div className="grid lg:grid-cols-12 gap-16">
                        <div className="lg:col-span-8">
                            {post.media?.url && (
                                <div className="aspect-[21/9] rounded-[2.5rem] overflow-hidden bg-[#FAFAF8] border border-[#F0EDE8] mb-16 shadow-2xl shadow-ink/5">
                                    <img src={post.media.url} alt={post.title} className="w-full h-full object-cover" />
                                </div>
                            )}

                            <div 
                                className="prose prose-lg md:prose-xl prose-ink max-w-none prose-headings:font-display prose-headings:font-normal prose-headings:tracking-tight prose-p:text-[#4A4A4A] prose-p:leading-relaxed prose-a:text-accent prose-img:rounded-3xl"
                                dangerouslySetInnerHTML={{ __html: post.content || '' }}
                            />

                            {/* Share Buttons */}
                            <div className="mt-20 pt-10 border-t border-[#F0EDE8] flex flex-wrap items-center justify-between gap-6">
                                <div className="flex items-center gap-4">
                                    <span className="text-xs font-bold uppercase tracking-widest text-[#8C92AC]">Share Insight</span>
                                    <div className="flex gap-2">
                                        {[Twitter, Facebook, Linkedin, LinkIcon].map((Icon, i) => (
                                            <button key={i} className="h-10 w-10 rounded-full border border-[#E8E6DF] flex items-center justify-center text-[#8C92AC] hover:bg-ink hover:text-white transition-all">
                                                <Icon className="h-4 w-4" />
                                            </button>
                                        ))}
                                    </div>
                                </div>
                            </div>
                        </div>

                        {/* Sidebar / Related */}
                        <div className="lg:col-span-4">
                            <aside className="sticky top-32 space-y-12">
                                {relatedPosts.length > 0 && (
                                    <div className="space-y-6">
                                        <h3 className="font-display text-2xl">Related Insights</h3>
                                        <div className="space-y-8">
                                            {relatedPosts.map((rel) => (
                                                <Link key={rel.id} href={route('blog.show', rel.slug)} className="group block">
                                                    <div className="flex gap-4">
                                                        <div className="h-20 w-20 rounded-2xl bg-[#FAFAF8] border border-[#F0EDE8] overflow-hidden flex-shrink-0">
                                                            {rel.media?.url && <img src={rel.media.url} className="w-full h-full object-cover" />}
                                                        </div>
                                                        <div>
                                                            <p className="text-[10px] font-bold uppercase tracking-widest text-accent mb-1">{rel.category?.name}</p>
                                                            <h4 className="text-[15px] font-semibold text-ink group-hover:text-accent transition-colors leading-snug">{rel.title}</h4>
                                                        </div>
                                                    </div>
                                                </Link>
                                            ))}
                                        </div>
                                    </div>
                                )}

                                <div className="p-8 rounded-[2.5rem] bg-secondary border border-[#F0EDE8]">
                                    <h3 className="font-display text-2xl mb-4">Newsletter</h3>
                                    <p className="text-[#8C92AC] text-sm leading-relaxed mb-8">
                                        Get our best growth strategies delivered to your inbox every week. No fluff, just systems.
                                    </p>
                                    <form className="space-y-3">
                                        <input 
                                            type="email" 
                                            placeholder="your@email.com" 
                                            className="w-full h-12 rounded-full border border-[#E8E6DF] bg-white px-5 text-sm outline-none focus:ring-2 focus:ring-accent/20 transition-all" 
                                        />
                                        <button className="w-full h-12 rounded-full bg-ink text-white text-sm font-bold hover:bg-accent transition-all shadow-lg">
                                            Subscribe
                                        </button>
                                    </form>
                                </div>
                            </aside>
                        </div>
                    </div>
                </section>
            </div>
        </AltivateLayout>
    );
}
