import React from 'react';
import { Link, router } from '@inertiajs/react';
import { PublicHeader } from '@/Components/PublicHeader';
import { PublicFooter } from '@/Components/PublicFooter';
import { SEOHead } from '@/Components/SEOHead';
import { BlogCard } from '@/Components/BlogCard';
import { Pagination } from '@/Components/Pagination';
import { Search } from 'lucide-react';

interface BlogIndexProps {
    posts: any;
    categories: any[];
    tags: any[];
    featuredPosts: any[];
    filters: any;
}

export default function Index({ posts, categories, tags, featuredPosts, filters }: BlogIndexProps) {
    const handleSearch = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();
        const formData = new FormData(e.currentTarget);
        const search = formData.get('search') as string;
        router.get('/blog', { search }, { preserveState: true });
    };

    return (
        <div className="min-h-screen bg-[#060B18] text-slate-100 flex flex-col font-sans">
            <SEOHead
                title="Blog & Insights | Tech in Soul"
                description="Articles, tutorials, and insights on Laravel 13, React, Inertia.js, UI/UX design, and technical SEO from Tech in Soul."
            />

            <PublicHeader />

            <main className="flex-grow pt-32 pb-20">
                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12 text-center space-y-4 mb-12">
                    <span className="text-xs font-bold text-[#00F2FE] uppercase tracking-widest">
                        TECH IN SOUL BLOG
                    </span>
                    <h1 className="text-4xl sm:text-6xl font-extrabold text-white tracking-tight">
                        Engineering Insights & <span className="text-gradient-aqua">Ideas</span>
                    </h1>
                    <p className="text-slate-400 text-base sm:text-lg max-w-2xl mx-auto">
                        In-depth articles covering modern web architecture, product design, and growth strategy.
                    </p>
                </div>

                {/* Search Bar & Category Filter */}
                <div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 mb-12 space-y-6">
                    <form onSubmit={handleSearch} className="relative">
                        <input
                            type="text"
                            name="search"
                            defaultValue={filters.search || ''}
                            placeholder="Search articles by title or keyword..."
                            className="w-full bg-[#0B132B] border border-slate-800 rounded-2xl px-5 py-3.5 pl-12 text-sm text-slate-200 placeholder-slate-500 focus:outline-none focus:border-[#00F2FE]"
                        />
                        <Search className="w-5 h-5 text-slate-500 absolute left-4 top-4" />
                    </form>

                    <div className="flex flex-wrap items-center justify-center gap-2">
                        <Link
                            href="/blog"
                            className={`px-3.5 py-1.5 rounded-xl text-xs font-semibold ${
                                !filters.category
                                    ? 'bg-[#00F2FE] text-[#060B18]'
                                    : 'bg-[#0B132B] text-slate-300 border border-slate-800'
                            }`}
                        >
                            All Categories
                        </Link>
                        {categories.map((cat) => (
                            <Link
                                key={cat.id}
                                href={`/blog?category=${cat.slug}`}
                                className={`px-3.5 py-1.5 rounded-xl text-xs font-semibold ${
                                    filters.category === cat.slug
                                        ? 'bg-[#00F2FE] text-[#060B18]'
                                        : 'bg-[#0B132B] text-slate-300 border border-slate-800 hover:border-[#00F2FE]/40'
                                }`}
                            >
                                {cat.name}
                            </Link>
                        ))}
                    </div>
                </div>

                <div className="max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-12 space-y-12">
                    {posts.data.length > 0 ? (
                        <>
                            <div className="grid grid-cols-1 md:grid-cols-3 gap-8">
                                {posts.data.map((post: any) => (
                                    <BlogCard key={post.id} post={post} />
                                ))}
                            </div>

                            {/* Public Pagination Bar */}
                            {posts.links && posts.links.length > 3 && (
                                <div className="glass-card rounded-2xl p-4 border border-slate-800">
                                    <Pagination
                                        links={posts.links}
                                        from={posts.from}
                                        to={posts.to}
                                        total={posts.total}
                                    />
                                </div>
                            )}

                        </>
                    ) : (
                        <div className="glass-card rounded-2xl p-12 text-center text-slate-400">
                            No articles found matching your query.
                        </div>
                    )}
                </div>
            </main>


            <PublicFooter />
        </div>
    );
}
