'use client';

import { useEffect } from 'react';
import { useAuthStore } from '@/lib/auth-store';

function setAuthCookie(authenticated: boolean) {
  if (authenticated) {
    document.cookie = 'goodgo_authenticated=1; path=/; max-age=604800; SameSite=Lax';
  } else {
    document.cookie = 'goodgo_authenticated=; path=/; max-age=0';
  }
}

export function AuthProvider({ children }: { children: React.ReactNode }) {
  const initialize = useAuthStore((s) => s.initialize);
  const tokens = useAuthStore((s) => s.tokens);

  useEffect(() => {
    initialize();
  }, [initialize]);

  useEffect(() => {
    setAuthCookie(!!tokens);
  }, [tokens]);

  return <>{children}</>;
}
