import React from 'react';
import { Link } from '@inertiajs/react';
import { AdminLayout } from '../AdminLayout';
import { SEOHead } from '@/Components/SEOHead';
import { Pagination } from '@/Components/Pagination';
import { Plus, SquarePen, Layers } from 'lucide-react';

interface PagesIndexProps {
    pages: any;
}

export default function Index({ pages }: PagesIndexProps) {
    const pagesList = pages.data ? pages.data : pages;

    return (
        <AdminLayout title="CMS Pages & Page Builder">
            <SEOHead title="CMS Pages & Builder | Tech in Soul Admin" description="Manage static and legal website pages, SEO metadata, and section builders." />

            <div className="space-y-6">
                <div className="flex items-center justify-between">
                    <div>
                        <h2 className="text-xl font-bold text-white">Dynamic Website Pages</h2>
                        <p className="text-xs text-slate-400">Manage site architecture and interactive section builders.</p>
                    </div>
                    <Link href="/admin/pages/create" className="btn-aqua px-4 py-2 rounded-xl text-xs font-bold flex items-center gap-1.5">
                        <Plus className="w-4 h-4" /> Create New Page
                    </Link>
                </div>

                <div className="glass-card rounded-2xl overflow-hidden border border-slate-800">
                    <table className="w-full text-left text-sm text-slate-300">
                        <thead className="bg-[#060B18] text-xs font-bold text-slate-400 uppercase border-b border-slate-800">
                            <tr>
                                <th className="px-6 py-4">Page Title</th>
                                <th className="px-6 py-4">URL Slug</th>
                                <th className="px-6 py-4">Sections</th>
                                <th className="px-6 py-4">Status</th>
                                <th className="px-6 py-4 text-right">Actions</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-slate-800/80">
                            {pagesList.length === 0 ? (
                                <tr>
                                    <td colSpan={5} className="px-6 py-12 text-center text-slate-500">
                                        <div className="flex flex-col items-center gap-2">
                                            <p className="text-sm font-semibold text-slate-400">No CMS pages created yet</p>
                                            <p className="text-xs text-slate-500">Click "Create New Page" above to build your first dynamic page.</p>
                                        </div>
                                    </td>
                                </tr>
                            ) : (
                                pagesList.map((page: any) => (
                                    <tr key={page.id} className="hover:bg-[#060B18]/40 transition-colors">
                                        <td className="px-6 py-4 font-bold text-white flex items-center gap-2">
                                            {page.title}
                                            {page.is_system && (
                                                <span className="text-[10px] bg-cyan-500/10 text-[#00F2FE] border border-cyan-500/20 px-2 py-0.5 rounded">
                                                    System
                                                </span>
                                            )}
                                        </td>
                                        <td className="px-6 py-4 text-xs font-mono text-[#00F2FE]">/{page.slug}</td>
                                        <td className="px-6 py-4 text-xs text-slate-400">{page.sections_count || 0} Section(s)</td>
                                        <td className="px-6 py-4">
                                            <span className={`text-[10px] font-semibold px-2.5 py-0.5 rounded-full capitalize ${
                                                page.status === 'published' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-slate-700 text-slate-300'
                                            }`}>
                                                {page.status}
                                            </span>
                                        </td>
                                        <td className="px-6 py-4 text-right">
                                            <div className="flex items-center justify-end gap-2">
                                                <Link
                                                    href={`/admin/pages/${page.id}/builder`}
                                                    className="px-3 py-1.5 rounded-lg bg-[#00F2FE]/10 text-[#00F2FE] border border-[#00F2FE]/30 text-xs font-semibold hover:bg-[#00F2FE]/20 transition-colors inline-flex items-center gap-1"
                                                >
                                                    <Layers className="w-3.5 h-3.5" /> Builder
                                                </Link>
                                                <Link
                                                    href={`/admin/pages/${page.id}/edit`}
                                                    className="px-3 py-1.5 rounded-lg bg-slate-800 text-slate-300 text-xs font-semibold hover:bg-slate-700 transition-colors inline-flex items-center gap-1"
                                                >
                                                    <SquarePen className="w-3.5 h-3.5" /> Edit
                                                </Link>
                                            </div>
                                        </td>
                                    </tr>
                                ))
                            )}
                        </tbody>
                    </table>

                    {pages.links && (
                        <Pagination links={pages.links} from={pages.from} to={pages.to} total={pages.total} />
                    )}
                </div>
            </div>
        </AdminLayout>
    );
}
