import { Metadata } from "next";
import Link from "next/link";
import { ArrowRight, MapPin, Building, Briefcase, TrendingUp, Sparkles } from "lucide-react";
import { JobService } from "@/services/JobService";

export const dynamic = "force-dynamic";

export const metadata: Metadata = {
    title: "Browse Jobs | Jobzio",
    description: "Explore job opportunities by industry, location, or company. Find the perfect role organized by category.",
};

const INDUSTRY_ICONS: Record<string, string> = {
    "Technology & IT": "💻",
    "Business & Finance": "💼",
    Healthcare: "🏥",
    "Marketing & Sales": "📊",
    "Creative & Design": "🎨",
    Education: "📚",
    Engineering: "⚙️",
    Legal: "⚖️",
    Hospitality: "🏨",
    "Retail & Commerce": "🛒",
    Manufacturing: "🏭",
    "Energy & Utilities": "⚡",
};

export default async function BrowsePage() {
    const data = await JobService.getPopularCategories();
    const totalJobs = await JobService.countJobs();

    return (
        <div className="min-h-screen bg-gradient-to-b from-slate-50 to-white">
            {/* Hero */}
            <div className="bg-gradient-to-br from-slate-900 via-blue-900 to-indigo-900 py-16">
                <div className="container mx-auto px-4 text-center">
                    <div className="inline-flex items-center gap-2 rounded-full border border-white/10 bg-white/5 px-4 py-1.5 mb-5 backdrop-blur-sm">
                        <Sparkles className="h-4 w-4 text-amber-400" />
                        <span className="text-sm font-medium text-white/80">
                            {totalJobs.toLocaleString()} opportunities waiting
                        </span>
                    </div>
                    <h1 className="text-4xl font-extrabold text-white sm:text-5xl mb-3">
                        Browse <span className="bg-gradient-to-r from-blue-400 to-purple-400 bg-clip-text text-transparent">Jobs</span>
                    </h1>
                    <p className="text-lg text-blue-100/70 max-w-xl mx-auto">
                        Discover opportunities organized by industry, location, and company.
                    </p>
                </div>
            </div>

            <div className="container mx-auto px-4 py-12 max-w-6xl">
                {/* Browse by Industry */}
                <section className="mb-16">
                    <div className="flex items-center gap-3 mb-6">
                        <div className="flex items-center justify-center h-10 w-10 rounded-xl bg-blue-100">
                            <Briefcase className="h-5 w-5 text-blue-600" />
                        </div>
                        <div>
                            <h2 className="text-xl font-bold text-slate-900">Browse by Industry</h2>
                            <p className="text-sm text-slate-500">Find jobs in your field of expertise</p>
                        </div>
                    </div>
                    <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
                        {data.industries.map((industry) => (
                            <div
                                key={industry.id}
                                className="group rounded-2xl border border-slate-200 bg-white p-5 hover:shadow-lg hover:border-blue-200 transition-all"
                            >
                                <div className="flex items-start justify-between mb-3">
                                    <div className="flex items-center gap-3">
                                        <span className="text-2xl">{INDUSTRY_ICONS[industry.name] || "📁"}</span>
                                        <div>
                                            <h3 className="font-bold text-slate-900 group-hover:text-blue-700 transition-colors">
                                                {industry.name}
                                            </h3>
                                            <p className="text-xs text-slate-500">{industry.count} open positions</p>
                                        </div>
                                    </div>
                                    <Link
                                        href={`/jobs?industry=${encodeURIComponent(industry.name)}`}
                                        className="p-1.5 rounded-lg text-slate-400 group-hover:text-blue-600 group-hover:bg-blue-50 transition-all"
                                    >
                                        <ArrowRight className="h-4 w-4" />
                                    </Link>
                                </div>
                                {industry.categories.length > 0 && (
                                    <div className="flex flex-wrap gap-1.5 mt-3">
                                        {industry.categories.slice(0, 5).map((cat) => (
                                            <Link
                                                key={cat.id}
                                                href={`/jobs?categoryId=${cat.id}`}
                                                className="rounded-full bg-slate-50 px-2.5 py-1 text-[10px] font-medium text-slate-600 hover:bg-blue-50 hover:text-blue-700 transition-colors"
                                            >
                                                {cat.name}
                                                {cat.count > 0 && (
                                                    <span className="text-slate-400 ml-1">({cat.count})</span>
                                                )}
                                            </Link>
                                        ))}
                                    </div>
                                )}
                            </div>
                        ))}
                    </div>
                </section>

                {/* Browse by Location */}
                <section className="mb-16">
                    <div className="flex items-center gap-3 mb-6">
                        <div className="flex items-center justify-center h-10 w-10 rounded-xl bg-orange-100">
                            <MapPin className="h-5 w-5 text-orange-600" />
                        </div>
                        <div>
                            <h2 className="text-xl font-bold text-slate-900">Popular Locations</h2>
                            <p className="text-sm text-slate-500">Browse jobs by city</p>
                        </div>
                    </div>
                    <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-3">
                        {data.topCities.map((city) => (
                            <Link
                                key={city.city}
                                href={`/jobs?l=${encodeURIComponent(city.city)}`}
                                className="group flex items-center gap-3 rounded-xl border border-slate-200 bg-white p-4 hover:shadow-md hover:border-orange-200 transition-all"
                            >
                                <MapPin className="h-4 w-4 text-orange-400 shrink-0" />
                                <div className="min-w-0">
                                    <p className="text-sm font-semibold text-slate-800 truncate group-hover:text-orange-700">
                                        {city.city}
                                    </p>
                                    <p className="text-[10px] text-slate-400">{city.count} jobs</p>
                                </div>
                            </Link>
                        ))}
                    </div>
                </section>

                {/* Featured Employers */}
                {data.featuredEmployers.length > 0 && (
                    <section className="mb-16">
                        <div className="flex items-center gap-3 mb-6">
                            <div className="flex items-center justify-center h-10 w-10 rounded-xl bg-emerald-100">
                                <Building className="h-5 w-5 text-emerald-600" />
                            </div>
                            <div>
                                <h2 className="text-xl font-bold text-slate-900">Featured Employers</h2>
                                <p className="text-sm text-slate-500">Top employers hiring now</p>
                            </div>
                        </div>
                        <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-4">
                            {data.featuredEmployers.map((employer) => (
                                <div
                                    key={employer.id}
                                    className="group rounded-xl border border-slate-200 bg-white p-5 text-center hover:shadow-md hover:border-emerald-200 transition-all"
                                >
                                    <div className="w-14 h-14 rounded-xl bg-gradient-to-br from-slate-100 to-slate-50 mx-auto mb-3 flex items-center justify-center text-2xl font-bold text-slate-400 overflow-hidden">
                                        {employer.logo ? (
                                            <img
                                                src={employer.logo}
                                                alt={employer.name}
                                                className="w-full h-full object-cover rounded-xl"
                                            />
                                        ) : (
                                            employer.name.charAt(0).toUpperCase()
                                        )}
                                    </div>
                                    <h3 className="text-sm font-bold text-slate-800 group-hover:text-emerald-700 transition-colors truncate">
                                        {employer.name}
                                    </h3>
                                    {employer.industry && (
                                        <p className="text-[10px] text-slate-400 mt-0.5 truncate">
                                            {employer.industry}
                                        </p>
                                    )}
                                    <p className="mt-2 inline-flex items-center gap-1 rounded-full bg-emerald-50 px-2.5 py-0.5 text-[10px] font-semibold text-emerald-700">
                                        <TrendingUp className="h-3 w-3" />
                                        {employer.openPositions} open positions
                                    </p>
                                </div>
                            ))}
                        </div>
                    </section>
                )}

                {/* CTA */}
                <section className="rounded-2xl bg-gradient-to-r from-blue-600 to-indigo-600 p-8 sm:p-12 text-center">
                    <h2 className="text-2xl font-bold text-white mb-3">
                        Can&apos;t find what you&apos;re looking for?
                    </h2>
                    <p className="text-blue-100/80 mb-6 max-w-md mx-auto">
                        Use our advanced search to find jobs by keyword, skills, salary range, and more.
                    </p>
                    <Link
                        href="/jobs"
                        className="inline-flex items-center gap-2 rounded-xl bg-white px-6 py-3 text-sm font-bold text-blue-700 hover:bg-blue-50 transition-colors shadow-lg"
                    >
                        Search All Jobs
                        <ArrowRight className="h-4 w-4" />
                    </Link>
                </section>
            </div>
        </div>
    );
}
