/**
 * Tests for augmentation API endpoints.
 *
 * TDD Phase 1: RED - Write tests first, then implement to pass.
 */

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { augmentationApi } from './augmentation'
import apiClient from '../client'

// Mock the API client
vi.mock('../client', () => ({
  default: {
    get: vi.fn(),
    post: vi.fn(),
  },
}))

describe('augmentationApi', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  describe('getTypes', () => {
    it('should fetch augmentation types', async () => {
      const mockResponse = {
        data: {
          augmentation_types: [
            {
              name: 'gaussian_noise',
              description: 'Adds Gaussian noise',
              affects_geometry: false,
              stage: 'noise',
              default_params: { mean: 0, std: 15 },
            },
          ],
        },
      }
      vi.mocked(apiClient.get).mockResolvedValueOnce(mockResponse)

      const result = await augmentationApi.getTypes()

      expect(apiClient.get).toHaveBeenCalledWith('/api/v1/admin/augmentation/types')
      expect(result.augmentation_types).toHaveLength(1)
      expect(result.augmentation_types[0].name).toBe('gaussian_noise')
    })
  })

  describe('getPresets', () => {
    it('should fetch augmentation presets', async () => {
      const mockResponse = {
        data: {
          presets: [
            { name: 'conservative', description: 'Safe augmentations' },
            { name: 'moderate', description: 'Balanced augmentations' },
          ],
        },
      }
      vi.mocked(apiClient.get).mockResolvedValueOnce(mockResponse)

      const result = await augmentationApi.getPresets()

      expect(apiClient.get).toHaveBeenCalledWith('/api/v1/admin/augmentation/presets')
      expect(result.presets).toHaveLength(2)
    })
  })

  describe('preview', () => {
    it('should preview single augmentation', async () => {
      const mockResponse = {
        data: {
          preview_url: 'data:image/png;base64,xxx',
          original_url: 'data:image/png;base64,yyy',
          applied_params: { std: 15 },
        },
      }
      vi.mocked(apiClient.post).mockResolvedValueOnce(mockResponse)

      const result = await augmentationApi.preview('doc-123', {
        augmentation_type: 'gaussian_noise',
        params: { std: 15 },
      })

      expect(apiClient.post).toHaveBeenCalledWith(
        '/api/v1/admin/augmentation/preview/doc-123',
        {
          augmentation_type: 'gaussian_noise',
          params: { std: 15 },
        },
        { params: { page: 1 } }
      )
      expect(result.preview_url).toBe('data:image/png;base64,xxx')
    })

    it('should support custom page number', async () => {
      const mockResponse = {
        data: {
          preview_url: 'data:image/png;base64,xxx',
          original_url: 'data:image/png;base64,yyy',
          applied_params: {},
        },
      }
      vi.mocked(apiClient.post).mockResolvedValueOnce(mockResponse)

      await augmentationApi.preview(
        'doc-123',
        { augmentation_type: 'gaussian_noise', params: {} },
        2
      )

      expect(apiClient.post).toHaveBeenCalledWith(
        '/api/v1/admin/augmentation/preview/doc-123',
        expect.anything(),
        { params: { page: 2 } }
      )
    })
  })
})
