import { describe, expect, it, vi } from 'vitest'
import {
  applyOAuthRuntimePatchToStore,
} from '../src/dimsdk/core/oauth-runtime-sync.ts'

describe('oauth runtime sync store updates', () => {
  it('writes provider state and env updates through the store bridge', () => {
    const setEnvVar = vi.fn()
    const store = {
      state: {
        context: {
          cwd: '/repo',
        },
        provider: {
          activeId: 'legacy-provider',
          connections: {
            'legacy-provider': {
              baseUrl: 'https://legacy.example.com',
            },
          },
        },
        ui: {
          workspaceSelectionByCwd: {
            '/repo': {
              providerId: 'legacy-provider',
            },
          },
        },
      },
      setEnvVar,
    }

    applyOAuthRuntimePatchToStore(store, {
      providerId: 'next-api-oauth',
      providerName: 'Next API',
      adapter: 'openai',
      modelId: 'next-api-model',
      baseUrl: 'https://nextapi.example.test/v1',
      apiKey: 'token-123',
      providerConnection: {
        providerId: 'next-api-oauth',
        baseUrl: 'https://nextapi.example.test/v1',
        apiKey: 'token-123',
        models: [{ id: 'next-api-model', name: 'Next API Model' }],
      },
      activeModel: {
        providerId: 'next-api-oauth',
        modelId: 'next-api-model',
      },
      workspaceSelection: {
        providerId: 'next-api-oauth',
        modelId: 'next-api-model',
      },
      clearWorkspaceSelectionsByProvider: 'legacy-provider',
    })

    expect(store.state.config).toEqual({
      model: 'next-api-model',
      baseUrl: 'https://nextapi.example.test/v1',
      apiKey: 'token-123',
    })
    expect(store.state.provider).toEqual({
      activeId: 'next-api-oauth',
      activeModelByProvider: {
        'next-api-oauth': 'next-api-model',
      },
      connections: {
        'legacy-provider': {
          baseUrl: 'https://legacy.example.com',
        },
        'next-api-oauth': {
          baseUrl: 'https://nextapi.example.test/v1',
          apiKey: 'token-123',
          models: [{ id: 'next-api-model', name: 'Next API Model' }],
        },
      },
    })
    expect(store.state.ui).toEqual({
      workspaceSelectionByCwd: {
        '/repo': {
          providerId: 'next-api-oauth',
          modelId: 'next-api-model',
        },
      },
    })
    expect(setEnvVar).toHaveBeenCalledWith('DIMCODE_PROVIDER_ID', 'next-api-oauth')
    expect(setEnvVar).toHaveBeenCalledWith(
      'DIMCODE_OPENAI_BASE_URL',
      'https://nextapi.example.test/v1',
    )
    expect(setEnvVar).toHaveBeenCalledWith('OPENAI_API_KEY', 'token-123')
  })

  it('infers the provider connection to clear from the current store state', () => {
    const setEnvVar = vi.fn()
    const store = {
      state: {
        context: {
          cwd: '/repo',
        },
        provider: {
          activeId: 'next-api-oauth',
          activeModelByProvider: {
            'next-api-oauth': 'next-api-model',
          },
          connections: {
            'next-api-oauth': {
              baseUrl: 'https://nextapi.example.test/v1',
              apiKey: 'token-123',
            },
          },
        },
      },
      setEnvVar,
    }

    applyOAuthRuntimePatchToStore(store, {
      clearProviderId: true,
      clearBaseUrl: true,
      clearApiKey: true,
      activeModel: {
        providerId: 'next-api-oauth',
        clear: true,
      },
    })

    expect(store.state.provider).toEqual({
      activeModelByProvider: {},
      connections: {},
    })
    expect(store.state.config).toEqual({})
    expect(setEnvVar).toHaveBeenCalledWith('DIMCODE_PROVIDER_ID', undefined)
    expect(setEnvVar).toHaveBeenCalledWith('DIMCODE_OPENAI_BASE_URL', undefined)
    expect(setEnvVar).toHaveBeenCalledWith('OPENAI_API_KEY', undefined)
  })
})
