'use client'

import * as React from 'react'
import { ChevronDown, Plus } from 'lucide-react'

import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuShortcut,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem } from '@/components/ui/sidebar'
import { SidebarLeftTeam } from './sidebar-left'
import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar'

const makeDefaultAcronym = (name: string, maxLength: number = 2) => {
  return name
    .split(' ')
    .map((part) => part[0])
    .slice(0, maxLength)
}

type Props = {
  teams: SidebarLeftTeam[]
}
export function TeamSwitcher(props: Props) {
  const { teams } = props
  const [activeTeam, setActiveTeam] = React.useState(teams[0])

  React.useEffect(() => {
    if (!activeTeam && teams.length) setActiveTeam(teams[0])
  }, [teams])

  if (!activeTeam) {
    return null
  }

  return (
    <SidebarMenu>
      <SidebarMenuItem>
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <SidebarMenuButton className="w-fit px-1.5">
              <div className="flex aspect-square size-5 items-center justify-center rounded-md bg-sidebar-primary text-sidebar-primary-foreground">
                <Avatar className="h-8 w-8 rounded-lg shrink-0">
                  <AvatarImage src={activeTeam.logo} alt={activeTeam.logo} />
                  <AvatarFallback className="rounded-lg">
                    {makeDefaultAcronym(activeTeam.name)}
                  </AvatarFallback>
                </Avatar>
              </div>
              <span className="truncate font-semibold">{activeTeam.name}</span>
              <ChevronDown className="opacity-50" />
            </SidebarMenuButton>
          </DropdownMenuTrigger>
          <DropdownMenuContent
            className="w-64 rounded-lg"
            align="start"
            side="bottom"
            sideOffset={4}
          >
            <DropdownMenuLabel className="text-xs text-muted-foreground">Teams</DropdownMenuLabel>
            {teams.map((team, index) => (
              <DropdownMenuItem
                key={team.name}
                onClick={() => setActiveTeam(team)}
                className="gap-2 p-2"
              >
                <div className="flex size-6 items-center justify-center rounded-sm border">
                  <Avatar className="size-8 rounded-lg shrink-0">
                    <AvatarImage src={team.logo} alt={team.logo} />
                    <AvatarFallback className="rounded-lg">
                      {makeDefaultAcronym(team.name)}
                    </AvatarFallback>
                  </Avatar>
                </div>
                {team.name}
                <DropdownMenuShortcut>⌘{index + 1}</DropdownMenuShortcut>
              </DropdownMenuItem>
            ))}
            <DropdownMenuSeparator />
            <DropdownMenuItem className="gap-2 p-2">
              <div className="flex size-6 items-center justify-center rounded-md border bg-background">
                <Plus className="size-4" />
              </div>
              <div className="font-medium text-muted-foreground">Add team</div>
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      </SidebarMenuItem>
    </SidebarMenu>
  )
}
