import { Search, X } from 'lucide-react';
import { useState } from 'react';
import type { ReactNode } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
} from '@/components/ui/select';

/** Radix Select items cannot have an empty value; this one stands for "all". */
const ALL = 'all';

type FiltersProps = {
    /** Shows the "clear" button. */
    active: boolean;
    onReset: () => void;
    children: ReactNode;
};

/** The row of filter controls above an admin list. */
export function ListFilters({ active, onReset, children }: FiltersProps) {
    return (
        <div className="flex flex-wrap items-center gap-2">
            {children}
            {active && (
                <Button
                    type="button"
                    variant="ghost"
                    size="sm"
                    onClick={onReset}
                >
                    <X />
                    Filtreleri temizle
                </Button>
            )}
        </div>
    );
}

type SearchProps = {
    /** The search that is currently applied. */
    value: string;
    onSearch: (text: string) => void;
    /** Says what is searched, e.g. "Ad, telefon ya da e-posta ara". */
    label: string;
};

/**
 * Text search of a list; applied on Enter or with the button. Give it
 * `key={value}` so the box follows filters that change elsewhere.
 */
export function SearchFilter({ value, onSearch, label }: SearchProps) {
    const [text, setText] = useState(value);

    return (
        <form
            role="search"
            className="flex items-center gap-2"
            onSubmit={(event) => {
                event.preventDefault();
                onSearch(text.trim());
            }}
        >
            <div className="relative">
                <Search
                    aria-hidden="true"
                    className="pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground"
                />
                <Input
                    type="search"
                    value={text}
                    onChange={(event) => setText(event.target.value)}
                    placeholder={label}
                    aria-label={label}
                    className="h-8 w-64 pl-8"
                />
            </div>
            <Button type="submit" variant="secondary" size="sm">
                Ara
            </Button>
        </form>
    );
}

type SelectProps = {
    /** The selected value; '' selects everything. */
    value: string;
    onChange: (value: string) => void;
    /** Accessible name of the filter, e.g. "Durum". */
    label: string;
    /** Text of the option that selects everything, e.g. "Tüm durumlar". */
    allLabel: string;
    options: { value: string; label: string }[];
};

/** A drop-down filter of a list; applied as soon as an option is chosen. */
export function SelectFilter({
    value,
    onChange,
    label,
    allLabel,
    options,
}: SelectProps) {
    return (
        <Select
            value={value === '' ? ALL : value}
            onValueChange={(next) => onChange(next === ALL ? '' : next)}
        >
            <SelectTrigger size="sm" aria-label={label} className="min-w-40">
                <SelectValue />
            </SelectTrigger>
            <SelectContent>
                <SelectItem value={ALL}>{allLabel}</SelectItem>
                {options.map((option) => (
                    <SelectItem key={option.value} value={option.value}>
                        {option.label}
                    </SelectItem>
                ))}
            </SelectContent>
        </Select>
    );
}
