import { fetchApi } from './client';
import type { User } from './types';

export const authApi = {
  // Magic link
  requestMagicLink: (email: string) =>
    fetchApi<{ message: string }>('/api/auth/magic-link/request', {
      method: 'POST',
      body: JSON.stringify({ email }),
    }),

  verifyMagicLink: (token: string) =>
    fetchApi<{ user: User; token: string; refreshToken: string }>('/api/auth/magic-link/verify', {
      method: 'POST',
      body: JSON.stringify({ token }),
    }),

  // Password reset
  requestPasswordReset: (email: string) =>
    fetchApi<{ message: string }>('/api/auth/password-reset/request', {
      method: 'POST',
      body: JSON.stringify({ email }),
    }),

  confirmPasswordReset: (token: string, password: string) =>
    fetchApi<{ message: string }>('/api/auth/password-reset/confirm', {
      method: 'POST',
      body: JSON.stringify({ token, password }),
    }),

  // Account claiming
  requestClaimAccount: (email: string) =>
    fetchApi<{ message: string }>('/api/auth/claim-account/request', {
      method: 'POST',
      body: JSON.stringify({ email }),
    }),

  confirmClaimAccount: (token: string, data: { password?: string; googleId?: string }) =>
    fetchApi<{ user: User; token: string; refreshToken: string; message: string }>(
      '/api/auth/claim-account/confirm',
      {
        method: 'POST',
        body: JSON.stringify({ token, ...data }),
      }
    ),

  // Google OAuth
  googleAuth: (credential: string) =>
    fetchApi<{ user: User; token: string; refreshToken: string }>('/api/auth/google', {
      method: 'POST',
      body: JSON.stringify({ credential }),
    }),

  // Change password
  changePassword: (currentPassword: string, newPassword: string) =>
    fetchApi<{ message: string }>('/api/auth/change-password', {
      method: 'POST',
      body: JSON.stringify({ currentPassword, newPassword }),
    }),

  // Get current user
  me: () => fetchApi<{ user: User }>('/api/auth/me'),
};
