import { describe, expect, it, vi, beforeEach } from 'vitest'

// Re-implement a minimal version of the service methods to test URL patterns
// This avoids private property issues

describe('Endpoint Contract Tests', () => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  let mockFetch: any

  beforeEach(() => {
    mockFetch = vi.fn()
    vi.stubGlobal('authFetch', mockFetch)
    mockFetch.mockResolvedValue({
      ok: true,
      json: async () => ({ code: 200, data: [] })
    })
  })

  describe('Risk Service URL Patterns - /risks/* endpoints', () => {
    // Simulate what risk.ts does with the baseUrl
    const riskService = {
      baseUrl: '/api/v1',
      async getAlerts(params?: { page?: number; size?: number }) {
        const searchParams = new URLSearchParams()
        if (params?.page) searchParams.set('page', String(params.page))
        if (params?.size) searchParams.set('size', String(params.size))
        const queryString = searchParams.toString()
        return await mockFetch(`${this.baseUrl}/risks/alerts?${queryString}`)
      },
      async getAlertById(id: number) {
        return await mockFetch(`${this.baseUrl}/risks/alerts/${id}`)
      },
      async handleAlert(id: number, data: { status: string; handleResult: string }) {
        return await mockFetch(`${this.baseUrl}/risks/alerts/${id}/handle`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify(data)
        })
      },
      async batchHandleAlerts(ids: number[], data: { status: string; handleResult: string }) {
        return await mockFetch(`${this.baseUrl}/risks/alerts/batch-handle`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ ids, ...data })
        })
      }
    }

    it('getAlerts should use /risks/alerts endpoint (not /risk/alerts)', async () => {
      await riskService.getAlerts({ page: 1, size: 10 })
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/risks/alerts')
      expect(calledUrl).not.toMatch(/\/risk\//)
    })

    it('getAlertById should use /risks/alerts/:id endpoint', async () => {
      await riskService.getAlertById(123)
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/risks/alerts/123')
      expect(calledUrl).not.toMatch(/\/risk\//)
    })

    it('handleAlert should use /risks/alerts/:id/handle endpoint', async () => {
      await riskService.handleAlert(123, { status: 'HANDLED', handleResult: 'Test' })
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/risks/alerts/123/handle')
      expect(calledUrl).not.toMatch(/\/risk\//)
    })

    it('batchHandleAlerts should use /risks/alerts/batch-handle endpoint', async () => {
      await riskService.batchHandleAlerts([1, 2, 3], { status: 'HANDLED', handleResult: 'Batch test' })
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/risks/alerts/batch-handle')
      expect(calledUrl).not.toMatch(/\/risk\//)
    })
  })

  describe('SystemConfig Service URL Patterns - /keys/* endpoints', () => {
    // Simulate what systemConfig.ts does
    const systemConfigService = {
      baseUrl: '/api/v1',
      async getApiKeys() {
        return await mockFetch(`${this.baseUrl}/keys`)
      },
      async createApiKey(name: string) {
        return await mockFetch(`${this.baseUrl}/keys`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ name })
        })
      },
      async deleteApiKey(id: number) {
        return await mockFetch(`${this.baseUrl}/keys/${id}`, { method: 'DELETE' })
      },
      async enableApiKey(id: number) {
        return await mockFetch(`${this.baseUrl}/keys/${id}/enable`, { method: 'POST' })
      },
      async disableApiKey(id: number) {
        return await mockFetch(`${this.baseUrl}/keys/${id}/disable`, { method: 'POST' })
      },
      async resetApiKey(id: number) {
        return await mockFetch(`${this.baseUrl}/keys/${id}/reset`, { method: 'POST' })
      }
    }

    it('getApiKeys should use /keys endpoint (not /api-keys)', async () => {
      await systemConfigService.getApiKeys()
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/keys')
      expect(calledUrl).not.toContain('/api-keys')
    })

    it('createApiKey should use /keys endpoint', async () => {
      await systemConfigService.createApiKey('test-key')
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/keys')
      expect(calledUrl).not.toContain('/api-keys')
    })

    it('deleteApiKey should use /keys/:id endpoint', async () => {
      await systemConfigService.deleteApiKey(123)
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/keys/123')
      expect(calledUrl).not.toContain('/api-keys')
    })

    it('enableApiKey should use /keys/:id/enable endpoint', async () => {
      await systemConfigService.enableApiKey(123)
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/keys/123/enable')
      expect(calledUrl).not.toContain('/api-keys')
    })

    it('disableApiKey should use /keys/:id/disable endpoint', async () => {
      await systemConfigService.disableApiKey(123)
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/keys/123/disable')
      expect(calledUrl).not.toContain('/api-keys')
    })

    it('resetApiKey should use /keys/:id/reset endpoint', async () => {
      await systemConfigService.resetApiKey(123)
      const calledUrl = mockFetch.mock.calls[0][0] as string
      expect(calledUrl).toContain('/keys/123/reset')
      expect(calledUrl).not.toContain('/api-keys')
    })
  })
})
