/**
 * Business API - Resources and Users
 */

import apiClient from './client';
import { User, Resource, BusinessOAuthSettings, BusinessOAuthSettingsResponse, BusinessOAuthCredentials } from '../types';

/**
 * Get all resources for the current business
 */
export const getResources = async (): Promise<Resource[]> => {
  const response = await apiClient.get<Resource[]>('/api/resources/');
  return response.data;
};

/**
 * Get all users for the current business
 */
export const getBusinessUsers = async (): Promise<User[]> => {
  const response = await apiClient.get<User[]>('/api/business/users/');
  return response.data;
};

/**
 * Get business OAuth settings and available platform providers
 */
export const getBusinessOAuthSettings = async (): Promise<BusinessOAuthSettingsResponse> => {
  const response = await apiClient.get<{
    business_settings: {
      oauth_enabled_providers: string[];
      oauth_allow_registration: boolean;
      oauth_auto_link_by_email: boolean;
    };
    available_providers: string[];
  }>('/api/business/oauth-settings/');

  // Transform snake_case to camelCase
  return {
    businessSettings: {
      enabledProviders: response.data.business_settings.oauth_enabled_providers || [],
      allowRegistration: response.data.business_settings.oauth_allow_registration,
      autoLinkByEmail: response.data.business_settings.oauth_auto_link_by_email,
    },
    availableProviders: response.data.available_providers || [],
  };
};

/**
 * Update business OAuth settings
 */
export const updateBusinessOAuthSettings = async (
  settings: Partial<BusinessOAuthSettings>
): Promise<BusinessOAuthSettingsResponse> => {
  // Transform camelCase to snake_case for backend
  const backendData: Record<string, any> = {};

  if (settings.enabledProviders !== undefined) {
    backendData.oauth_enabled_providers = settings.enabledProviders;
  }
  if (settings.allowRegistration !== undefined) {
    backendData.oauth_allow_registration = settings.allowRegistration;
  }
  if (settings.autoLinkByEmail !== undefined) {
    backendData.oauth_auto_link_by_email = settings.autoLinkByEmail;
  }

  const response = await apiClient.patch<{
    business_settings: {
      oauth_enabled_providers: string[];
      oauth_allow_registration: boolean;
      oauth_auto_link_by_email: boolean;
    };
    available_providers: string[];
  }>('/api/business/oauth-settings/update/', backendData);

  // Transform snake_case to camelCase
  return {
    businessSettings: {
      enabledProviders: response.data.business_settings.oauth_enabled_providers || [],
      allowRegistration: response.data.business_settings.oauth_allow_registration,
      autoLinkByEmail: response.data.business_settings.oauth_auto_link_by_email,
    },
    availableProviders: response.data.available_providers || [],
  };
};

/**
 * Get business OAuth credentials (custom credentials for paid tiers)
 */
export const getBusinessOAuthCredentials = async (): Promise<BusinessOAuthCredentials> => {
  const response = await apiClient.get<BusinessOAuthCredentials>('/api/business/oauth-credentials/');
  return response.data;
};

/**
 * Update business OAuth credentials (custom credentials for paid tiers)
 */
export const updateBusinessOAuthCredentials = async (
  credentials: Partial<BusinessOAuthCredentials>
): Promise<BusinessOAuthCredentials> => {
  const response = await apiClient.patch<BusinessOAuthCredentials>(
    '/api/business/oauth-credentials/update/',
    credentials
  );
  return response.data;
};
