"use client";

import { type ReactNode, useState, useEffect } from "react";
import { useStudents, useStudentFilter } from "@/hooks/use-students";
import {
  PageHeader,
  PaperCard,
  Input,
  Select,
  Badge,
  LoadingState,
  ErrorState,
  EmptyState,
  Table,
  TableRow,
  TableCell,
  Pagination,
} from "@/components/ui";
import { t } from "@/lib/i18n";

export default function StudentsPage(): ReactNode {
  const { filter, setPage, setSearch, setStatus } = useStudentFilter();
  const { data, loading, error } = useStudents(filter);
  const [searchInput, setSearchInput] = useState("");

  useEffect(() => {
    const timer = setTimeout(() => setSearch(searchInput), 300);
    return () => clearTimeout(timer);
  }, [searchInput, setSearch]);

  return (
    <div style={{ padding: 32, maxWidth: 1200, margin: "0 auto" }}>
      <PageHeader
        title={t("admin.students.title")}
        description="全校学生一览"
      />

      <PaperCard className="p-4 mb-4">
        <div className="flex gap-3 items-center">
          <Input
            type="search"
            placeholder={t("admin.common.search")}
            value={searchInput}
            onChange={(e) => setSearchInput(e.target.value)}
            style={{ width: 240 }}
          />
          <Select
            value={filter.status ?? "all"}
            onChange={(e) => setStatus(e.target.value)}
          >
            <option value="all">{t("admin.common.all")}</option>
            <option value="active">{t("admin.common.active")}</option>
            <option value="disabled">{t("admin.common.disabled")}</option>
          </Select>
        </div>
      </PaperCard>

      <PaperCard className="p-4">
        {loading && <LoadingState />}
        {error && <ErrorState message={error.message} />}
        {!loading && !error && data && (
          <>
            {data.items.length === 0 ? (
              <EmptyState message={t("admin.common.empty")} />
            ) : (
              <>
                <Table
                  headers={[
                    t("admin.common.email"),
                    t("admin.common.name"),
                    t("admin.common.class"),
                    t("admin.common.grade"),
                    t("admin.common.school"),
                    t("admin.students.guardian"),
                    t("admin.students.guardianPhone"),
                    t("admin.common.status"),
                  ]}
                >
                  {data.items.map((student) => (
                    <TableRow key={student.id}>
                      <TableCell>{student.email}</TableCell>
                      <TableCell>{student.name}</TableCell>
                      <TableCell>{student.className}</TableCell>
                      <TableCell>{student.gradeName}</TableCell>
                      <TableCell>{student.schoolName}</TableCell>
                      <TableCell>{student.guardianName ?? "—"}</TableCell>
                      <TableCell
                        style={{ fontFamily: "var(--font-mono)", fontSize: 12 }}
                      >
                        {student.guardianPhone ?? "—"}
                      </TableCell>
                      <TableCell>
                        <Badge status={student.status} />
                      </TableCell>
                    </TableRow>
                  ))}
                </Table>
                <Pagination
                  page={data.page}
                  pageSize={data.pageSize}
                  total={data.total}
                  hasNext={data.hasNext}
                  onPageChange={setPage}
                />
              </>
            )}
          </>
        )}
      </PaperCard>
    </div>
  );
}
