"use client";

import { SearchX, ArrowRight, SlidersHorizontal, MapPin, Banknote, Sparkles } from "lucide-react";
import Link from "next/link";
import { useSearchParams, useRouter } from "next/navigation";

export function EmptyState({ query, hasFilters }: { query?: string; hasFilters: boolean }) {
    const searchParams = useSearchParams();
    const router = useRouter();

    // Generate smart suggestions based on active filters
    const smartSuggestions: { label: string; action: () => void; icon: React.ReactNode }[] = [];

    const location = searchParams.get("l");
    if (location) {
        smartSuggestions.push({
            label: `Remove location filter "${location}"`,
            icon: <MapPin className="h-3.5 w-3.5 text-orange-500" />,
            action: () => {
                const params = new URLSearchParams(searchParams.toString());
                params.delete("l");
                params.delete("page");
                router.push(`/jobs?${params.toString()}`);
            },
        });
    }

    const salaryMin = searchParams.get("salaryMin");
    if (salaryMin && Number(salaryMin) > 0) {
        smartSuggestions.push({
            label: `Lower salary minimum from ₦${Number(salaryMin).toLocaleString()}`,
            icon: <Banknote className="h-3.5 w-3.5 text-emerald-500" />,
            action: () => {
                const params = new URLSearchParams(searchParams.toString());
                params.delete("salaryMin");
                params.delete("page");
                router.push(`/jobs?${params.toString()}`);
            },
        });
    }

    const types = searchParams.get("type")?.split(",").filter(Boolean);
    if (types && types.length > 1) {
        smartSuggestions.push({
            label: `Remove some job type filters (${types.length} active)`,
            icon: <SlidersHorizontal className="h-3.5 w-3.5 text-blue-500" />,
            action: () => {
                const params = new URLSearchParams(searchParams.toString());
                params.delete("type");
                params.delete("page");
                router.push(`/jobs?${params.toString()}`);
            },
        });
    }

    const experience = searchParams.get("experience")?.split(",").filter(Boolean);
    if (experience && experience.length > 0) {
        smartSuggestions.push({
            label: "Remove experience level filter",
            icon: <SlidersHorizontal className="h-3.5 w-3.5 text-purple-500" />,
            action: () => {
                const params = new URLSearchParams(searchParams.toString());
                params.delete("experience");
                params.delete("page");
                router.push(`/jobs?${params.toString()}`);
            },
        });
    }

    // Auto-adjust: remove all non-query filters
    const autoAdjust = () => {
        const params = new URLSearchParams();
        const q = searchParams.get("q");
        if (q) params.set("q", q);
        router.push(`/jobs?${params.toString()}`);
    };

    return (
        <div className="flex flex-col items-center justify-center py-16 px-8 text-center">
            <div className="relative mb-6">
                <div className="flex h-20 w-20 items-center justify-center rounded-2xl bg-gradient-to-br from-slate-100 to-slate-50">
                    <SearchX className="h-10 w-10 text-slate-300" />
                </div>
                <div className="absolute -bottom-1 -right-1 flex h-8 w-8 items-center justify-center rounded-full bg-amber-100 text-sm">
                    😕
                </div>
            </div>

            <h3 className="text-xl font-bold text-slate-900 mb-2">
                {query ? `No jobs found for "${query}"` : "No jobs match your filters"}
            </h3>

            <p className="text-sm text-slate-500 max-w-md mb-6">
                {hasFilters
                    ? "Try adjusting your filters or broadening your search to see more results."
                    : "There are no published jobs at the moment. Check back soon!"}
            </p>

            <div className="flex flex-col sm:flex-row gap-3">
                {hasFilters && (
                    <>
                        <Link
                            href="/jobs"
                            className="inline-flex items-center gap-2 rounded-xl bg-slate-900 px-6 py-2.5 text-sm font-bold text-white hover:bg-slate-800 transition-colors"
                        >
                            Clear all filters
                        </Link>
                        {smartSuggestions.length > 0 && (
                            <button
                                onClick={autoAdjust}
                                className="inline-flex items-center gap-2 rounded-xl bg-gradient-to-r from-blue-500 to-purple-500 px-6 py-2.5 text-sm font-bold text-white hover:from-blue-600 hover:to-purple-600 transition-all shadow-lg shadow-blue-500/20"
                            >
                                <Sparkles className="h-4 w-4" />
                                Auto-adjust filters
                            </button>
                        )}
                    </>
                )}
                <Link
                    href="/jobs"
                    className="inline-flex items-center gap-2 rounded-xl border border-slate-200 bg-white px-6 py-2.5 text-sm font-semibold text-slate-700 hover:bg-slate-50 transition-colors"
                >
                    Browse all jobs
                    <ArrowRight className="h-4 w-4" />
                </Link>
            </div>

            {/* Smart Suggestions */}
            {smartSuggestions.length > 0 && (
                <div className="mt-8 rounded-xl bg-gradient-to-br from-blue-50 to-indigo-50 border border-blue-100 p-6 max-w-md w-full">
                    <p className="text-xs font-semibold text-blue-600 uppercase tracking-wide mb-3 flex items-center gap-1.5">
                        <Sparkles className="h-3.5 w-3.5" />
                        Smart Suggestions
                    </p>
                    <ul className="space-y-2">
                        {smartSuggestions.map((suggestion, i) => (
                            <li key={i}>
                                <button
                                    onClick={suggestion.action}
                                    className="flex items-center gap-3 w-full rounded-lg px-3 py-2 text-sm text-left text-slate-700 hover:bg-white/70 transition-colors"
                                >
                                    {suggestion.icon}
                                    <span className="underline decoration-slate-300 underline-offset-2">
                                        {suggestion.label}
                                    </span>
                                </button>
                            </li>
                        ))}
                    </ul>
                </div>
            )}

            {/* General Suggestions */}
            <div className="mt-6 rounded-xl bg-slate-50 border border-slate-100 p-6 max-w-md w-full">
                <p className="text-xs font-semibold text-slate-500 uppercase tracking-wide mb-3">
                    General Tips
                </p>
                <ul className="space-y-2 text-sm text-slate-600 text-left">
                    <li className="flex items-start gap-2">
                        <span className="text-blue-500 mt-0.5">•</span>
                        Try different or fewer keywords
                    </li>
                    <li className="flex items-start gap-2">
                        <span className="text-blue-500 mt-0.5">•</span>
                        Check for typos in your search terms
                    </li>
                    <li className="flex items-start gap-2">
                        <span className="text-blue-500 mt-0.5">•</span>
                        Use more general keywords like &quot;developer&quot; instead of &quot;React developer&quot;
                    </li>
                </ul>
            </div>
        </div>
    );
}
