"use client"

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

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

interface AuditLogFiltersProps {
  moduleOptions: string[]
}

export function AuditLogFilters({ moduleOptions }: AuditLogFiltersProps) {
  const t = useTranslations("audit")
  const [module, setModule] = useQueryState("module", parseAsString.withOptions({ shallow: false }))
  const [action, setAction] = useQueryState("action", parseAsString.withOptions({ shallow: false }))
  const [status, setStatus] = useQueryState("status", parseAsString.withOptions({ shallow: false }))
  const [userId, setUserId] = useQueryState("userId", parseAsString.withOptions({ shallow: false }))
  const [startDate, setStartDate] = useQueryState(
    "startDate",
    parseAsString.withOptions({ shallow: false }),
  )
  const [endDate, setEndDate] = useQueryState(
    "endDate",
    parseAsString.withOptions({ shallow: false }),
  )

  const hasFilters = Boolean(module || action || status || userId || startDate || endDate)

  return (
    <FilterBar
      layout="wrap"
      hasFilters={hasFilters}
      onReset={() => {
        setModule(null)
        setAction(null)
        setStatus(null)
        setUserId(null)
        setStartDate(null)
        setEndDate(null)
      }}
    >
      <Select value={module || "all"} onValueChange={(val) => setModule(val === "all" ? null : val)}>
        <SelectTrigger className="w-[160px] bg-background" aria-label={t("filter.modulePlaceholder")}>
          <SelectValue placeholder={t("filter.modulePlaceholder")} />
        </SelectTrigger>
        <SelectContent>
          <SelectItem value="all">{t("filter.anyModule")}</SelectItem>
          {moduleOptions.map((m) => (
            <SelectItem key={m} value={m}>
              {m}
            </SelectItem>
          ))}
        </SelectContent>
      </Select>

      <Input
        placeholder={t("filter.actionPlaceholder")}
        className="w-full md:w-[180px] bg-background"
        aria-label={t("filter.actionPlaceholder")}
        value={action || ""}
        onChange={(e) => setAction(e.target.value || null)}
      />

      <Input
        placeholder={t("filter.userIdPlaceholder")}
        className="w-full md:w-[180px] bg-background"
        aria-label={t("filter.userIdPlaceholder")}
        value={userId || ""}
        onChange={(e) => setUserId(e.target.value || null)}
      />

      <Select value={status || "all"} onValueChange={(val) => setStatus(val === "all" ? null : val)}>
        <SelectTrigger className="w-[140px] bg-background" aria-label={t("filter.statusPlaceholder")}>
          <SelectValue placeholder={t("filter.statusPlaceholder")} />
        </SelectTrigger>
        <SelectContent>
          <SelectItem value="all">{t("filter.anyStatus")}</SelectItem>
          <SelectItem value="success">{t("filter.success")}</SelectItem>
          <SelectItem value="failure">{t("filter.failure")}</SelectItem>
        </SelectContent>
      </Select>

      <Input
        type="date"
        className="w-full md:w-[160px] bg-background"
        aria-label={t("filter.startDate")}
        value={startDate || ""}
        onChange={(e) => setStartDate(e.target.value || null)}
      />
      <Input
        type="date"
        className="w-full md:w-[160px] bg-background"
        aria-label={t("filter.endDate")}
        value={endDate || ""}
        onChange={(e) => setEndDate(e.target.value || null)}
      />
    </FilterBar>
  )
}
