/**
 * 소셜 로그인 헬퍼 — Google/Apple ID Token
 * 실제 클라이언트 키가 없으면(OAuth mock) 자체 서명 토큰으로 백엔드 placeholder 검증 경로를 탄다.
 */
const OAUTH_MOCK = import.meta.env.VITE_OAUTH_MOCK === 'true';

function base64url(str: string): string {
  return btoa(unescape(encodeURIComponent(str)))
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');
}

export function mockIdToken(payload: { email: string; sub?: string }): string {
  const header = base64url(JSON.stringify({ alg: 'HS256', typ: 'JWT' }));
  const body = base64url(JSON.stringify({ ...payload, iss: 'mock', aud: 'mock', iat: Date.now() / 1000 }));
  return `${header}.${body}.mock-signature`;
}

export function buildSocialToken(provider: 'GOOGLE' | 'APPLE', email: string): string {
  return mockIdToken({ email, sub: `${provider.toLowerCase()}-${email}` });
}

export const socialEnabled = OAUTH_MOCK;
