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, Search, X } from 'lucide-react';

interface TeamIndexProps {
    team: any;
    filters?: {
        search?: string;
    };
}

export default function Index({ team, filters = {} }: TeamIndexProps) {
    const [search, setSearch] = useState(filters.search || '');
    const [deleteId, setDeleteId] = useState<number | null>(null);

    const teamList = team.data ? team.data : team;

    const handleSearch = (e?: React.FormEvent) => {
        if (e) e.preventDefault();
        router.get('/admin/team', {
            search: search || undefined,
        }, {
            preserveState: true,
            preserveScroll: true,
        });
    };

    const handleClearSearch = () => {
        setSearch('');
        router.get('/admin/team');
    };

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

    return (
        <AdminLayout title="Team Members Management">
            <SEOHead title="Team Management | Tech in Soul Admin" description="Manage leadership, engineering profiles, designations, and bios." />

            <div className="space-y-6">
                <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                    <div>
                        <h2 className="text-xl font-bold text-white">Team Profiles</h2>
                        <p className="text-xs text-slate-400">Manage leadership and engineering team profiles.</p>
                    </div>
                    <Link href="/admin/team/create" className="btn-aqua px-4 py-2 rounded-xl text-xs font-bold flex items-center gap-1.5 self-start sm:self-auto">
                        <Plus className="w-4 h-4" /> Add Team Member
                    </Link>
                </div>

                {/* Search Bar */}
                <div className="glass-card rounded-2xl p-4 border border-slate-800">
                    <form onSubmit={handleSearch} className="flex items-center gap-3">
                        <div className="relative flex-grow">
                            <Search className="w-4 h-4 text-slate-400 absolute left-3.5 top-1/2 -translate-y-1/2" />
                            <input
                                type="text"
                                value={search}
                                onChange={(e) => setSearch(e.target.value)}
                                placeholder="Search team members by name, designation, department, or email..."
                                className="w-full bg-[#060B18] border border-slate-800 rounded-xl pl-10 pr-4 py-2 text-xs text-white placeholder-slate-500 focus:outline-none focus:border-[#00F2FE]"
                            />
                        </div>
                        <button type="submit" className="btn-aqua px-4 py-2 rounded-xl text-xs font-bold shrink-0">
                            Search
                        </button>
                        {search && (
                            <button
                                type="button"
                                onClick={handleClearSearch}
                                className="p-2 rounded-xl bg-slate-800/60 hover:bg-slate-800 text-slate-400 hover:text-white text-xs font-semibold shrink-0 transition-colors"
                                title="Clear search"
                            >
                                <X className="w-4 h-4" />
                            </button>
                        )}
                    </form>
                </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">Name</th>
                                <th className="px-6 py-4">Designation</th>
                                <th className="px-6 py-4">Department</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">
                            {teamList.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 team profiles added yet</p>
                                            <p className="text-xs text-slate-500">Click "Add Team Member" above to add your first team profile.</p>
                                        </div>
                                    </td>
                                </tr>
                            ) : (
                                teamList.map((member: any) => (
                                    <tr key={member.id} className="hover:bg-[#060B18]/40 transition-colors">
                                        <td className="px-6 py-4 font-bold text-white">{member.name}</td>
                                        <td className="px-6 py-4 text-xs font-semibold text-[#00F2FE]">{member.designation}</td>
                                        <td className="px-6 py-4 text-xs text-slate-400">{member.department || 'General'}</td>
                                        <td className="px-6 py-4">
                                            <span className={`text-[10px] font-semibold px-2.5 py-0.5 rounded-full capitalize ${
                                                member.status === 'published' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-slate-700 text-slate-300'
                                            }`}>
                                                {member.status}
                                            </span>
                                        </td>
                                        <td className="px-6 py-4 text-right">
                                            <div className="flex items-center justify-end gap-2">
                                                <Link
                                                    href={`/admin/team/${member.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 Team Member"
                                                >
                                                    <SquarePen className="w-4 h-4" />
                                                </Link>
                                                <button
                                                    onClick={() => setDeleteId(member.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 Team Member"
                                                >
                                                    <Trash2 className="w-4 h-4" />
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                ))
                            )}
                        </tbody>
                    </table>

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

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