import AdminLayout from '@/Layouts/AdminLayout';
import { Head, Link, router } from '@inertiajs/react';
import { FileText, ExternalLink, Edit, Trash2, CheckCircle2, Circle, User, Folder } from 'lucide-react';

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

export default function Index({ posts }: { posts: Post[] }) {
    const deletePost = (id: number) => {
        if (confirm('Delete this article?')) {
            router.delete(route('admin.posts.destroy', id));
        }
    };

    return (
        <AdminLayout>
            <Head title="Blog Posts" />

            {/* Header */}
            <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-6 mb-10">
                <div>
                    <h1 className="font-display text-[1.5rem] md:text-[1.75rem] font-normal tracking-[-0.02em] text-ink leading-tight">
                        Blog Insights
                    </h1>
                    <p className="mt-2 text-[13px] md:text-[14px] text-[#8C92AC]">
                        Share your thoughts and growth strategies.
                    </p>
                </div>
                <Link
                    href={route('admin.posts.create')}
                    className="h-11 sm:h-9 flex items-center justify-center px-6 bg-ink text-white text-[12px] font-semibold rounded-lg hover:bg-primary transition-colors w-full sm:w-auto shadow-lg shadow-ink/10"
                >
                    New Article
                </Link>
            </div>

            {/* Mobile View: Cards */}
            <div className="grid gap-3 md:hidden mb-8">
                {posts.length > 0 ? posts.map((p) => (
                    <div key={p.id} className="bg-white rounded-[1.5rem] border border-[#E8E6DF] p-5 shadow-sm">
                        <div className="flex items-start gap-4 mb-4">
                            <div className="h-12 w-12 bg-[#F5F3EF] border border-[#E8E6DF] rounded-xl overflow-hidden flex-shrink-0">
                                {p.media?.url ? (
                                    <img src={p.media.url} className="w-full h-full object-cover" />
                                ) : (
                                    <div className="w-full h-full flex items-center justify-center text-[#C0BDBA]">
                                        <FileText className="h-5 w-5" />
                                    </div>
                                )}
                            </div>
                            <div className="flex-1 min-w-0">
                                <div className="flex items-start justify-between gap-2">
                                    <p className="text-[14px] font-bold text-ink leading-tight">{p.title}</p>
                                    <div className="flex-shrink-0 mt-0.5">
                                        {p.is_published ? (
                                            <CheckCircle2 className="h-4 w-4 text-green-500" />
                                        ) : (
                                            <Circle className="h-4 w-4 text-[#C0BDBA]" />
                                        )}
                                    </div>
                                </div>
                                <p className="text-[10px] text-[#8C92AC] mt-1.5 font-bold uppercase tracking-tighter">
                                    {p.published_at ? new Date(p.published_at).toLocaleDateString() : 'Draft'} • {p.category?.name || 'Uncategorized'}
                                </p>
                            </div>
                        </div>
                        
                        <div className="flex items-center justify-between pt-4 border-t border-[#F0EDE8]">
                            <Link href={route('admin.posts.edit', p.id)} className="flex items-center gap-2 px-4 py-2 bg-[#F5F3EF] text-[11px] font-bold text-ink rounded-lg border border-[#E8E6DF]">
                                <Edit className="h-3.5 w-3.5" /> Edit
                            </Link>
                            <div className="flex items-center gap-1">
                                <Link href={`/blog/${p.slug}`} target="_blank" className="p-2 text-[#8C92AC] hover:text-ink">
                                    <ExternalLink className="h-4 w-4" />
                                </Link>
                                <button onClick={() => deletePost(p.id)} className="p-2 text-[#8C92AC] hover:text-red-500">
                                    <Trash2 className="h-4 w-4" />
                                </button>
                            </div>
                        </div>
                    </div>
                )) : (
                    <div className="bg-white rounded-[1.5rem] border border-[#E8E6DF] p-12 text-center text-[13px] text-[#8C92AC]">
                        No articles found.
                    </div>
                )}
            </div>

            {/* Desktop View: Table */}
            <div className="hidden md:block bg-white rounded-xl border border-[#E8E6DF] overflow-hidden shadow-sm">
                <table className="w-full text-left table-fixed">
                    <thead>
                        <tr className="border-b border-[#F0EDE8]">
                            <th className="px-3 sm:px-6 py-3.5 text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC] w-[60%] sm:w-auto">Article</th>
                            <th className="px-3 sm:px-6 py-3.5 text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC] w-[15%] sm:w-auto text-center sm:text-left">Status</th>
                            <th className="px-6 py-3.5 text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC] hidden md:table-cell">Category</th>
                            <th className="px-6 py-3.5 text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC] hidden lg:table-cell">Author</th>
                            <th className="px-3 sm:px-6 py-3.5 text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC] w-[25%] sm:w-auto text-right"></th>
                        </tr>
                    </thead>
                    <tbody>
                        {posts.length > 0 ? posts.map((p) => (
                            <tr
                                key={p.id}
                                onClick={() => router.get(route('admin.posts.edit', p.id))}
                                className="border-b border-[#F0EDE8] last:border-0 hover:bg-[#FAFAF8] transition-colors group cursor-pointer"
                            >
                                <td className="px-3 sm:px-6 py-4">
                                    <div className="flex items-center gap-2 sm:gap-3 min-w-0">
                                        <div className="h-8 w-8 sm:h-10 sm:w-10 bg-[#F5F3EF] border border-[#E8E6DF] rounded-lg overflow-hidden flex-shrink-0">
                                            {p.media?.url ? (
                                                <img src={p.media.url} className="w-full h-full object-cover" />
                                            ) : (
                                                <div className="w-full h-full flex items-center justify-center text-[#C0BDBA]">
                                                    <FileText className="h-3 w-3 sm:h-4 sm:w-4" />
                                                </div>
                                            )}
                                        </div>
                                        <div className="min-w-0 truncate">
                                            <p className="text-[12px] sm:text-[13px] font-semibold text-ink leading-tight truncate">{p.title}</p>
                                            <p className="text-[9px] sm:text-[10px] text-[#8C92AC] mt-0.5 italic">
                                                {p.published_at ? new Date(p.published_at).toLocaleDateString() : 'Draft'}
                                            </p>
                                        </div>
                                    </div>
                                </td>
                                <td className="px-3 sm:px-6 py-4">
                                    <div className="flex items-center justify-center sm:justify-start gap-1.5">
                                        {p.is_published ? (
                                            <CheckCircle2 className="h-3.5 w-3.5 text-green-500" />
                                        ) : (
                                            <Circle className="h-3.5 w-3.5 text-[#C0BDBA]" />
                                        )}
                                        <span className="text-[11px] font-medium text-ink hidden sm:inline">
                                            {p.is_published ? 'Published' : 'Draft'}
                                        </span>
                                    </div>
                                </td>
                                <td className="px-6 py-4 hidden md:table-cell">
                                    <div className="flex items-center gap-1.5 text-[12px] text-ink">
                                        <Folder className="h-3.5 w-3.5 text-[#C0BDBA]" />
                                        {p.category?.name || 'Uncategorized'}
                                    </div>
                                </td>
                                <td className="px-6 py-4 hidden lg:table-cell">
                                    <div className="flex items-center gap-1.5 text-[12px] text-ink">
                                        <User className="h-3.5 w-3.5 text-[#C0BDBA]" />
                                        {p.author?.name || 'System'}
                                    </div>
                                </td>
                                <td className="px-3 sm:px-6 py-4 text-right" onClick={(e) => e.stopPropagation()}>
                                    <div className="flex items-center justify-end gap-0.5 sm:gap-1 opacity-100 md:opacity-0 md:group-hover:opacity-100 transition-opacity">
                                        <Link href={`/blog/${p.slug}`} className="p-1 text-[#8C92AC] hover:text-ink rounded transition-colors">
                                            <ExternalLink className="h-3.5 w-3.5" />
                                        </Link>
                                        <Link href={route('admin.posts.edit', p.id)} className="p-1 text-[#8C92AC] hover:text-ink rounded transition-colors">
                                            <Edit className="h-3.5 w-3.5" />
                                        </Link>
                                        <button onClick={() => deletePost(p.id)} className="p-1 text-[#8C92AC] hover:text-red-500 rounded transition-colors">
                                            <Trash2 className="h-3.5 w-3.5" />
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        )) : (
                            <tr>
                                <td colSpan={5} className="px-6 py-16 text-center text-[13px] text-[#8C92AC]">
                                    No articles yet. Start sharing your insights.
                                </td>
                            </tr>
                        )}
                    </tbody>
                </table>
            </div>
        </AdminLayout>
    );
}
