import { Head, Link, router } from '@inertiajs/react';
import AdminLayout from '@/Layouts/AdminLayout';
import Card from '@/Components/UI/Card';
import Badge from '@/Components/UI/Badge';
import Button from '@/Components/UI/Button';

interface CourseRow {
    id: number;
    title: string;
    code: string | null;
    status: string;
    programme: { title: string } | null;
    facilitator: { name: string } | null;
}

export default function Index({ courses }: { courses: CourseRow[] }) {
    const destroy = (course: CourseRow) => {
        if (confirm(`Remove "${course.title}"? This cannot be undone.`)) {
            router.delete(route('admin.courses.destroy', course.id));
        }
    };

    return (
        <AdminLayout>
            <Head title="Courses" />

            <div className="flex items-center justify-between">
                <div>
                    <h1 className="text-institute-title text-on-surface">Courses</h1>
                    <p className="text-body-md text-on-surface-variant">
                        Required courses assigned to active Programmes.
                    </p>
                </div>
                <Button href={route('admin.courses.create')} variant="action">
                    + New Course
                </Button>
            </div>

            <Card className="mt-6 overflow-x-auto p-0">
                <table className="w-full text-left text-sm">
                    <thead className="border-b border-outline-variant text-on-surface-variant">
                        <tr>
                            <th className="px-6 py-3 font-medium">Code</th>
                            <th className="px-6 py-3 font-medium">Title</th>
                            <th className="px-6 py-3 font-medium">Programme</th>
                            <th className="px-6 py-3 font-medium">Facilitator</th>
                            <th className="px-6 py-3 font-medium">Status</th>
                            <th className="px-6 py-3 font-medium">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {courses.map((course) => (
                            <tr key={course.id} className="border-b border-outline-variant last:border-0">
                                <td className="px-6 py-3 text-on-surface-variant">{course.code}</td>
                                <td className="px-6 py-3 font-medium text-on-surface">{course.title}</td>
                                <td className="px-6 py-3 text-on-surface-variant">{course.programme?.title ?? '—'}</td>
                                <td className="px-6 py-3 text-on-surface-variant">{course.facilitator?.name ?? '—'}</td>
                                <td className="px-6 py-3">
                                    <Badge tone={course.status === 'published' ? 'published' : 'draft'}>
                                        {course.status}
                                    </Badge>
                                </td>
                                <td className="px-6 py-3">
                                    <div className="flex gap-3">
                                        <Link
                                            href={route('admin.courses.edit', course.id)}
                                            className="text-jgcp-navy hover:underline"
                                        >
                                            Edit
                                        </Link>
                                        <button onClick={() => destroy(course)} className="text-error hover:underline">
                                            Delete
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        ))}
                    </tbody>
                </table>
            </Card>
        </AdminLayout>
    );
}
