"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 ElectiveFilters() {
  const t = useTranslations("elective")
  const [search, setSearch] = useQueryState("q", parseAsString.withDefault(""))
  const [mode, setMode] = useQueryState("mode", parseAsString.withDefault("all"))

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

  return (
    <FilterBar
      layout="between"
      hasFilters={hasFilters}
      onReset={() => {
        setSearch(null)
        setMode(null)
      }}
    >
      <FilterSearchInput
        value={search}
        onChange={(v) => setSearch(v || null)}
        placeholder={t("form.namePlaceholder")}
      />

      <div className="flex flex-wrap gap-2 w-full md:w-auto">
        <Select value={mode} onValueChange={(val) => setMode(val === "all" ? null : val)}>
          <SelectTrigger className="w-[160px] bg-background border-muted-foreground/20" aria-label={t("fields.selectionMode")}>
            <SelectValue placeholder={t("fields.selectionMode")} />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="all">{t("filters.allStatuses")}</SelectItem>
            <SelectItem value="fcfs">{t("selectionMode.fcfs")}</SelectItem>
            <SelectItem value="lottery">{t("selectionMode.lottery")}</SelectItem>
          </SelectContent>
        </Select>
      </div>
    </FilterBar>
  )
}
