import AltivateLayout from '@/Layouts/AltivateLayout';
import { Head, Link } from '@inertiajs/react';
import { ArrowUpRight, Calendar, User, Folder, ChevronLeft, ChevronRight } from 'lucide-react';

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

interface Category {
    id: number;
    name: string;
    slug: string;
}

interface PaginationLink {
    url: string | null;
    label: string;
    active: boolean;
}

interface Props {
    posts: {
        data: Post[];
        links: PaginationLink[];
        current_page: number;
        last_page: number;
    };
    categories: Category[];
}

export default function Index({ posts, categories }: Props) {
    const postList = posts.data || [];

    return (
        <AltivateLayout title="Insights" description="Strategies, frameworks, and deep dives into building high-performance business systems.">
            <Head title="Insights" />

            <div className="bg-white">
                {/* Header */}
                <section className="container-x pt-32 pb-20 md:pt-40 md:pb-32">
                    <p className="eyebrow">Altivate Insights</p>
                    <h1 className="font-display mt-6 text-[clamp(2.5rem,7vw,5rem)] leading-[0.95] text-ink">
                        Strategies for the <span className="italic">modern system.</span>
                    </h1>
                    
                    <div className="mt-12 flex flex-wrap gap-3">
                        <Link 
                            href={route('blog.index')}
                            className="px-5 py-2 rounded-full border border-border text-sm font-medium hover:border-accent transition-colors"
                        >
                            All Articles
                        </Link>
                        {categories.map((cat) => (
                            <Link 
                                key={cat.id}
                                href={route('blog.index', { category: cat.slug })}
                                className="px-5 py-2 rounded-full border border-border text-sm font-medium hover:border-accent transition-colors"
                            >
                                {cat.name}
                            </Link>
                        ))}
                    </div>
                </section>

                {/* Blog Grid */}
                <section className="container-x pb-32">
                    {postList.length > 0 ? (
                        <>
                            <div className="grid md:grid-cols-2 lg:grid-cols-3 gap-x-8 gap-y-16">
                                {postList.map((post) => (
                                    <article key={post.id} className="group cursor-pointer">
                                        <Link href={route('blog.show', post.slug)}>
                                            <div className="aspect-[16/10] rounded-[2rem] overflow-hidden bg-[#FAFAF8] border border-[#F0EDE8] mb-6 transition-transform group-hover:scale-[1.02] duration-500 shadow-xl shadow-ink/5">
                                                {post.media?.url ? (
                                                    <img src={post.media.url} alt={post.title} className="w-full h-full object-cover" />
                                                ) : (
                                                    <div className="w-full h-full flex items-center justify-center text-[#C0BDBA]">
                                                        <Folder className="h-10 w-10 opacity-20" />
                                                    </div>
                                                )}
                                            </div>
                                            
                                            <div className="space-y-4">
                                                <div className="flex items-center gap-3 text-[10px] font-bold uppercase tracking-widest text-accent">
                                                    <span>{post.category?.name || 'Insight'}</span>
                                                    <span className="h-1 w-1 rounded-full bg-[#E8E6DF]" />
                                                    <span className="text-[#8C92AC]">{new Date(post.published_at).toLocaleDateString()}</span>
                                                </div>

                                                <h2 className="font-display text-2xl leading-[1.2] text-ink group-hover:text-accent transition-colors">
                                                    {post.title}
                                                </h2>

                                                <p className="text-[#8C92AC] text-sm leading-relaxed line-clamp-2">
                                                    {post.excerpt}
                                                </p>

                                                <div className="pt-2 inline-flex items-center gap-2 text-xs font-bold uppercase tracking-widest text-ink group-hover:gap-3 transition-all">
                                                    Read full story <ArrowUpRight className="h-4 w-4" />
                                                </div>
                                            </div>
                                        </Link>
                                    </article>
                                ))}
                            </div>

                            {/* Public Pagination */}
                            {posts.last_page > 1 && (
                                <div className="mt-20 flex items-center justify-center gap-1.5 border-t border-border pt-10">
                                    {posts.links.map((link, i) => {
                                        const isPrev = i === 0;
                                        const isNext = i === posts.links.length - 1;
                                        
                                        if (isPrev) {
                                            return (
                                                <Link
                                                    key={i}
                                                    href={link.url || '#'}
                                                    className={`p-3 rounded-full border border-border transition-all ${!link.url ? 'opacity-30 cursor-not-allowed' : 'hover:border-accent text-ink'}`}
                                                >
                                                    <ChevronLeft className="h-4 w-4" />
                                                </Link>
                                            );
                                        }
                                        
                                        if (isNext) {
                                            return (
                                                <Link
                                                    key={i}
                                                    href={link.url || '#'}
                                                    className={`p-3 rounded-full border border-border transition-all ${!link.url ? 'opacity-30 cursor-not-allowed' : 'hover:border-accent text-ink'}`}
                                                >
                                                    <ChevronRight className="h-4 w-4" />
                                                </Link>
                                            );
                                        }

                                        const isCurrent = link.active;
                                        const pageNum = parseInt(link.label);
                                        const isNear = Math.abs(pageNum - posts.current_page) <= 1;
                                        const isEdge = pageNum === 1 || pageNum === posts.last_page;

                                        if (isCurrent || isNear || isEdge) {
                                            return (
                                                <Link
                                                    key={i}
                                                    href={link.url || '#'}
                                                    className={`min-w-[40px] h-[40px] flex items-center justify-center rounded-full border text-xs font-semibold transition-all ${
                                                        link.active
                                                            ? 'bg-primary border-primary text-white'
                                                            : 'bg-white border-border text-ink hover:border-accent'
                                                    }`}
                                                    dangerouslySetInnerHTML={{ __html: link.label }}
                                                />
                                            );
                                        }
                                        
                                        if (pageNum === posts.current_page - 2 || pageNum === posts.current_page + 2) {
                                            return <span key={i} className="px-1 text-[#8C92AC]">...</span>;
                                        }

                                        return null;
                                    })}
                                </div>
                            )}
                        </>
                    ) : (
                        <div className="py-20 text-center">
                            <p className="text-muted-foreground italic">No articles found in this category. Check back soon.</p>
                        </div>
                    )}
                </section>
            </div>
        </AltivateLayout>
    );
}
