import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'

vi.mock('@/api/acp', () => ({
  acpApi: {
    sessions: {
      list: vi.fn(),
      get: vi.fn(),
      create: vi.fn(),
      terminate: vi.fn(),
      events: vi.fn(),
    },
    agentKinds: {
      list: vi.fn().mockResolvedValue([]),
      get: vi.fn(),
      create: vi.fn(),
      update: vi.fn(),
      remove: vi.fn(),
    },
  },
  isAgentKindAdmin: () => false,
}))

vi.mock('@/api/workspaces', () => ({
  workspacesApi: {
    list: vi.fn().mockResolvedValue([]),
    get: vi.fn(),
    create: vi.fn(),
    update: vi.fn(),
    delete: vi.fn(),
    sync: vi.fn(),
    upsertCredential: vi.fn(),
    getCredential: vi.fn(),
    listWorktrees: vi.fn(),
    createWorktree: vi.fn(),
    deleteWorktree: vi.fn(),
    acquire: vi.fn(),
    release: vi.fn(),
    statusOnMain: vi.fn(),
    commitOnMain: vi.fn(),
    pushOnMain: vi.fn(),
    statusOnWorktree: vi.fn(),
    commitOnWorktree: vi.fn(),
    pushOnWorktree: vi.fn(),
  },
}))

const routeMock = vi.hoisted(() => ({
  params: { slug: 'p', wsSlug: 'w' },
  query: {} as Record<string, string>,
}))

vi.mock('vue-router', () => ({
  useRoute: () => routeMock,
  useRouter: () => ({ push: vi.fn() }),
}))

import AcpSessionCreateView from './AcpSessionCreateView.vue'

beforeEach(() => {
  setActivePinia(createPinia())
  routeMock.query = {}
})

describe('AcpSessionCreateView', () => {
  it('mounts without error', async () => {
    const wrapper = mount(AcpSessionCreateView)
    await new Promise((r) => setTimeout(r, 20))
    expect(wrapper.html()).toContain('新建 ACP 会话')
  })

  it('prefills issue number from query', async () => {
    routeMock.query = { issue_number: '42' }
    const wrapper = mount(AcpSessionCreateView)
    await new Promise((r) => setTimeout(r, 20))
    const issueInput = wrapper.find('[data-testid="issue-number-input"] input')
    const reqInput = wrapper.find('[data-testid="requirement-number-input"] input')
    expect((issueInput.element as HTMLInputElement).value).toBe('42')
    expect((reqInput.element as HTMLInputElement).value).toBe('')
  })

  it('prefills requirement number from query', async () => {
    routeMock.query = { requirement_number: '7' }
    const wrapper = mount(AcpSessionCreateView)
    await new Promise((r) => setTimeout(r, 20))
    const issueInput = wrapper.find('[data-testid="issue-number-input"] input')
    const reqInput = wrapper.find('[data-testid="requirement-number-input"] input')
    expect((issueInput.element as HTMLInputElement).value).toBe('')
    expect((reqInput.element as HTMLInputElement).value).toBe('7')
  })

  it('rejects submit when issue # and requirement # are both filled', async () => {
    const wrapper = mount(AcpSessionCreateView)
    await new Promise((r) => setTimeout(r, 20))
    const issueInput = wrapper.find('[data-testid="issue-number-input"] input')
    const reqInput = wrapper.find('[data-testid="requirement-number-input"] input')
    await issueInput.setValue('42')
    await reqInput.setValue('7')
    await wrapper.find('form').trigger('submit.prevent')
    expect(wrapper.text()).toContain('最多只能填写一项')
  })
})
