import { apiClient } from './api'

export interface UploadLimitResponse {
  upload_limit_mb: number
  description: string
}

export interface UploadLimitUpdate {
  upload_limit_mb: number
}

export interface GlobalSetting {
  id: number
  setting_key: string
  setting_value: string
  description?: string
  created_at: string
  updated_at: string
}

export interface GlobalSettingCreate {
  setting_key: string
  setting_value: string
  description?: string
}

export interface GlobalSettingUpdate {
  setting_value: string
  description?: string
}

class SettingsService {
  async getUploadLimit(): Promise<UploadLimitResponse> {
    const response = await apiClient.get('/settings/upload-limit')
    return response.data
  }

  async updateUploadLimit(uploadLimit: UploadLimitUpdate): Promise<UploadLimitResponse> {
    const response = await apiClient.put('/settings/upload-limit', uploadLimit)
    return response.data
  }

  async getAllSettings(): Promise<GlobalSetting[]> {
    const response = await apiClient.get('/settings/')
    return response.data
  }

  async createSetting(setting: GlobalSettingCreate): Promise<GlobalSetting> {
    const response = await apiClient.post('/settings/', setting)
    return response.data
  }

  async updateSetting(settingKey: string, setting: GlobalSettingUpdate): Promise<GlobalSetting> {
    const response = await apiClient.put(`/settings/${settingKey}`, setting)
    return response.data
  }

  async deleteSetting(settingKey: string): Promise<void> {
    await apiClient.delete(`/settings/${settingKey}`)
  }
}

export const settingsService = new SettingsService()