"use client"

import Link from "next/link"
import { AgendaMark } from "@/components/suite/agenda-mark"
import { ChevronDown, ChevronLeft, ChevronRight, Menu } from "lucide-react"
import { HeaderAccountActions } from "@/components/suite/header-account-actions"
import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuShortcut,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"
import { viewTitle, type WeekStartsOn } from "@/lib/agenda/agenda-date"
import type { AgendaWeekStart } from "@/lib/agenda/agenda-settings-types"
import {
  AGENDA_VIEW_LABELS,
  AGENDA_VIEWS,
  type AgendaView,
} from "@/lib/agenda/agenda-url"
import { useAgendaSettingsStore, useAgendaUIStore } from "@/lib/agenda/agenda-store"
import { useIsMobile } from "@/hooks/use-mobile"
import { ULTICAL_APP_NAME } from "@/lib/suite/page-metadata"
import {
  SUITE_APP_LOGO_LOCKUP_CLASS,
  SUITE_APP_LOGO_MARK_CLASS,
  SUITE_APP_LOGO_TEXT_CLASS,
} from "@/lib/suite/suite-chrome-classes"
import { cn } from "@/lib/utils"

const VIEW_SHORTCUTS: Record<AgendaView, string> = {
  day: "J",
  week: "S",
  month: "M",
}

export function AgendaHeader({
  view,
  date,
  weekStart = "auto",
  weekStartsOn,
  onToday,
  onStep,
  onViewChange,
}: {
  view: AgendaView
  date: Date
  weekStart?: AgendaWeekStart
  weekStartsOn?: WeekStartsOn
  onToday: () => void
  onStep: (delta: 1 | -1) => void
  onViewChange: (view: AgendaView) => void
}) {
  const isMobile = useIsMobile()
  const sidebarCollapsed = useAgendaUIStore((s) => s.sidebarCollapsed)
  const setSidebarCollapsed = useAgendaUIStore((s) => s.setSidebarCollapsed)
  const openQuickSettings = useAgendaSettingsStore((s) => s.setQuickSettingsOpen)

  return (
    <header className="flex h-16 shrink-0 items-center gap-2 bg-app-canvas px-2 sm:px-4">
      <Button
        variant="ghost"
        size="icon"
        className="size-10 shrink-0 rounded-full text-muted-foreground hover:bg-mail-nav-hover"
        aria-label={sidebarCollapsed ? "Ouvrir le menu" : "Fermer le menu"}
        onClick={() => setSidebarCollapsed(!sidebarCollapsed)}
      >
        <Menu className="size-5" />
      </Button>

      <Link
        href="/agenda"
        className={cn("mr-1 hidden sm:flex", SUITE_APP_LOGO_LOCKUP_CLASS)}
      >
        <AgendaMark className={SUITE_APP_LOGO_MARK_CLASS} />
        <span className={cn("hidden md:block", SUITE_APP_LOGO_TEXT_CLASS)}>{ULTICAL_APP_NAME}</span>
      </Link>

      <Button
        variant="outline"
        className="ml-1 h-9 rounded-full px-4 text-sm font-medium sm:ml-4"
        onClick={onToday}
      >
        Aujourd&apos;hui
      </Button>

      <div className="flex items-center">
        <Tooltip>
          <TooltipTrigger asChild>
            <Button
              variant="ghost"
              size="icon"
              className="size-9 rounded-full text-muted-foreground hover:bg-mail-nav-hover"
              aria-label="Période précédente"
              onClick={() => onStep(-1)}
            >
              <ChevronLeft className="size-5" />
            </Button>
          </TooltipTrigger>
          <TooltipContent>Période précédente</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger asChild>
            <Button
              variant="ghost"
              size="icon"
              className="size-9 rounded-full text-muted-foreground hover:bg-mail-nav-hover"
              aria-label="Période suivante"
              onClick={() => onStep(1)}
            >
              <ChevronRight className="size-5" />
            </Button>
          </TooltipTrigger>
          <TooltipContent>Période suivante</TooltipContent>
        </Tooltip>
      </div>

      <h1
        className={cn(
          "min-w-0 truncate text-[1.05rem] font-normal text-foreground/90 sm:text-[1.35rem]",
        )}
      >
        {viewTitle(view, date, weekStart, weekStartsOn)}
      </h1>

      <div className="ml-auto flex items-center gap-1">
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              variant="outline"
              className="h-9 gap-1.5 rounded-full px-3 text-sm sm:px-4"
            >
              {isMobile
                ? AGENDA_VIEW_LABELS[view].slice(0, 1)
                : AGENDA_VIEW_LABELS[view]}
              <ChevronDown className="size-4 opacity-70" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-44">
            {AGENDA_VIEWS.map((v) => (
              <DropdownMenuItem key={v} onSelect={() => onViewChange(v)}>
                {AGENDA_VIEW_LABELS[v]}
                <DropdownMenuShortcut>{VIEW_SHORTCUTS[v]}</DropdownMenuShortcut>
              </DropdownMenuItem>
            ))}
          </DropdownMenuContent>
        </DropdownMenu>
        <HeaderAccountActions
          className="pl-1"
          onSettingsClick={() => openQuickSettings(true)}
        />
      </div>
    </header>
  )
}
