import { Head, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';
import { useState } from 'react';
import type { PaginationState } from '@/components/data-table';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { DataTable } from '@/components/data-table';
import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog';
import { PageHeader } from '@/components/page-header';
import { Button } from '@/components/ui/button';
import { useServerTable } from '@/hooks/use-server-table';
import {
    create,
    destroy,
    edit,
    reset_password,
    index as studentsIndex,
} from '@/routes/admin/users/students';
import { createStudentColumns, type Student } from './columns';

type Props = {
    students: {
        data: Student[];
        current_page: number;
        last_page: number;
        per_page: number;
        total: number;
    };
};

export default function StudentIndex({ students }: Props) {
    const [deleting, setDeleting] = useState<Student | null>(null);
    const [resetting, setResetting] = useState<Student | null>(null);

    const pagination: PaginationState = {
        current_page: students.current_page,
        last_page: students.last_page,
        per_page: students.per_page,
        total: students.total,
    };

    const {
        search,
        handlePageChange,
        handlePerPageChange,
        handleSearchChange,
    } = useServerTable({
        route: () => studentsIndex.url(),
        pagination,
    });

    function handleDelete() {
        if (!deleting) {
            return;
        }

        router.delete(destroy(deleting.id), {
            onSuccess: () => setDeleting(null),
        });
    }

    function handleResetPassword() {
        if (!resetting) {
            return;
        }

        router.patch(reset_password.url(resetting.id), {}, {
            onSuccess: () => setResetting(null),
        });
    }

    const columns = createStudentColumns({
        handleEdit: (student) => {
            router.get(edit.url(student.id));
        },
        handleDeleteClick: (student) => setDeleting(student),
        handleResetPassword: (student) => setResetting(student),
    });

    return (
        <>
            <Head title="Mahasiswa" />

            <div className="flex h-full flex-1 flex-col gap-6 overflow-x-auto p-4 md:p-6">
                <PageHeader
                    title="Mahasiswa"
                    actions={
                        <Button asChild>
                            <a href={create.url()}>
                                <Plus className="h-4 w-4" />
                                Tambah
                            </a>
                        </Button>
                    }
                />

                <DataTable
                    columns={columns}
                    data={students.data}
                    searchKey="name"
                    searchPlaceholder="Cari mahasiswa..."
                    emptyText="Belum ada data mahasiswa."
                    pagination={pagination}
                    onPageChange={handlePageChange}
                    onPerPageChange={handlePerPageChange}
                    onSearchChange={handleSearchChange}
                    searchValue={search}
                />

                <DeleteConfirmDialog
                    target={deleting}
                    onOpenChange={(open) => {
                        if (!open) {
                            setDeleting(null);
                        }
                    }}
                    title="Hapus Mahasiswa"
                    description={(student) =>
                        `Apakah Anda yakin ingin menghapus mahasiswa "${student.profile?.full_name}"? Tindakan ini tidak dapat dibatalkan.`
                    }
                    onConfirm={handleDelete}
                />

                <ConfirmDialog
                    open={resetting !== null}
                    onOpenChange={(open) => {
                        if (!open) {
                            setResetting(null);
                        }
                    }}
                    title="Reset Kata Sandi"
                    description={
                        resetting
                            ? `Apakah Anda yakin ingin mereset kata sandi mahasiswa "${resetting.profile?.full_name}" ke kata sandi default?`
                            : ''
                    }
                    confirmLabel="Reset"
                    variant="default"
                    onConfirm={handleResetPassword}
                />
            </div>
        </>
    );
}
