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 {
    index as lecturersIndex,
    create,
    edit,
    destroy,
    reset_password,
} from '@/routes/admin/users/lecturers';
import type { Lecturer } from './columns';
import { createLecturerColumns } from './columns';

type Props = {
    lecturers: {
        data: Lecturer[];
        current_page: number;
        last_page: number;
        per_page: number;
        total: number;
    };
};

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

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

    const {
        search,
        handlePageChange,
        handlePerPageChange,
        handleSearchChange,
    } = useServerTable({
        route: () => lecturersIndex.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 = createLecturerColumns({
        handleEdit: (lecturer) => {
            router.get(edit.url(lecturer.id));
        },
        handleDeleteClick: (lecturer) => setDeleting(lecturer),
        handleResetPassword: (lecturer) => setResetting(lecturer),
    });

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

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

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

                <DeleteConfirmDialog
                    target={deleting}
                    onOpenChange={(open) => {
                        if (!open) {
                            setDeleting(null);
                        }
                    }}
                    title="Hapus Dosen"
                    description={(lecturer) =>
                        `Apakah Anda yakin ingin menghapus dosen "${lecturer.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 dosen "${resetting.profile?.full_name}" ke kata sandi default?`
                            : ''
                    }
                    confirmLabel="Reset"
                    variant="default"
                    onConfirm={handleResetPassword}
                />
            </div>
        </>
    );
}
