"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 ExamFilters() {
  const [search, setSearch] = useQueryState("q", parseAsString.withOptions({ shallow: false }))
  const [status, setStatus] = useQueryState("status", parseAsString.withOptions({ shallow: false }))
  const [difficulty, setDifficulty] = useQueryState("difficulty", parseAsString.withOptions({ shallow: false }))

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

  return (
    <FilterBar
      hasFilters={hasFilters}
      onReset={() => {
        setSearch(null)
        setStatus(null)
        setDifficulty(null)
      }}
    >
      <FilterSearchInput
        value={search || ""}
        onChange={(v) => setSearch(v || null)}
        placeholder="Search exams..."
      />

      <div className="flex flex-wrap gap-2 w-full md:w-auto">
        <Select value={status || "all"} onValueChange={(val) => setStatus(val === "all" ? null : val)}>
          <SelectTrigger className="w-[140px] bg-background border-muted-foreground/20">
            <SelectValue placeholder="Status" />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="all">Any Status</SelectItem>
            <SelectItem value="draft">Draft</SelectItem>
            <SelectItem value="published">Published</SelectItem>
            <SelectItem value="archived">Archived</SelectItem>
          </SelectContent>
        </Select>

        <Select value={difficulty || "all"} onValueChange={(val) => setDifficulty(val === "all" ? null : val)}>
          <SelectTrigger className="w-[140px] bg-background border-muted-foreground/20">
            <SelectValue placeholder="Difficulty" />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="all">Any Difficulty</SelectItem>
            <SelectItem value="1">Easy (1)</SelectItem>
            <SelectItem value="2">Easy-Med (2)</SelectItem>
            <SelectItem value="3">Medium (3)</SelectItem>
            <SelectItem value="4">Med-Hard (4)</SelectItem>
            <SelectItem value="5">Hard (5)</SelectItem>
          </SelectContent>
        </Select>
      </div>
    </FilterBar>
  )
}
