/**
 * @ai-summary API client for auth feature (signup, verification)
 */

import axios from 'axios';
import { apiClient } from '../../../core/api/client';
import {
  SignupRequest,
  SignupResponse,
  VerifyStatusResponse,
  ResendVerificationResponse,
  ResendVerificationPublicRequest,
  UserStatusResponse,
} from '../types/auth.types';

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api';

// Create unauthenticated client for public endpoints (no JWT required)
const unauthenticatedClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
});

export const authApi = {
  signup: async (data: SignupRequest): Promise<SignupResponse> => {
    const response = await unauthenticatedClient.post('/auth/signup', data);
    return response.data;
  },

  getVerifyStatus: async (): Promise<VerifyStatusResponse> => {
    const response = await apiClient.get('/auth/verify-status');
    return response.data;
  },

  resendVerification: async (): Promise<ResendVerificationResponse> => {
    const response = await apiClient.post('/auth/resend-verification');
    return response.data;
  },

  /**
   * Resend verification email by email address (public, no auth required)
   * Used on the "Check Your Email" page before user can login
   */
  resendVerificationPublic: async (data: ResendVerificationPublicRequest): Promise<ResendVerificationResponse> => {
    const response = await unauthenticatedClient.post('/auth/resend-verification-public', data);
    return response.data;
  },

  /**
   * Get user status for routing decisions (requires auth)
   * Returns email verification and onboarding completion status
   */
  getUserStatus: async (): Promise<UserStatusResponse> => {
    const response = await apiClient.get('/auth/user-status');
    return response.data;
  },
};
