/**
 * Server-only auth utilities.
 * Import as: import { getServerAuthState } from '@/lib/auth/server'
 *
 * Uses `next/headers` (via the server cookie manager) and must only be called
 * from Server Components, Server Actions, or Route Handlers — never from a
 * client component.
 */
import { COOKIE_NAMES } from '@/lib/cookies';
import { getServerCookie } from '@/lib/cookies/server';
import type { AuthState } from '@/context/auth/types';
import { isTokenAlive } from './token';

/**
 * Resolve the current request's auth state from the access-token cookie so the
 * root layout can seed AuthProvider with a server-correct value before the first
 * paint.
 */
export async function getServerAuthState(): Promise<AuthState> {
  const token = await getServerCookie(COOKIE_NAMES.ACCESS_TOKEN);
  return { isAuthenticated: isTokenAlive(token) };
}
