import { apiClient } from './client'
import type { SettingsResponse, BoostBotSettings, LicenseInfo, FeaturesResponse } from '@/types/settings'
import type { ApiResponse } from '@/types/api'

/**
 * Settings API
 */
export const settingsApi = {
  /**
   * Get all settings, optionally scoped to a language for per-language keys.
   */
  async getSettings(language?: string): Promise<SettingsResponse> {
    const params = language ? `?language=${encodeURIComponent(language)}` : ''
    return apiClient.get<SettingsResponse>(`settings${params}`)
  },
  
  /**
   * Update settings, optionally scoped to a language for per-language keys.
   */
  async updateSettings(data: Partial<BoostBotSettings>, language?: string): Promise<ApiResponse> {
    const payload = language ? { ...data, language } : data
    return apiClient.post<ApiResponse>('settings', payload)
  },
  
  /**
   * Reset settings to defaults
   */
  async resetSettings(): Promise<ApiResponse> {
    return apiClient.post<ApiResponse>('settings/reset', { confirm: true })
  },
  
  // =========================================================================
  // BYOK FUNCTIONALITY - FOR FUTURE DELETION
  // The following methods were used for Bring Your Own Key (BYOK) model.
  // Now all AI calls go through the credits backend (Week 26+).
  // =========================================================================

  /*
  // BYOK FUNCTIONALITY - FOR FUTURE DELETION
  async testApiKey(apiKey: string): Promise<ApiResponse> {
    return apiClient.post<ApiResponse>('settings/test-api', { api_key: apiKey })
  },

  // BYOK FUNCTIONALITY - FOR FUTURE DELETION
  async testOpenAiKey(apiKey: string): Promise<ApiResponse> {
    return apiClient.post<ApiResponse>('settings/test-openai-api', { openai_api_key: apiKey })
  },
  
  // BYOK FUNCTIONALITY - FOR FUTURE DELETION
  async clearApiKey(): Promise<ApiResponse> {
    return apiClient.post<ApiResponse>('settings', { api_key: '' })
  },

  // BYOK FUNCTIONALITY - FOR FUTURE DELETION
  async clearOpenAiKey(): Promise<ApiResponse> {
    return apiClient.post<ApiResponse>('settings', { openai_api_key: '' })
  },
  */
  // END BYOK FUNCTIONALITY
}

/**
 * License API
 */
export const licenseApi = {
  /**
   * Get license status
   */
  async getStatus(): Promise<ApiResponse<LicenseInfo>> {
    return apiClient.get<ApiResponse<LicenseInfo>>('license/status')
  },
  
  /**
   * Get features list
   */
  async getFeatures(): Promise<FeaturesResponse> {
    return apiClient.get<FeaturesResponse>('license/features')
  },
}

