"use client";

import { Search, Filter, X } from "lucide-react";
import type { LogFilters } from "./AdminActivityLogsDashboard";

type Props = {
    filters: LogFilters;
    setFilters: React.Dispatch<React.SetStateAction<LogFilters>>;
    onFilterChange?: (newFilters: LogFilters) => void;
    onClear?: () => void;
};

export default function ActivitySearchFilter({ filters, setFilters, onFilterChange, onClear }: Props) {
    const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        const newFilters = { ...filters, search: e.target.value };
        setFilters(newFilters);
        if (onFilterChange) onFilterChange(newFilters);
    };

    const handleSelectChange = (key: string, value: string) => {
        const newFilters = { ...filters, [key]: value };
        setFilters(newFilters);
        if (onFilterChange) onFilterChange(newFilters);
    };

    const clearFilters = () => {
        if (onClear) {
            onClear();
        } else {
            setFilters({
                search: "",
                action: "all",
                admin_id: "all",
                timeRange: "30d",
            });
        }
    };

    return (
        <div className="bg-white rounded-xl shadow-sm border border-slate-200 p-5">
            <div className="flex items-center justify-between mb-4">
                <h3 className="font-bold text-slate-800 flex items-center gap-2">
                    <Filter className="w-4 h-4 text-slate-500" />
                    Search & Filter
                </h3>
            </div>

            <div className="space-y-4">
                <div>
                    <label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-2">Search Logs</label>
                    <div className="relative">
                        <Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400" />
                        <input
                            type="text"
                            placeholder="Search actions, resources, IDs..."
                            value={filters.search}
                            onChange={handleChange}
                            className="w-full pl-9 pr-4 py-2 bg-slate-50 border border-slate-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500/50 focus:border-indigo-500 transition-all placeholder:text-slate-400"
                        />
                    </div>
                </div>

                <div>
                    <label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-2">Event Category</label>
                    <select
                        value={filters.action}
                        onChange={(e) => handleSelectChange('action', e.target.value)}
                        className="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-lg text-sm text-slate-700 outline-none focus:border-indigo-500 transition-colors"
                    >
                        <option value="all">All Events</option>
                        <option value="auth">Authentication</option>
                        <option value="job">Job Moderation</option>
                        <option value="user">User Management</option>
                        <option value="system">System Settings</option>
                    </select>
                </div>

                <div>
                    <label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-2">Date Range</label>
                    <select
                        value={filters.timeRange}
                        onChange={(e) => handleSelectChange('timeRange', e.target.value)}
                        className="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-lg text-sm text-slate-700 outline-none focus:border-indigo-500 transition-colors"
                    >
                        <option value="1h">Last 1 Hour</option>
                        <option value="24h">Last 24 Hours</option>
                        <option value="7d">Last 7 Days</option>
                        <option value="30d">Last 30 Days</option>
                        <option value="all">All Time</option>
                    </select>
                </div>

                <div className="pt-2 border-t border-slate-100">
                    <button
                        onClick={clearFilters}
                        className="w-full py-2 flex items-center justify-center gap-2 text-sm font-medium text-slate-500 hover:text-slate-800 hover:bg-slate-100 rounded-lg transition-colors"
                    >
                        <X className="w-4 h-4" />
                        Clear All Filters
                    </button>
                </div>
            </div>
        </div>
    );
}
