/**
 * Augmentation API endpoints.
 *
 * Provides functions for fetching augmentation types, presets, and previewing augmentations.
 */

import apiClient from '../client'

// Types
export interface AugmentationTypeInfo {
  name: string
  description: string
  affects_geometry: boolean
  stage: string
  default_params: Record<string, unknown>
}

export interface AugmentationTypesResponse {
  augmentation_types: AugmentationTypeInfo[]
}

export interface PresetInfo {
  name: string
  description: string
  config?: Record<string, unknown>
}

export interface PresetsResponse {
  presets: PresetInfo[]
}

export interface PreviewRequest {
  augmentation_type: string
  params: Record<string, unknown>
}

export interface PreviewResponse {
  preview_url: string
  original_url: string
  applied_params: Record<string, unknown>
}

export interface AugmentationParams {
  enabled: boolean
  probability: number
  params: Record<string, unknown>
}

export interface AugmentationConfig {
  perspective_warp?: AugmentationParams
  wrinkle?: AugmentationParams
  edge_damage?: AugmentationParams
  stain?: AugmentationParams
  lighting_variation?: AugmentationParams
  shadow?: AugmentationParams
  gaussian_blur?: AugmentationParams
  motion_blur?: AugmentationParams
  gaussian_noise?: AugmentationParams
  salt_pepper?: AugmentationParams
  paper_texture?: AugmentationParams
  scanner_artifacts?: AugmentationParams
  preserve_bboxes?: boolean
  seed?: number | null
}

export interface BatchRequest {
  dataset_id: string
  config: AugmentationConfig
  output_name: string
  multiplier: number
}

export interface BatchResponse {
  task_id: string
  status: string
  message: string
  estimated_images: number
}

// API functions
export const augmentationApi = {
  /**
   * Fetch available augmentation types.
   */
  async getTypes(): Promise<AugmentationTypesResponse> {
    const response = await apiClient.get<AugmentationTypesResponse>(
      '/api/v1/admin/augmentation/types'
    )
    return response.data
  },

  /**
   * Fetch augmentation presets.
   */
  async getPresets(): Promise<PresetsResponse> {
    const response = await apiClient.get<PresetsResponse>(
      '/api/v1/admin/augmentation/presets'
    )
    return response.data
  },

  /**
   * Preview a single augmentation on a document page.
   */
  async preview(
    documentId: string,
    request: PreviewRequest,
    page: number = 1
  ): Promise<PreviewResponse> {
    const response = await apiClient.post<PreviewResponse>(
      `/api/v1/admin/augmentation/preview/${documentId}`,
      request,
      { params: { page } }
    )
    return response.data
  },

  /**
   * Preview full augmentation config on a document page.
   */
  async previewConfig(
    documentId: string,
    config: AugmentationConfig,
    page: number = 1
  ): Promise<PreviewResponse> {
    const response = await apiClient.post<PreviewResponse>(
      `/api/v1/admin/augmentation/preview-config/${documentId}`,
      config,
      { params: { page } }
    )
    return response.data
  },

  /**
   * Create an augmented dataset.
   */
  async createBatch(request: BatchRequest): Promise<BatchResponse> {
    const response = await apiClient.post<BatchResponse>(
      '/api/v1/admin/augmentation/batch',
      request
    )
    return response.data
  },
}
