// src/lib/api/auth.ts
import { apiClient } from './client';
import type { AuthResponse, TrialRegistrationData, LoginData, ForgotPasswordVerifyResponse } from '@/types/auth';
import type { SubscriptionAlertData } from '@/types/subscription';

export const authApi = {
  // Register a new trial organization
  registerTrial: async (data: TrialRegistrationData): Promise<AuthResponse> => {
    const response = await apiClient.post('/auth/register', data);
    return response.data;
  },

  // Login user
  login: async (data: LoginData): Promise<AuthResponse> => {
    const response = await apiClient.post('/auth/login', data);
    return response.data;
  },

  // Get user profile
  getProfile: async (): Promise<AuthResponse> => {
    const response = await apiClient.get('/auth/profile');
    return response.data;
  },

  /** Owner-only meaningful data; staff always gets showWarning: false */
  getSubscriptionAlert: async (): Promise<{
    success: boolean;
    data: SubscriptionAlertData;
  }> => {
    const response = await apiClient.get('/auth/subscription-alert');
    return response.data;
  },

  // Select organization
  selectOrganization: async (organizationId: string): Promise<any> => {
    const response = await apiClient.post('/auth/select-organization', { organizationId });
    return response.data;
  },

  // Create organization for current user
  createOrganization: async (data: {
    organizationName: string;
    organizationEmail: string;
    organizationType: 'CLINIC' | 'LAB';
    planName?: string;
  }): Promise<any> => {
    const response = await apiClient.post('/auth/organizations', data);
    return response.data;
  },

  // Logout
  logout: async (): Promise<void> => {
    await apiClient.post('/auth/logout');
  },

  updateLanguage: async (
    language: string,
  ): Promise<{ success: boolean; data: { user: { language: string } } }> => {
    const response = await apiClient.patch('/auth/profile/language', { language });
    return response.data;
  },

  // Refresh access token using httpOnly cookies (same as the axios interceptor).
  refreshSessionFromCookies: async (): Promise<{
    success: boolean;
    data?: { accessToken?: string; accessTokenExpiresAt?: string };
  }> => {
    const response = await apiClient.post('/auth/refresh', {});
    return response.data;
  },

  sendForgotPasswordCode: async (mobile: string): Promise<{ success: boolean; message: string }> => {
    const response = await apiClient.post('/auth/forgot-password/send-code', { mobile });
    return response.data;
  },

  verifyForgotPasswordCode: async (
    mobile: string,
    code: string,
  ): Promise<ForgotPasswordVerifyResponse> => {
    const response = await apiClient.post('/auth/forgot-password/verify', { mobile, code });
    return response.data;
  },

  changePassword: async (data: {
    currentPassword?: string;
    newPassword: string;
  }): Promise<{ success: boolean; message: string }> => {
    const response = await apiClient.patch('/auth/profile/password', data);
    return response.data;
  },
};