"use client"

import { useQueryState, parseAsString } from "nuqs"

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 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="Search assignments..."
      />

      <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="Status" />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="all">All Status</SelectItem>
            <SelectItem value="pending">Pending</SelectItem>
            <SelectItem value="submitted">Submitted</SelectItem>
            <SelectItem value="graded">Graded</SelectItem>
          </SelectContent>
        </Select>
      </div>
    </FilterBar>
  )
}
