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

vi.mock('../client')

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

  describe('getModes', () => {
    it('calls correct endpoint', async () => {
      const mockResponse = {
        data: {
          modes: {
            pattern: { name: 'Pattern-Based' },
            intelligent: { name: 'ML-Based' },
          },
        },
      }

      vi.mocked(apiClient.get).mockResolvedValue(mockResponse)

      const result = await autopilotApi.getModes()

      expect(apiClient.get).toHaveBeenCalledWith('/api/autopilot/modes')
      expect(result).toEqual(mockResponse.data)
    })
  })

  describe('startUnified', () => {
    it('calls correct endpoint with config', async () => {
      const config = {
        symbol: 'BTC/USD',
        mode: 'pattern' as const,
        auto_execute: false,
      }

      const mockResponse = { data: { status: 'started' } }
      vi.mocked(apiClient.post).mockResolvedValue(mockResponse)

      const result = await autopilotApi.startUnified(config)

      expect(apiClient.post).toHaveBeenCalledWith('/api/autopilot/start-unified', config)
      expect(result).toEqual(mockResponse.data)
    })
  })

  describe('stopUnified', () => {
    it('calls correct endpoint with parameters', async () => {
      const mockResponse = { data: { status: 'stopped' } }
      vi.mocked(apiClient.post).mockResolvedValue(mockResponse)

      const result = await autopilotApi.stopUnified('BTC/USD', 'pattern', '1h')

      expect(apiClient.post).toHaveBeenCalledWith(
        '/api/autopilot/stop-unified?symbol=BTC/USD&mode=pattern&timeframe=1h'
      )
      expect(result).toEqual(mockResponse.data)
    })
  })

  describe('getUnifiedStatus', () => {
    it('calls correct endpoint with parameters', async () => {
      const mockResponse = {
        data: {
          running: true,
          mode: 'pattern',
        },
      }
      vi.mocked(apiClient.get).mockResolvedValue(mockResponse)

      const result = await autopilotApi.getUnifiedStatus('BTC/USD', 'pattern', '1h')

      expect(apiClient.get).toHaveBeenCalledWith(
        '/api/autopilot/status-unified/BTC/USD?mode=pattern&timeframe=1h'
      )
      expect(result).toEqual(mockResponse.data)
    })
  })

  describe('backward compatibility', () => {
    it('start method still exists', () => {
      expect(autopilotApi.start).toBeDefined()
    })

    it('stop method still exists', () => {
      expect(autopilotApi.stop).toBeDefined()
    })

    it('getStatus method still exists', () => {
      expect(autopilotApi.getStatus).toBeDefined()
    })

    it('startIntelligent method still exists', () => {
      expect(autopilotApi.startIntelligent).toBeDefined()
    })

    it('stopIntelligent method still exists', () => {
      expect(autopilotApi.stopIntelligent).toBeDefined()
    })
  })
})

