import type { MockMethod } from './types'

let addresses = [
  { id: 1, name: 'FANUC-A栋', url: 'http://10.1.1.1/', brandId: 1, brandName: 'FANUC', collectInterval: 30, isEnabled: 1, lastCollectTime: '2026-04-25T17:36:38', failCount: 0, machineCount: 32 },
  { id: 2, name: 'FANUC-B栋', url: 'http://10.1.2.1/', brandId: 1, brandName: 'FANUC', collectInterval: 60, isEnabled: 1, lastCollectTime: '2026-04-25T17:35:38', failCount: 0, machineCount: 28 },
  { id: 3, name: 'SIEMENS-C栋', url: 'http://10.1.3.1/', brandId: 2, brandName: 'SIEMENS', collectInterval: 30, isEnabled: 0, lastCollectTime: null, failCount: 3, machineCount: 0 },
]

/** 重置mock数据为初始状态（供测试使用） */
export function resetAddressMockData() {
  addresses = [
    { id: 1, name: 'FANUC-A栋', url: 'http://10.1.1.1/', brandId: 1, brandName: 'FANUC', collectInterval: 30, isEnabled: 1, lastCollectTime: '2026-04-25T17:36:38', failCount: 0, machineCount: 32 },
    { id: 2, name: 'FANUC-B栋', url: 'http://10.1.2.1/', brandId: 1, brandName: 'FANUC', collectInterval: 60, isEnabled: 1, lastCollectTime: '2026-04-25T17:35:38', failCount: 0, machineCount: 28 },
    { id: 3, name: 'SIEMENS-C栋', url: 'http://10.1.3.1/', brandId: 2, brandName: 'SIEMENS', collectInterval: 30, isEnabled: 0, lastCollectTime: null, failCount: 3, machineCount: 0 },
  ]
}

