/**
 * orgs API 契约测试（组织/部门/岗位）
 */
import { describe, it, expect, vi, beforeEach } from 'vitest'

const mockGet = vi.fn()
const mockPost = vi.fn()
const mockPut = vi.fn()
const mockDelete = vi.fn()

vi.mock('./client', () => ({
  default: {
    get: (...args: unknown[]) => mockGet(...args),
    post: (...args: unknown[]) => mockPost(...args),
    put: (...args: unknown[]) => mockPut(...args),
    delete: (...args: unknown[]) => mockDelete(...args),
  },
}))

import * as orgsApi from './orgs'

beforeEach(() => {
  vi.clearAllMocks()
})

describe('organizations API', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listOrgTree 应调用 GET /organizations', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await orgsApi.listOrgTree()

    expect(mockGet).toHaveBeenCalledWith('/organizations')
  })

  it('createOrg 应调用 POST /organizations', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '总公司', code: 'HQ' }
    await orgsApi.createOrg(req)

    expect(mockPost).toHaveBeenCalledWith('/organizations', req)
  })

  it('updateOrg 应调用 PUT /organizations/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { name: '改名', version: 1 }
    await orgsApi.updateOrg('org-001', req)

    expect(mockPut).toHaveBeenCalledWith('/organizations/org-001', req)
  })

  it('deleteOrg 应调用 DELETE /organizations/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await orgsApi.deleteOrg('org-001')

    expect(mockDelete).toHaveBeenCalledWith('/organizations/org-001')
  })
})

describe('departments API', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listDeptTree 应调用 GET /organizations/:orgId/departments', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await orgsApi.listDeptTree('org-001')

    expect(mockGet).toHaveBeenCalledWith('/organizations/org-001/departments')
  })

  it('createDept 应调用 POST /organizations/:orgId/departments', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '内科', code: 'NK' }
    await orgsApi.createDept('org-001', req)

    expect(mockPost).toHaveBeenCalledWith('/organizations/org-001/departments', req)
  })

  it('updateDept 应调用 PUT /departments/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { name: '内科(更新)', version: 1 }
    await orgsApi.updateDept('dept-001', req)

    expect(mockPut).toHaveBeenCalledWith('/departments/dept-001', req)
  })

  it('deleteDept 应调用 DELETE /departments/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await orgsApi.deleteDept('dept-001')

    expect(mockDelete).toHaveBeenCalledWith('/departments/dept-001')
  })
})

describe('positions API', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listPositions 应调用 GET /departments/:deptId/positions', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await orgsApi.listPositions('dept-001')

    expect(mockGet).toHaveBeenCalledWith('/departments/dept-001/positions')
  })

  it('createPosition 应调用 POST /departments/:deptId/positions', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '主治医师', code: 'ZYS' }
    await orgsApi.createPosition('dept-001', req)

    expect(mockPost).toHaveBeenCalledWith('/departments/dept-001/positions', req)
  })

  it('updatePosition 应调用 PUT /positions/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { name: '主任医师', version: 1 }
    await orgsApi.updatePosition('pos-001', req)

    expect(mockPut).toHaveBeenCalledWith('/positions/pos-001', req)
  })

  it('deletePosition 应调用 DELETE /positions/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await orgsApi.deletePosition('pos-001')

    expect(mockDelete).toHaveBeenCalledWith('/positions/pos-001')
  })
})
