'use client';

import { useTranslations } from 'next-intl';
import { Link } from '@/i18n/navigation';
import { useCookieConsentStore } from '@/store/cookieConsentStore';

export default function CookieConsentBanner() {
  const t = useTranslations('cookieBanner');
  const { hasConsented, hasHydrated, acceptAll, rejectAll } = useCookieConsentStore();

  // Avoid flash on SSR — only render after client hydration
  if (!hasHydrated || hasConsented) return null;

  return (
    <div
      role="dialog"
      aria-label={t('heading')}
      aria-modal="false"
      className="fixed bottom-0 left-0 right-0 z-50 p-4 sm:p-6"
    >
      <div className="max-w-4xl mx-auto bg-white border border-gray-200 rounded-2xl shadow-xl px-6 py-5 flex flex-col sm:flex-row sm:items-center gap-4">
        <div className="flex-1 min-w-0">
          <p className="text-sm font-semibold text-gray-900 mb-1">{t('heading')}</p>
          <p className="text-sm text-gray-500 leading-relaxed">{t('body')}</p>
        </div>

        <div className="flex flex-wrap items-center gap-2 flex-shrink-0">
          <button
            onClick={acceptAll}
            className="bg-brand-purple hover:bg-purple-700 text-white text-sm font-medium px-4 py-2 rounded-md transition-colors whitespace-nowrap"
          >
            {t('acceptAll')}
          </button>
          <button
            onClick={rejectAll}
            className="border border-gray-300 hover:border-gray-400 text-gray-700 text-sm px-4 py-2 rounded-md transition-colors whitespace-nowrap"
          >
            {t('rejectAll')}
          </button>
          <Link
            href="/cookies"
            className="text-sm text-gray-400 hover:text-gray-600 px-2 py-2 transition-colors whitespace-nowrap underline"
          >
            {t('customize')}
          </Link>
        </div>
      </div>
    </div>
  );
}
