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

interface ServicesIndexProps {
    services: any;
}

export default function Index({ services }: ServicesIndexProps) {
    const [deleteId, setDeleteId] = useState<number | null>(null);

    const servicesList = services.data ? services.data : services;

    const handleDelete = () => {
        if (deleteId) {
            router.delete(`/admin/services/${deleteId}`);
        }
    };

    return (
        <AdminLayout title="Services CMS Management">
            <SEOHead title="Services Management | Tech in Soul Admin" description="Manage IT services, categories, descriptions, and icon configurations." />

            <div className="space-y-6">
                <div className="flex items-center justify-between">
                    <div>
                        <h2 className="text-xl font-bold text-white">Services List</h2>
                        <p className="text-xs text-slate-400">Manage IT services, icons, descriptions, and featured flags.</p>
                    </div>
                    <Link href="/admin/services/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" /> Add New Service
                    </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">Title</th>
                                <th className="px-6 py-4">Category</th>
                                <th className="px-6 py-4">Icon</th>
                                <th className="px-6 py-4">Featured</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">
                            {servicesList.length === 0 ? (
                                <tr>
                                    <td colSpan={6} 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 services listed yet</p>
                                            <p className="text-xs text-slate-500">Click "Add New Service" above to publish your first IT service offering.</p>
                                        </div>
                                    </td>
                                </tr>
                            ) : (
                                servicesList.map((srv: any) => (
                                    <tr key={srv.id} className="hover:bg-[#060B18]/40 transition-colors">
                                        <td className="px-6 py-4 font-bold text-white">{srv.title}</td>
                                        <td className="px-6 py-4 text-xs text-slate-400">{srv.category?.name || 'General'}</td>
                                        <td className="px-6 py-4 text-xs font-mono text-[#00F2FE]">{srv.icon || 'Code2'}</td>
                                        <td className="px-6 py-4">
                                            {srv.featured ? (
                                                <span className="text-[10px] bg-[#00F2FE]/10 text-[#00F2FE] border border-[#00F2FE]/20 px-2 py-0.5 rounded">
                                                    Featured
                                                </span>
                                            ) : (
                                                <span className="text-[10px] text-slate-500">Standard</span>
                                            )}
                                        </td>
                                        <td className="px-6 py-4">
                                            <span className={`text-[10px] font-semibold px-2.5 py-0.5 rounded-full capitalize ${
                                                srv.status === 'published' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-slate-700 text-slate-300'
                                            }`}>
                                                {srv.status}
                                            </span>
                                        </td>
                                        <td className="px-6 py-4 text-right">
                                            <div className="flex items-center justify-end gap-2">
                                                <Link
                                                    href={`/admin/services/${srv.id}/edit`}
                                                    className="inline-flex items-center justify-center p-1.5 rounded-lg text-slate-400 hover:text-[#00F2FE] hover:bg-[#00F2FE]/10 transition-colors"
                                                    title="Edit Service"
                                                >
                                                    <SquarePen className="w-4 h-4" />
                                                </Link>
                                                <button
                                                    onClick={() => setDeleteId(srv.id)}
                                                    className="inline-flex items-center justify-center p-1.5 rounded-lg text-rose-400 hover:text-rose-300 hover:bg-rose-500/10 transition-colors"
                                                    title="Delete Service"
                                                >
                                                    <Trash2 className="w-4 h-4" />
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                ))
                            )}
                        </tbody>
                    </table>

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

            <ConfirmDeleteModal
                isOpen={deleteId !== null}
                title="Delete Service Offering?"
                message="Are you sure you want to delete this service? It will be removed from public listings."
                onConfirm={handleDelete}
                onClose={() => setDeleteId(null)}
            />
        </AdminLayout>
    );
}
