import type { MockMethod } from './types'

let machines = [
  { id: 1, name: '西-1.8', deviceCode: 'fanake_1.8', workshopId: 1, workshopName: 'A栋', brandId: 1, brandName: 'FANUC', collectAddressId: 1, ipAddress: '10.1.1.8', isOnline: 1, workerId: 1, workerName: '张三', isEnabled: 1 },
  { id: 2, name: '西-1.10', deviceCode: 'fanake_1.10', workshopId: 1, workshopName: 'A栋', brandId: 1, brandName: 'FANUC', collectAddressId: 1, ipAddress: '10.1.1.10', isOnline: 1, workerId: 2, workerName: '李四', isEnabled: 1 },
  { id: 3, name: '东-2.0', deviceCode: 'fanake_2.0', workshopId: 2, workshopName: 'B栋', brandId: 1, brandName: 'FANUC', collectAddressId: 2, ipAddress: '10.1.2.0', isOnline: 0, workerId: 3, workerName: '王五', isEnabled: 1 },
  { id: 4, name: '东-2.5', deviceCode: 'siemens_2.5', workshopId: 2, workshopName: 'B栋', brandId: 2, brandName: 'SIEMENS', collectAddressId: 2, ipAddress: '10.1.2.5', isOnline: 0, workerId: null, workerName: null, isEnabled: 0 },
  { id: 5, name: '南-3.1', deviceCode: 'fanake_3.1', workshopId: 3, workshopName: 'C栋', brandId: 1, brandName: 'FANUC', collectAddressId: 1, ipAddress: '10.1.3.1', isOnline: 1, workerId: 4, workerName: '赵六', isEnabled: 1 },
]

/** 重置mock数据为初始状态（供测试使用） */
export function resetMachineMockData() {
  machines = [
    { id: 1, name: '西-1.8', deviceCode: 'fanake_1.8', workshopId: 1, workshopName: 'A栋', brandId: 1, brandName: 'FANUC', collectAddressId: 1, ipAddress: '10.1.1.8', isOnline: 1, workerId: 1, workerName: '张三', isEnabled: 1 },
    { id: 2, name: '西-1.10', deviceCode: 'fanake_1.10', workshopId: 1, workshopName: 'A栋', brandId: 1, brandName: 'FANUC', collectAddressId: 1, ipAddress: '10.1.1.10', isOnline: 1, workerId: 2, workerName: '李四', isEnabled: 1 },
    { id: 3, name: '东-2.0', deviceCode: 'fanake_2.0', workshopId: 2, workshopName: 'B栋', brandId: 1, brandName: 'FANUC', collectAddressId: 2, ipAddress: '10.1.2.0', isOnline: 0, workerId: 3, workerName: '王五', isEnabled: 1 },
    { id: 4, name: '东-2.5', deviceCode: 'siemens_2.5', workshopId: 2, workshopName: 'B栋', brandId: 2, brandName: 'SIEMENS', collectAddressId: 2, ipAddress: '10.1.2.5', isOnline: 0, workerId: null, workerName: null, isEnabled: 0 },
    { id: 5, name: '南-3.1', deviceCode: 'fanake_3.1', workshopId: 3, workshopName: 'C栋', brandId: 1, brandName: 'FANUC', collectAddressId: 1, ipAddress: '10.1.3.1', isOnline: 1, workerId: 4, workerName: '赵六', isEnabled: 1 },
  ]
}

