"use client"

import { useQueryState, parseAsString } from "nuqs"
import { useTranslations } from "next-intl"

import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/shared/components/ui/select"
import { FilterBar, FilterSearchInput } from "@/shared/components/ui/filter-bar"

export function AssignmentFilters() {
  const [search, setSearch] = useQueryState("q", parseAsString.withDefault(""))
  const [status, setStatus] = useQueryState("status", parseAsString.withDefault("all"))
  const t = useTranslations("examHomework")

  const hasFilters = Boolean(search || status !== "all")

  return (
    <FilterBar
      layout="between"
      hasFilters={hasFilters}
      onReset={() => {
        setSearch(null)
        setStatus(null)
      }}
    >
      <FilterSearchInput
        value={search}
        onChange={(v) => setSearch(v || null)}
        placeholder={t("homework.filters.searchPlaceholder")}
      />

      <div className="flex flex-wrap gap-2 w-full md:w-auto">
        <Select value={status} onValueChange={(val) => setStatus(val === "all" ? null : val)}>
          <SelectTrigger className="w-[160px] bg-background border-muted-foreground/20">
            <SelectValue placeholder={t("homework.filters.status")} />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="all">{t("homework.filters.allStatus")}</SelectItem>
            <SelectItem value="pending">{t("homework.filters.statusPending")}</SelectItem>
            <SelectItem value="submitted">{t("homework.filters.statusSubmitted")}</SelectItem>
            <SelectItem value="graded">{t("homework.filters.statusGraded")}</SelectItem>
          </SelectContent>
        </Select>
      </div>
    </FilterBar>
  )
}
