import type { MockMethod } from './types'

let workers = [
  { id: 1, code: 'W001', name: '张三', isEnabled: 1, machineCount: 2, machineNames: '西-1.8,西-2.0' },
  { id: 2, code: 'W002', name: '李四', isEnabled: 1, machineCount: 1, machineNames: '西-1.10' },
  { id: 3, code: 'W003', name: '王五', isEnabled: 0, machineCount: 0, machineNames: '-' },
]

/** 重置mock数据为初始状态（供测试使用） */
export function resetWorkerMockData() {
  workers = [
    { id: 1, code: 'W001', name: '张三', isEnabled: 1, machineCount: 2, machineNames: '西-1.8,西-2.0' },
    { id: 2, code: 'W002', name: '李四', isEnabled: 1, machineCount: 1, machineNames: '西-1.10' },
    { id: 3, code: 'W003', name: '王五', isEnabled: 0, machineCount: 0, machineNames: '-' },
  ]
}

const mock: MockMethod[] = [
  { url: '/mock-api/admin/worker', method: 'get', response: ({ query }: any) => {
    let items = [...workers]
    const page = Number(query.page) || 1
    const pageSize = Number(query.pageSize) || 20
    const original = items
    if (query.isEnabled !== undefined && query.isEnabled !== '') items = items.filter((w: any) => w.isEnabled === Number(query.isEnabled))
    if (query.keyword) items = items.filter((w: any) => w.name.includes(query.keyword) || w.code.includes(query.keyword))
    const total = original.length
    const start = (page - 1) * pageSize
    const end = start + pageSize
    items = items.slice(start, end)
    return { code: 0, data: { items, total, page, pageSize } }
  }},
  // 参数化路由：GET /mock-api/admin/worker/:id
  { url: '/mock-api/admin/worker/:id', method: 'get', response: ({ params }: any) => {
    const w = workers.find((w: any) => w.id === Number(params.id))
    return { code: 0, data: w || null }
  }},
  // 兼容旧URL格式
  { url: '/mock-api/admin/worker/detail', method: 'get', response: ({ query }: any) => {
    const w = workers.find((w: any) => w.id === Number(query.id))
    return { code: 0, data: w || null }
  }},
  { url: '/mock-api/admin/worker', method: 'post', response: () => ({ code: 0, message: 'success', data: { id: 4, name: '赵六' } }) },
  { url: '/mock-api/admin/worker/update', method: 'post', response: () => ({ code: 0, message: 'success', data: null }) },
  { url: '/mock-api/admin/worker/delete', method: 'post', response: () => ({ code: 0, message: 'success', data: null }) },
  { url: '/mock-api/admin/worker/batch-status', method: 'post', response: () => ({ code: 0, message: 'success', data: null }) },
  // 参数化路由：GET /mock-api/admin/worker/:id/machines
  { url: '/mock-api/admin/worker/:id/machines', method: 'get', response: () => ({ code: 0, data: { items: [
    { machineId: 1, machineName: '西-1.8', deviceCode: 'fanake_1.8', workshopName: 'A栋', brandName: 'FANUC', isOnline: 1, programName: '1566.NC' },
    { machineId: 2, machineName: '西-2.0', deviceCode: 'fanake_2.0', workshopName: 'A栋', brandName: 'FANUC', isOnline: 0, programName: null },
  ] } }) },
  // 兼容旧URL格式
  { url: '/mock-api/admin/worker/machines', method: 'get', response: () => ({ code: 0, data: { items: [
    { machineId: 1, machineName: '西-1.8', deviceCode: 'fanake_1.8', workshopName: 'A栋', brandName: 'FANUC', isOnline: 1, programName: '1566.NC' },
    { machineId: 2, machineName: '西-2.0', deviceCode: 'fanake_2.0', workshopName: 'A栋', brandName: 'FANUC', isOnline: 0, programName: null },
  ] } }) },
  // 参数化路由：GET /mock-api/admin/worker/:id/production/today
  { url: '/mock-api/admin/worker/:id/production/today', method: 'get', response: () => ({ code: 0, data: { items: [
    { machineName: '西-1.8', programName: '1566.NC', quantity: 580, runTime: '4h20m', cuttingTime: '3h50m' },
    { machineName: '西-2.0', programName: '-', quantity: null, runTime: '-', cuttingTime: '-' },
  ] } }) },
  // 兼容旧URL格式
  { url: '/mock-api/admin/worker/production/today', method: 'get', response: () => ({ code: 0, data: { items: [
    { machineName: '西-1.8', programName: '1566.NC', quantity: 580, runTime: '4h20m', cuttingTime: '3h50m' },
    { machineName: '西-2.0', programName: '-', quantity: null, runTime: '-', cuttingTime: '-' },
  ] } }) },
  // 参数化路由：GET /mock-api/admin/worker/:id/production/trend
  { url: '/mock-api/admin/worker/:id/production/trend', method: 'get', response: () => ({ code: 0, data: { items: [
    { date: '2026-04-19', quantity: 980 }, { date: '2026-04-20', quantity: 920 },
    { date: '2026-04-21', quantity: 1100 }, { date: '2026-04-22', quantity: 1050 },
    { date: '2026-04-23', quantity: 990 }, { date: '2026-04-24', quantity: 1080 },
    { date: '2026-04-25', quantity: 580 },
  ] } }) },
  // 兼容旧URL格式
  { url: '/mock-api/admin/worker/production/trend', method: 'get', response: () => ({ code: 0, data: { items: [
    { date: '2026-04-19', quantity: 980 }, { date: '2026-04-20', quantity: 920 },
    { date: '2026-04-21', quantity: 1100 }, { date: '2026-04-22', quantity: 1050 },
    { date: '2026-04-23', quantity: 990 }, { date: '2026-04-24', quantity: 1080 },
    { date: '2026-04-25', quantity: 580 },
  ] } }) },
  { url: '/mock-api/admin/worker/available-machines', method: 'get', response: () => ({ code: 0, data: { items: [
    { id: 5, name: '东-2.5', deviceCode: 'siemens_2.5', workshopName: 'B栋' },
    { id: 8, name: '北-4.1', deviceCode: 'fanake_4.1', workshopName: 'C栋' },
  ] } }) },
  // 批量删除工人
  { url: '/mock-api/admin/worker/batch-delete', method: 'post', response: ({ body }: any) => {
    const ids: number[] = body?.ids || []
    workers = workers.filter((w: any) => !ids.includes(w.id))
    return { code: 0, message: 'success', data: null }
  }},
  // 参数化路由：PUT /mock-api/admin/worker/:id/toggle（切换启用/停用）
  { url: '/mock-api/admin/worker/:id/toggle', method: 'put', response: ({ params }: any) => {
    const id = Number(params.id)
    const w = workers.find((w: any) => w.id === id)
    if (w) (w as any).isEnabled = (w as any).isEnabled ? 0 : 1
    return { code: 0, message: 'success', data: null }
  }},
  // 参数化路由：DELETE /mock-api/admin/worker/:id（单个删除）
  { url: '/mock-api/admin/worker/:id', method: 'delete', response: ({ params }: any) => {
    const id = Number(params.id)
    workers = workers.filter((w: any) => w.id !== id)
    return { code: 0, message: 'success', data: null }
  }},
  // 参数化路由：PUT /mock-api/admin/worker/:id（编辑更新）
  { url: '/mock-api/admin/worker/:id', method: 'put', response: () => ({ code: 0, message: 'success', data: null }) },
  // 测试专用：重置mock数据
  { url: '/mock-api/test/reset-workers', method: 'post', response: () => { resetWorkerMockData(); return { code: 0, message: 'reset ok' } } },
]

export default mock