const mock: MockMethod[] = [
  { url: '/mock-api/admin/collect-address', method: 'get', response: () => ({ code: 0, data: { items: addresses } }) },
  // 参数化路由：GET /mock-api/admin/collect-address/:id
  { url: '/mock-api/admin/collect-address/:id', method: 'get', response: ({ params }: any) => { const id = Number(params.id); const a = addresses.find((a: any) => a.id === id); return { code: 0, data: a || null } } },
  // 兼容旧URL格式
  { url: '/mock-api/admin/collect-address/detail', method: 'get', response: ({ query }: any) => { const id = Number(query.id); const a = addresses.find((a: any) => a.id === id); return { code: 0, data: a || null } } },
  { url: '/mock-api/admin/collect-address', method: 'post', response: () => ({ code: 0, message: 'success', data: { id: 4, name: '新地址' } }) },
  { url: '/mock-api/admin/collect-address/update', method: 'post', response: () => ({ code: 0, message: 'success', data: null }) },
  { url: '/mock-api/admin/collect-address/delete', method: 'post', response: () => ({ code: 0, message: 'success', data: null }) },
  { url: '/mock-api/admin/collect-address/toggle', method: 'post', response: () => ({ code: 0, message: 'success', data: null }) },
  // 参数化路由：GET /mock-api/admin/collect-address/:id/machines
  { url: '/mock-api/admin/collect-address/:id/machines', method: 'get', response: () => ({ code: 0, data: { items: [
    { machineId: 1, machineName: '西-1.8', deviceCode: 'fanake_1.8', workshopName: 'A栋', isOnline: 1, programName: '1566.NC', partCount: 580 },
    { machineId: 2, machineName: '西-1.10', deviceCode: 'fanake_1.10', workshopName: 'A栋', isOnline: 1, programName: 'O123.NC', partCount: 120 },
  ] } }) },
  // 兼容旧URL格式
  { url: '/mock-api/admin/collect-address/machines', method: 'get', response: () => ({ code: 0, data: { items: [
    { machineId: 1, machineName: '西-1.8', deviceCode: 'fanake_1.8', workshopName: 'A栋', isOnline: 1, programName: '1566.NC', partCount: 580 },
    { machineId: 2, machineName: '西-1.10', deviceCode: 'fanake_1.10', workshopName: 'A栋', isOnline: 1, programName: 'O123.NC', partCount: 120 },
  ] } }) },
  // 参数化路由：GET /mock-api/admin/collect-address/:id/collect-records
  { url: '/mock-api/admin/collect-address/:id/collect-records', method: 'get', response: () => ({ code: 0, data: { items: [
    { requestTime: '2026-04-25T17:36:38', duration: 1200, isSuccess: 1, machineCount: 32, failMachineCount: 0, machineName: '西-1.8' },
    { requestTime: '2026-04-25T17:35:38', duration: 3500, isSuccess: 1, machineCount: 31, failMachineCount: 1, machineName: '西-1.10' },
    { requestTime: '2026-04-25T17:35:08', duration: null, isSuccess: 0, machineCount: null, failMachineCount: null, machineName: '西-1.12' },
  ] } }) },
  // 兼容旧URL格式
  { url: '/mock-api/admin/collect-address/collect-records', method: 'get', response: () => ({ code: 0, data: { items: [
    { requestTime: '2026-04-25T17:36:38', duration: 1200, isSuccess: 1, machineCount: 32, failMachineCount: 0, machineName: '西-1.8' },
    { requestTime: '2026-04-25T17:35:38', duration: 3500, isSuccess: 1, machineCount: 31, failMachineCount: 1, machineName: '西-1.10' },
    { requestTime: '2026-04-25T17:35:08', duration: null, isSuccess: 0, machineCount: null, failMachineCount: null, machineName: '西-1.12' },
  ] } }) },
  // 参数化路由：GET /mock-api/admin/collect-address/:id/raw-json
  { url: '/mock-api/admin/collect-address/:id/raw-json', method: 'get', response: () => ({ code: 0, data: { rawJson: '[{device:fanake_1.8,tags:[{id:Tag5,value:1566.NC}]}]' } }) },
  // 兼容旧URL格式
  { url: '/mock-api/admin/collect-address/raw-json', method: 'get', response: () => ({ code: 0, data: { rawJson: '[{device:fanake_1.8,tags:[{id:Tag5,value:1566.NC}]}]' } }) },
  { url: '/mock-api/admin/brand/list', method: 'get', response: () => ({ code: 0, data: { items: [{ id: 1, brandName: 'FANUC' }, { id: 2, brandName: 'SIEMENS' }] } }) },
  { url: '/mock-api/admin/machine/list', method: 'get', response: ({ query }: any) => {
    const brandName = query?.brandName || ''
    const machines = brandName === 'FANUC' ? [
      { machineId: 1, machineName: '西-1.8', deviceCode: 'fanake_1.8', workshopName: 'A栋', isOnline: 1, programName: '1566.NC', partCount: 580 },
      { machineId: 2, machineName: '西-1.10', deviceCode: 'fanake_1.10', workshopName: 'A栋', isOnline: 1, programName: 'O123.NC', partCount: 120 },
    ] : [
      { machineId: 3, machineName: '东-2.3', deviceCode: 'sec_2.3', workshopName: 'B栋', isOnline: 0, programName: '2000.NC', partCount: 50 }
    ]
    return { code: 0, data: { items: machines } }
  } },
  // 批量删除采集地址
  { url: '/mock-api/admin/collect-address/batch-delete', method: 'post', response: ({ body }: any) => {
    const ids: number[] = body?.ids || []
    addresses = addresses.filter((a: any) => !ids.includes(a.id))
    return { code: 0, message: 'success', data: null }
  }},
  // 参数化路由：PUT /mock-api/admin/collect-address/:id/toggle（切换启用/停用）
  { url: '/mock-api/admin/collect-address/:id/toggle', method: 'put', response: ({ params }: any) => {
    const id = Number(params.id)
    const a = addresses.find((a: any) => a.id === id)
    if (a) (a as any).isEnabled = (a as any).isEnabled ? 0 : 1
    return { code: 0, message: 'success', data: null }
  }},
  // 参数化路由：DELETE /mock-api/admin/collect-address/:id（单个删除）
  { url: '/mock-api/admin/collect-address/:id', method: 'delete', response: ({ params }: any) => {
    const id = Number(params.id)
    addresses = addresses.filter((a: any) => a.id !== id)
    return { code: 0, message: 'success', data: null }
  }},
  // 参数化路由：PUT /mock-api/admin/collect-address/:id（编辑更新）
  { url: '/mock-api/admin/collect-address/:id', method: 'put', response: () => ({ code: 0, message: 'success', data: null }) },
  // 测试专用：重置mock数据
  { url: '/mock-api/test/reset-addresses', method: 'post', response: () => { resetAddressMockData(); return { code: 0, message: 'reset ok' } } },
]

export default mock