const mock: MockMethod[] = [
  {
    url: '/mock-api/admin/machine',
    method: 'get',
    response: ({ query }: any) => {
      let items = [...machines]
      if (query.workshopId) items = items.filter((m: any) => m.workshopId === Number(query.workshopId))
      if (query.isOnline !== undefined && query.isOnline !== '') items = items.filter((m: any) => m.isOnline === Number(query.isOnline))
      if (query.brandId) items = items.filter((m: any) => m.brandId === Number(query.brandId))
      if (query.brandName) items = items.filter((m: any) => m.brandName === query.brandName)
      if (query.keyword) items = items.filter((m: any) => m.name.includes(query.keyword) || m.deviceCode.includes(query.keyword))
      return { code: 0, data: { items, total: items.length, page: Number(query.page || 1), pageSize: Number(query.pageSize || 20) } }
    },
  },
  {
    // 参数化路由：GET /mock-api/admin/machine/:id
    url: '/mock-api/admin/machine/:id',
    method: 'get',
    response: ({ params }: any) => {
      const id = Number(params.id)
      const m = machines.find((m: any) => m.id === id)
      return { code: 0, data: m || null }
    },
  },
  {
    // 兼容旧URL格式
    url: '/mock-api/admin/machine/detail',
    method: 'get',
    response: ({ query }: any) => {
      const id = Number(query.id)
      const m = machines.find((m: any) => m.id === id)
      return { code: 0, data: m || null }
    },
  },
  {
    // 参数化路由：GET /mock-api/admin/machine/:id/status
    url: '/mock-api/admin/machine/:id/status',
    method: 'get',
    response: () => ({
      code: 0,
      data: { programName: '1566.NC', partCount: 580, runStatus: '运行中', operateMode: '自动', spindleSpeedSet: 3000, feedSpeedSet: 500, spindleSpeedActual: 2980, feedSpeedActual: 480, spindleLoad: 65, machiningStatus: 'G01', lastCollectTime: '2026-04-25T17:36:38' },
    }),
  },
  {
    // 兼容旧URL格式
    url: '/mock-api/admin/machine/status',
    method: 'get',
    response: () => ({
      code: 0,
      data: { programName: '1566.NC', partCount: 580, runStatus: '运行中', operateMode: '自动', spindleSpeedSet: 3000, feedSpeedSet: 500, spindleSpeedActual: 2980, feedSpeedActual: 480, spindleLoad: 65, machiningStatus: 'G01', lastCollectTime: '2026-04-25T17:36:38' },
    }),
  },
  {
    url: '/mock-api/admin/machine',
    method: 'post',
    response: () => ({ code: 0, message: 'success', data: { id: 161, name: '新机床' } }),
  },
  {
    url: '/mock-api/admin/machine/update',
    method: 'post',
    response: () => ({ code: 0, message: 'success', data: null }),
  },
  {
    url: '/mock-api/admin/machine/delete',
    method: 'post',
    response: () => ({ code: 0, message: 'success', data: null }),
  },
  {
    url: '/mock-api/admin/machine/batch-status',
    method: 'post',
    response: () => ({ code: 0, message: 'success', data: null }),
  },
  {
    // 参数化路由：GET /mock-api/admin/machine/:id/production/today
    url: '/mock-api/admin/machine/:id/production/today',
    method: 'get',
    response: () => ({
      code: 0,
      data: { items: [
        { programName: '1566.NC', quantity: 580, runTime: '4h20m', cuttingTime: '3h50m' },
        { programName: 'O123.NC', quantity: 120, runTime: '2h10m', cuttingTime: '1h45m' },
      ] },
    }),
  },
  {
    // 兼容旧URL格式
    url: '/mock-api/admin/machine/production/today',
    method: 'get',
    response: () => ({
      code: 0,
      data: { items: [
        { programName: '1566.NC', quantity: 580, runTime: '4h20m', cuttingTime: '3h50m' },
        { programName: 'O123.NC', quantity: 120, runTime: '2h10m', cuttingTime: '1h45m' },
      ] },
    }),
  },
  {
    // 参数化路由：GET /mock-api/admin/machine/:id/production/trend
    url: '/mock-api/admin/machine/:id/production/trend',
    method: 'get',
    response: () => ({
      code: 0,
      data: { items: [
        { date: '2026-04-19', quantity: 820 }, { date: '2026-04-20', quantity: 760 },
        { date: '2026-04-21', quantity: 910 }, { date: '2026-04-22', quantity: 850 },
        { date: '2026-04-23', quantity: 780 }, { date: '2026-04-24', quantity: 900 },
        { date: '2026-04-25', quantity: 700 },
      ] },
    }),
  },
  {
    // 兼容旧URL格式
    url: '/mock-api/admin/machine/production/trend',
    method: 'get',
    response: () => ({
      code: 0,
      data: { items: [
        { date: '2026-04-19', quantity: 820 }, { date: '2026-04-20', quantity: 760 },
        { date: '2026-04-21', quantity: 910 }, { date: '2026-04-22', quantity: 850 },
        { date: '2026-04-23', quantity: 780 }, { date: '2026-04-24', quantity: 900 },
        { date: '2026-04-25', quantity: 700 },
      ] },
    }),
  },
  {
    // 参数化路由：GET /mock-api/admin/machine/:id/collect-records
    url: '/mock-api/admin/machine/:id/collect-records',
    method: 'get',
    response: () => ({
      code: 0,
      data: { items: [
        { collectTime: '2026-04-25T17:36:35', programName: '1566.NC', partCount: 580, runStatus: '运行中' },
        { collectTime: '2026-04-25T17:36:05', programName: '1566.NC', partCount: 579, runStatus: '运行中' },
        { collectTime: '2026-04-25T17:35:35', programName: '1566.NC', partCount: 578, runStatus: '运行中' },
      ] },
    }),
  },
  {
    // 兼容旧URL格式
    url: '/mock-api/admin/machine/collect-records',
    method: 'get',
    response: () => ({
      code: 0,
      data: { items: [
        { collectTime: '2026-04-25T17:36:35', programName: '1566.NC', partCount: 580, runStatus: '运行中' },
        { collectTime: '2026-04-25T17:36:05', programName: '1566.NC', partCount: 579, runStatus: '运行中' },
        { collectTime: '2026-04-25T17:35:35', programName: '1566.NC', partCount: 578, runStatus: '运行中' },
      ] },
    }),
  },
  {
    url: '/mock-api/admin/workshop/list',
    method: 'get',
    response: () => ({ code: 0, data: { items: [{ id: 1, name: 'A栋' }, { id: 2, name: 'B栋' }, { id: 3, name: 'C栋' }] } }),
  },
  {
    url: '/mock-api/admin/brand/list',
    method: 'get',
    response: () => ({ code: 0, data: { items: [{ id: 1, brandName: 'FANUC' }, { id: 2, brandName: 'SIEMENS' }, { id: 3, brandName: 'MITSUBISHI' }] } }),
  },
  {
    url: '/mock-api/admin/collect-address/list',
    method: 'get',
    response: () => ({ code: 0, data: { items: [{ id: 1, name: 'FANUC-A栋', brandId: 1 }, { id: 2, name: 'FANUC-B栋', brandId: 1 }] } }),
  },
  {
    url: '/mock-api/admin/worker/list',
    method: 'get',
    response: () => ({ code: 0, data: { items: [{ id: 1, name: '张三', code: 'W001' }, { id: 2, name: '李四', code: 'W002' }, { id: 3, name: '王五', code: 'W003' }] } }),
  },
  // 批量删除机床
  {
    url: '/mock-api/admin/machine/batch-delete',
    method: 'post',
    response: ({ body }: any) => {
      const ids: number[] = body?.ids || []
      machines = machines.filter((m: any) => !ids.includes(m.id))
      return { code: 0, message: 'success', data: null }
    },
  },
  // 参数化路由：PUT /mock-api/admin/machine/:id/toggle（切换启用/停用）
  {
    url: '/mock-api/admin/machine/:id/toggle',
    method: 'put',
    response: ({ params }: any) => {
      const id = Number(params.id)
      const m = machines.find((m: any) => m.id === id)
      if (m) (m as any).isEnabled = (m as any).isEnabled ? 0 : 1
      return { code: 0, message: 'success', data: null }
    },
  },
  // 参数化路由：DELETE /mock-api/admin/machine/:id（单个删除）
  {
    url: '/mock-api/admin/machine/:id',
    method: 'delete',
    response: ({ params }: any) => {
      const id = Number(params.id)
      machines = machines.filter((m: any) => m.id !== id)
      return { code: 0, message: 'success', data: null }
    },
  },
  // 参数化路由：PUT /mock-api/admin/machine/:id（编辑更新）
  {
    url: '/mock-api/admin/machine/:id',
    method: 'put',
    response: () => ({ code: 0, message: 'success', data: null }),
  },
  // 测试专用：重置mock数据
  {
    url: '/mock-api/test/reset-machines',
    method: 'post',
    response: () => { resetMachineMockData(); return { code: 0, message: 'reset ok' } },
  },
]

export default mock
