'use client';

import { ActionLink } from '@/components/actions/ActionLink';
import type { ShellMessages } from '@/lib/localization/messages';
import { localizedModePath, type Locale } from '@/lib/localization/config';
import { accountCreateUrl } from '@/lib/account-urls';
import type { ThemePreference } from '@/lib/theme/config';
import { useEffect, useRef, useState } from 'react';
import { HeaderNavigation } from './HeaderNavigation';
import { LocaleSelector } from './LocaleSelector';
import styles from './SiteHeader.module.css';
import { ThemeSelector } from './ThemeSelector';

function signInUrl(locale: Locale, mode: ThemePreference): string {
  return localizedModePath('sign-in', locale, mode);
}

interface MobileNavigationProps {
  locale: Locale;
  messages: ShellMessages;
  themePreference: ThemePreference;
}

export function MobileNavigation({
  locale,
  messages,
  themePreference,
}: MobileNavigationProps) {
  const [open, setOpen] = useState(false);
  const dialogRef = useRef<HTMLDialogElement>(null);
  const triggerRef = useRef<HTMLButtonElement>(null);
  const closeRef = useRef<HTMLButtonElement>(null);

  useEffect(() => {
    const dialog = dialogRef.current;
    if (!dialog) return;

    if (open && !dialog.open) {
      dialog.showModal();
      document.body.dataset.navigationOpen = 'true';
      closeRef.current?.focus();
    } else if (!open && dialog.open) {
      dialog.close();
    }

    return () => {
      delete document.body.dataset.navigationOpen;
    };
  }, [open]);

  const close = () => {
    dialogRef.current?.close();
    setOpen(false);
    delete document.body.dataset.navigationOpen;
    window.requestAnimationFrame(() => triggerRef.current?.focus());
  };

  return (
    <div className={styles.mobileNavigation}>
      <button
        ref={triggerRef}
        type="button"
        className={styles.menuButton}
        aria-label={messages.header.menuOpen}
        aria-haspopup="dialog"
        aria-expanded={open}
        onClick={() => setOpen(true)}
      >
        <span aria-hidden="true" className={styles.menuIcon}>
          <span />
          <span />
          <span />
        </span>
      </button>

      <dialog
        ref={dialogRef}
        className={styles.drawer}
        aria-label={messages.header.navigationLabel}
        onClose={() => {
          setOpen(false);
          delete document.body.dataset.navigationOpen;
        }}
        onCancel={(event) => {
          event.preventDefault();
          close();
        }}
      >
        <div className={styles.drawerPanel}>
          <div className={styles.drawerHeader}>
            <strong>RentalDriveGo</strong>
            <button
              ref={closeRef}
              type="button"
              className={styles.closeButton}
              aria-label={messages.header.menuClose}
              onClick={close}
            >
              <span aria-hidden="true">×</span>
            </button>
          </div>

          <HeaderNavigation
            locale={locale}
            mode={themePreference}
            label={messages.header.navigationLabel}
            labels={messages.header.nav}
            mobile
            onNavigate={close}
          />

          <div
            className={styles.drawerControls}
            role="group"
            aria-label={messages.header.controlsLabel}
          >
            <LocaleSelector
              currentLocale={locale}
              label={messages.controls.language}
              localeNames={messages.controls.localeNames}
            />
            <ThemeSelector
              initialPreference={themePreference}
              label={messages.controls.theme}
              statusTemplate={messages.controls.themeStatus}
              optionLabels={messages.controls.themes}
            />
          </div>

          <div className={styles.drawerActions}>
            <ActionLink href={signInUrl(locale, themePreference)} variant="button-primary">
              {messages.header.login}
            </ActionLink>
            <ActionLink href={accountCreateUrl(locale)} variant="button-conversion">
              {messages.header.demo}
            </ActionLink>
          </div>
        </div>
      </dialog>
    </div>
  );
}
