import { describe, expect, test } from 'bun:test'

import './index.js'
import {
  getGateway,
  getModelsForGateway,
  getVendor,
} from './index.js'
import {
  PRESET_VENDOR_MAP,
  gatewayIdForPreset,
  routeForPreset,
  vendorIdForPreset,
} from './compatibility.js'
import { resolveProfileRoute } from './profileResolver.js'
import type { ProviderPreset } from '../utils/providerProfiles.js'

const EXPECTED_PRESETS = [
  'anthropic',
  'atlas-cloud',
  'openai',
  'ollama',
  'kimi-code',
  'moonshotai',
  'deepseek',
  'gemini',
  'mistral',
  'together',
  'groq',
  'hicap',
  'azure-openai',
  'openrouter',
  'lmstudio',
  'dashscope-cn',
  'dashscope-intl',
  'custom',
  'nvidia-nim',
  'minimax',
  'xai',
  'venice',
  'xiaomi-mimo',
  'xiaomi-mimo-token',
  'zai',
  'bankr',
  'atomic-chat',
  'gitlawb-opengateway',
  'nearai',
  'fireworks',
  'opencode',
  'opencode-go',
] as const satisfies readonly ProviderPreset[]

describe('compatibility mappings', () => {
  test('cover every current provider preset exactly once', () => {
    expect(PRESET_VENDOR_MAP.map(mapping => mapping.preset).sort()).toEqual(
      [...EXPECTED_PRESETS].sort(),
    )
    expect(new Set(PRESET_VENDOR_MAP.map(mapping => mapping.preset)).size).toBe(
      EXPECTED_PRESETS.length,
    )
  })

  test('every preset resolves to an existing vendor and optional gateway', () => {
    for (const preset of EXPECTED_PRESETS) {
      const vendorId = vendorIdForPreset(preset)
      const gatewayId = gatewayIdForPreset(preset)
      const route = routeForPreset(preset)

      expect(getVendor(vendorId)?.id).toBe(vendorId)
      if (gatewayId) {
        expect(getGateway(gatewayId)?.id).toBe(gatewayId)
      }

      expect(route.vendorId).toBe(vendorId)
      expect(route.gatewayId).toBe(gatewayId)
      expect(route.routeId).toBe(gatewayId ?? vendorId)
    }
  })

  test('Atlas Cloud is modeled as a gateway while preserving the atlas-cloud preset', () => {
    expect(getVendor('atlas-cloud')).toBeUndefined()
    expect(getGateway('atlas-cloud')?.id).toBe('atlas-cloud')
    expect(routeForPreset('atlas-cloud')).toEqual({
      vendorId: 'openai',
      gatewayId: 'atlas-cloud',
      routeId: 'atlas-cloud',
    })
    expect(resolveProfileRoute('atlas-cloud')).toEqual({
      vendorId: 'openai',
      gatewayId: 'atlas-cloud',
      routeId: 'atlas-cloud',
    })
  })

  test('Atlas Cloud gateway models do not resolve to NearAI-scoped descriptors', () => {
    const atlasModels = getModelsForGateway('atlas-cloud')
    expect(atlasModels.length).toBeGreaterThan(0)
    expect(
      atlasModels.filter(model => model.vendorId === 'nearai'),
    ).toEqual([])
  })
  test('native gateway profile routes use their descriptor vendor', () => {
    expect(resolveProfileRoute('bedrock')).toEqual({
      vendorId: 'anthropic',
      gatewayId: 'bedrock',
      routeId: 'bedrock',
    })
    expect(resolveProfileRoute('vertex')).toEqual({
      vendorId: 'anthropic',
      gatewayId: 'vertex',
      routeId: 'vertex',
    })
  })
})
