'use client'

import { useSession } from 'next-auth/react'
import { handleLogout } from '@/lib/logout'
import { Avatar, AvatarFallback } from '@/components/ui/avatar'
import { Button } from '@/components/ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { Badge } from '@/components/ui/badge'
import { User, Settings, LogOut, Shield } from 'lucide-react'
import Link from 'next/link'

export default function UserMenu() {
  const { data: session } = useSession()

  // A session without a user means the account was deactivated or its SSO access removed.
  if (!session?.user) return null

  const getRoleColor = (role: string) => {
    const colors = {
      'SUPER_ADMIN': 'bg-primary text-primary-foreground border-transparent',
      'UPPER_MANAGEMENT': 'bg-primary/85 text-primary-foreground border-transparent',
      'MANAGER': 'bg-primary/10 text-primary border-primary/20',
      'UNIT_LEAD': 'bg-muted text-foreground border-border',
      'MEMBER': 'bg-transparent text-muted-foreground border-border'
    }
    return colors[role as keyof typeof colors] || 'bg-muted text-muted-foreground border-border'
  }

  const getRoleIcon = (role: string) => {
    if (role === 'SUPER_ADMIN') {
      return <Shield className="h-3 w-3" />
    }
    return null
  }

  const handleSignOut = async () => {
    await handleLogout()
  }

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="ghost" className="relative h-10 w-10 rounded-full">
          <Avatar className="h-10 w-10">
            <AvatarFallback className="bg-avatar text-avatar-foreground">
              {session.user.name?.charAt(0)?.toUpperCase() || 'U'}
            </AvatarFallback>
          </Avatar>
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent className="w-80" align="end" forceMount>
        <DropdownMenuLabel className="font-normal">
          <div className="flex flex-col space-y-2">
            <div className="flex items-center gap-2">
              <p className="text-sm font-medium leading-none">
                {session.user.name}
              </p>
              <Badge variant="outline" className={`text-xs ${getRoleColor(session.user.role)}`}>
                <div className="flex items-center gap-1">
                  {getRoleIcon(session.user.role)}
                  {session.user.role.replace('_', ' ')}
                </div>
              </Badge>
            </div>
            <p className="text-xs leading-none text-muted-foreground">
              {session.user.email}
            </p>
            <div className="text-xs text-muted-foreground">
              <p>Position: {session.user.position}</p>
              <p>Department: {session.user.department}</p>
            </div>
          </div>
        </DropdownMenuLabel>
        <DropdownMenuSeparator />
        <DropdownMenuItem asChild>
          <Link href="/dashboard/profile" className="flex items-center">
            <User className="mr-2 h-4 w-4" />
            <span>Profile</span>
          </Link>
        </DropdownMenuItem>
        <DropdownMenuItem asChild>
          <Link href="/dashboard/settings" className="flex items-center">
            <Settings className="mr-2 h-4 w-4" />
            <span>Settings</span>
          </Link>
        </DropdownMenuItem>
        <DropdownMenuSeparator />
        <DropdownMenuItem 
          onClick={handleSignOut}
          className="text-destructive focus:text-destructive"
        >
          <LogOut className="mr-2 h-4 w-4" />
          <span>Sign out</span>
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  )
} 