'use client';

import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslations } from 'next-intl';
import { Link } from '@/i18n/navigation';
import {
  Settings,
  AlertTriangle,
  Building2,
  CreditCard,
  User,
  LogOut,
  ChevronDown,
} from 'lucide-react';
import { useAuth } from '@/lib/hooks/useAuth';
import { useAsyncAction } from '@/lib/hooks/useAsyncAction';
import { authApi } from '@/lib/api/auth';
import type { SubscriptionAlertData } from '@/types/subscription';
import { Button } from '@/components/ui/shared/Button';

function warningTooltip(
  data: SubscriptionAlertData | null,
  t: ReturnType<typeof useTranslations<'accountMenu'>>,
): string {
  if (!data?.showWarning) return '';
  if (data.noActiveSubscription) return t('noActiveSubscription');
  if (data.trialExpired) return t('trialEnded');
  if (data.trialEndingSoon) return t('trialEndingSoon');
  if (data.seatsLow) return t('seatsLow');
  return t('reviewSubscriptions');
}

export function DashboardAccountMenu() {
  const t = useTranslations('auth');
  const tAccount = useTranslations('accountMenu');
  const { user, currentOrganization, logout } = useAuth();
  const { pending: logoutPending, run: runLogout } = useAsyncAction();
  const [open, setOpen] = useState(false);
  const menuRef = useRef<HTMLDivElement>(null);
  const [alert, setAlert] = useState<SubscriptionAlertData | null>(null);
  const isOwner = currentOrganization?.isOwner ?? false;

  useEffect(() => {
    const onDocClick = (e: MouseEvent) => {
      if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
        setOpen(false);
      }
    };
    document.addEventListener('mousedown', onDocClick);
    return () => document.removeEventListener('mousedown', onDocClick);
  }, []);

  useEffect(() => {
    if (!isOwner || !currentOrganization) {
      setAlert(null);
      return;
    }
    let cancelled = false;
    void (async () => {
      try {
        const res = await authApi.getSubscriptionAlert();
        if (!cancelled && res.success) setAlert(res.data);
      } catch {
        if (!cancelled) setAlert(null);
      }
    })();
    return () => {
      cancelled = true;
    };
  }, [isOwner, currentOrganization?.id]);

  const showWarning = Boolean(isOwner && alert?.showWarning);
  const tooltip = useMemo(() => warningTooltip(alert, tAccount), [alert, tAccount]);

  const handleLogout = useCallback(() => {
    return runLogout(async () => {
      await logout();
      setOpen(false);
    });
  }, [logout, runLogout]);

  return (
    <div className="relative z-[120]" ref={menuRef}>
      <button
        type="button"
        onClick={() => setOpen((v) => !v)}
        className="inline-flex items-center gap-2 rounded-[var(--radius-md)] border border-border/70 px-3 py-2 text-sm text-text-primary hover:bg-background-card/80 transition-colors"
        aria-expanded={open}
        aria-haspopup="menu"
      >
        <span className="relative inline-flex shrink-0" title={showWarning ? tooltip : undefined}>
          <Settings className="h-5 w-5 icon-flat" aria-hidden />
          {showWarning && (
            <span
              className="absolute -right-1 -top-1 flex h-3.5 w-3.5 items-center justify-center rounded-full bg-amber-500 ring-2 ring-background-secondary"
              aria-label={tooltip}
            >
              <AlertTriangle className="h-2.5 w-2.5 text-amber-950" strokeWidth={2.5} />
            </span>
          )}
        </span>
        <span className="hidden sm:inline max-w-[160px] truncate">{user?.name}</span>
        <ChevronDown className="h-4 w-4 text-text-muted shrink-0" aria-hidden />
      </button>

      {open && (
        <div
          role="menu"
          className="absolute right-0 mt-2 w-[min(18rem,calc(100vw-1.5rem))] rounded-[var(--radius-md)] border border-border bg-background-secondary/95 py-2 shadow-lg z-[200] backdrop-blur-sm"
        >
          <div className="px-3 py-2 border-b border-border/60">
            <p className="text-xs text-text-muted">{t('signedIn')}</p>
            <p className="text-sm font-medium truncate">{user?.email}</p>
            <p className="text-xs text-text-secondary mt-1 truncate">
              {currentOrganization?.name}
            </p>
          </div>

          <div className="py-1">
            <Link
              href="/settings/organizations"
              role="menuitem"
              className="flex items-center gap-3 px-3 py-2.5 text-sm text-text-primary hover:bg-background-card/70"
              onClick={() => setOpen(false)}
            >
              <Building2 className="h-4 w-4 icon-flat shrink-0" />
              {t('switchOrganization')}
            </Link>

            {isOwner && (
              <Link
                href="/settings/subscriptions"
                role="menuitem"
                className="flex items-center gap-3 px-3 py-2.5 text-sm text-text-primary hover:bg-background-card/70"
                onClick={() => setOpen(false)}
              >
                <CreditCard className="h-4 w-4 icon-flat shrink-0" />
                {t('subscriptions')}
              </Link>
            )}

            <Link
              href="/settings/account"
              role="menuitem"
              className="flex items-center gap-3 px-3 py-2.5 text-sm text-text-primary hover:bg-background-card/70"
              onClick={() => setOpen(false)}
            >
              <User className="h-4 w-4 icon-flat shrink-0" />
              {t('account')}
            </Link>
          </div>

          <div className="border-t border-border/60 pt-1">
            <Button
              type="button"
              variant="ghost"
              role="menuitem"
              className="flex w-full items-center justify-start gap-3 rounded-none px-3 py-2.5 text-sm text-text-secondary hover:bg-background-card/70 hover:text-text-primary"
              isLoading={logoutPending}
              onClick={() => handleLogout()}
            >
              <LogOut className="h-4 w-4 icon-flat shrink-0" />
              {t('signOut')}
            </Button>
          </div>
        </div>
      )}
    </div>
  );
}
