"use client"

import Link from "next/link"
import Image from "next/image"
import { usePathname } from "next/navigation"
import { cn } from "@/lib/utils"
import { LayoutDashboard, Briefcase, Users, MessageSquare, Building2, FileText, HelpCircle } from "lucide-react"
import { getCurrentUser, isAdminUser } from "@/lib/auth"

const adminItems = [
    {
        title: "Dashboard",
        href: "/dashboard",
        icon: LayoutDashboard,
    },
    {
        title: "Jobs",
        href: "/dashboard/jobs",
        icon: Briefcase,
    },
    {
        title: "Candidates",
        href: "/dashboard/candidates",
        icon: Users,
    },
    {
        title: "Users",
        href: "/dashboard/users",
        icon: Users,
    },
    {
        title: "Companies",
        href: "/dashboard/companies",
        icon: Building2,
    },
    {
        title: "Backoffice",
        href: "/dashboard/backoffice",
        icon: FileText,
    },
    {
        title: "Messages",
        href: "/dashboard/messages",
        icon: MessageSquare,
    },
]

const companyItems = [
    {
        title: "Dashboard",
        href: "/dashboard",
        icon: LayoutDashboard,
    },
    {
        title: "My jobs",
        href: "/dashboard/my-jobs",
        icon: Briefcase,
    },
    {
        title: "Applications",
        href: "/dashboard/applications",
        icon: Users,
    },
]

const candidateItems = [
    {
        title: "Dashboard",
        href: "/dashboard",
        icon: LayoutDashboard,
    },
    {
        title: "Jobs",
        href: "/jobs", // Public search
        icon: Briefcase,
    },
    {
        title: "My applications",
        href: "/dashboard/my-applications",
        icon: FileText,
    },
    {
        title: "Support",
        href: "/dashboard/support/tickets",
        icon: HelpCircle,
    },
]

export function Sidebar() {
    const pathname = usePathname()
    const user = getCurrentUser()

    let items = candidateItems
    if (isAdminUser(user)) {
        items = adminItems
    } else if (user?.role === "company") {
        items = companyItems
    }

    return (
        <aside className="w-64 shrink-0 border-r border-border bg-muted/30 min-h-screen flex flex-col">
            {/* Branding Header with CORRECT Padding (pl-6) */}
            <div className="flex h-16 shrink-0 items-center px-6 gap-3 border-b border-border">
                <Link href="/" className="flex items-center gap-3 hover:opacity-80 transition-opacity">
                    <Image
                        src="/logohorse.png"
                        alt="GoHorse Jobs"
                        width={40}
                        height={40}
                        className="rounded-lg"
                    />
                    <span className="font-bold text-lg text-foreground">GoHorse Jobs</span>
                </Link>
            </div>

            {/* Navigation */}
            <nav className="flex-1 p-4 space-y-2 overflow-y-auto">
                {items.map((item) => {
                    const Icon = item.icon
                    const isActive = pathname === item.href

                    return (
                        <Link
                            key={item.href}
                            href={item.href}
                            className={cn(
                                "flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium transition-colors",
                                isActive
                                    ? "bg-primary text-primary-foreground shadow-sm"
                                    : "text-muted-foreground hover:bg-muted hover:text-foreground",
                            )}
                        >
                            <Icon className="h-5 w-5" />
                            {item.title}
                        </Link>
                    )
                })}
            </nav>

            {/* Footer / User Info could go here */}
            <div className="p-4 border-t border-border mt-auto">
                <div className="text-xs text-muted-foreground text-center">
                    v1.0.0
                </div>
            </div>
        </aside>
    )
}
