'use client';

import { usePathname } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { Button } from '@/components/ui/button';
import { LanguageSwitcher } from '@/components/ui/language-switcher';
import { Link } from '@/i18n/navigation';
import { useAuthStore } from '@/lib/auth-store';
import { cn } from '@/lib/utils';

export default function PublicLayout({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  const { user } = useAuthStore();
  const t = useTranslations();

  return (
    <div className="min-h-screen bg-background">
      <header
        role="banner"
        className="sticky top-0 z-50 border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60"
      >
        <div className="mx-auto flex h-14 max-w-7xl items-center px-4">
          <Link href="/" className="mr-6 flex items-center space-x-2">
            <span className="text-lg font-bold text-primary">{t('common.goodgo')}</span>
          </Link>

          <nav aria-label={t('nav.mainNav')} className="flex items-center space-x-1">
            <Link
              href="/"
              className={cn(
                'rounded-md px-3 py-2 text-sm font-medium transition-colors hover:bg-accent hover:text-accent-foreground',
                pathname === '/' || pathname.match(/^\/(vi|en)\/?$/)
                  ? 'bg-accent text-accent-foreground'
                  : 'text-muted-foreground',
              )}
            >
              {t('nav.home')}
            </Link>
            <Link
              href="/search"
              className={cn(
                'rounded-md px-3 py-2 text-sm font-medium transition-colors hover:bg-accent hover:text-accent-foreground',
                pathname.includes('/search')
                  ? 'bg-accent text-accent-foreground'
                  : 'text-muted-foreground',
              )}
            >
              {t('nav.search')}
            </Link>
          </nav>

          <div className="ml-auto flex items-center space-x-2">
            <LanguageSwitcher />
            {user ? (
              <>
                <span className="hidden text-sm text-muted-foreground sm:inline">
                  {user.fullName}
                </span>
                <Link href="/dashboard">
                  <Button size="sm">{t('common.dashboard')}</Button>
                </Link>
              </>
            ) : (
              <>
                <Link href="/login">
                  <Button variant="ghost" size="sm">
                    {t('common.login')}
                  </Button>
                </Link>
                <Link href="/register">
                  <Button size="sm">{t('common.register')}</Button>
                </Link>
              </>
            )}
          </div>
        </div>
      </header>

      <main id="main-content" role="main">
        {children}
      </main>

      <footer role="contentinfo" className="border-t bg-muted/40">
        <div className="mx-auto max-w-7xl px-4 py-8">
          <div className="grid gap-8 sm:grid-cols-2 lg:grid-cols-4">
            <div>
              <h3 className="mb-3 text-sm font-semibold">{t('common.goodgo')}</h3>
              <p className="text-sm text-muted-foreground">
                {t('footer.description')}
              </p>
            </div>
            <div>
              <h3 className="mb-3 text-sm font-semibold">{t('footer.propertyTypes')}</h3>
              <ul className="space-y-2 text-sm text-muted-foreground">
                <li><Link href="/search?propertyType=APARTMENT" className="hover:text-foreground">{t('propertyTypes.APARTMENT')}</Link></li>
                <li><Link href="/search?propertyType=HOUSE" className="hover:text-foreground">{t('propertyTypes.HOUSE')}</Link></li>
                <li><Link href="/search?propertyType=VILLA" className="hover:text-foreground">{t('propertyTypes.VILLA')}</Link></li>
                <li><Link href="/search?propertyType=LAND" className="hover:text-foreground">{t('propertyTypes.LAND')}</Link></li>
              </ul>
            </div>
            <div>
              <h3 className="mb-3 text-sm font-semibold">{t('footer.areas')}</h3>
              <ul className="space-y-2 text-sm text-muted-foreground">
                <li><Link href="/search?city=Hồ Chí Minh" className="hover:text-foreground">TP. Hồ Chí Minh</Link></li>
                <li><Link href="/search?city=Hà Nội" className="hover:text-foreground">Hà Nội</Link></li>
                <li><Link href="/search?city=Đà Nẵng" className="hover:text-foreground">Đà Nẵng</Link></li>
                <li><Link href="/search?city=Nha Trang" className="hover:text-foreground">Nha Trang</Link></li>
              </ul>
            </div>
            <div>
              <h3 className="mb-3 text-sm font-semibold">{t('footer.support')}</h3>
              <ul className="space-y-2 text-sm text-muted-foreground">
                <li><Link href="/login" className="hover:text-foreground">{t('common.login')}</Link></li>
                <li><Link href="/register" className="hover:text-foreground">{t('common.register')}</Link></li>
              </ul>
            </div>
          </div>
          <div className="mt-8 border-t pt-4 text-center text-sm text-muted-foreground">
            {t('common.allRightsReserved')}
          </div>
        </div>
      </footer>
    </div>
  );
}
