var MOCK_TOKEN = 'mock_token_dev_' + Date.now()

var MOCK_USER = {
  user_id: 10001,
  open_id: 'mock_open_id_12345',
  nickname: '测试用户',
  avatar_url: '',
  phone: '',
  gender: 0,
  created_at: '2025-01-01T00:00:00.000Z'
}

var MOCK_DEVICE = {
  device_id: 'DEV001',
  name: '我的光子美容仪',
  firmware_version: '1.0.0',
  battery_level: 85,
  status: 'online',
  bind_time: '2025-01-01T00:00:00.000Z'
}

var MOCK_SUBSCRIPTION = {
  plan_type: 'trial',
  status: 'active',
  start_time: '2025-01-01T00:00:00.000Z',
  end_time: '2025-02-01T00:00:00.000Z',
  remaining_days: 30
}

var MOCK_TREATMENTS = [
  {
    session_id: 'S20250101001',
    device_id: 'DEV001',
    regions: [1, 2],
    wavelength: 630,
    brightness: 5,
    duration: 600000,
    mode: 1,
    avg_pd: 128,
    created_at: '2025-01-15T10:30:00.000Z'
  },
  {
    session_id: 'S20250102002',
    device_id: 'DEV001',
    regions: [1],
    wavelength: 850,
    brightness: 3,
    duration: 300000,
    mode: 2,
    avg_pd: 96,
    created_at: '2025-01-14T08:00:00.000Z'
  }
]

var mockHandlers = {
  'POST /api/v1/auth/login': function () {
    return {
      code: 0,
      data: {
        token: MOCK_TOKEN,
        user_id: MOCK_USER.user_id,
        user_info: MOCK_USER
      }
    }
  },

  'POST /api/v1/auth/refresh': function () {
    return { code: 0, data: { token: MOCK_TOKEN } }
  },

  'GET /api/v1/user/profile': function () {
    return { code: 0, data: MOCK_USER }
  },

  'PUT /api/v1/user/profile': function (data) {
    Object.assign(MOCK_USER, data)
    return { code: 0, data: MOCK_USER }
  },

  'POST /api/v1/device/bind': function (data) {
    var d = Object.assign({}, MOCK_DEVICE, { device_id: data.device_id || 'DEV001' })
    return {
      code: 0,
      data: {
        device: d,
        bind_token: 'mock_bind_token_' + Date.now(),
        subscription: MOCK_SUBSCRIPTION
      }
    }
  },

  'POST /api/v1/device/unbind': function () {
    return { code: 0, data: {} }
  },

  'GET /api/v1/device/list': function () {
    return { code: 0, data: { devices: [MOCK_DEVICE], total: 1 } }
  },

  'GET /api/v1/device/detail': function () {
    return { code: 0, data: MOCK_DEVICE }
  },

  'GET /api/v1/subscription/status': function () {
    return { code: 0, data: MOCK_SUBSCRIPTION }
  },

  'POST /api/v1/subscription/purchase': function (data) {
    return {
      code: 0,
      data: {
        order_id: 'ORD' + Date.now(),
        plan_type: data.plan_type,
        status: 'paid'
      }
    }
  },

  'POST /api/v1/subscription/verify': function () {
    return { code: 0, data: { valid: true } }
  },

  'POST /api/v1/treatment/sync': function (data) {
    return { code: 0, data: { session_id: data.session_id || 'S' + Date.now() } }
  },

  'GET /api/v1/treatment/history': function () {
    return { code: 0, data: { records: MOCK_TREATMENTS, total: MOCK_TREATMENTS.length } }
  }
}

function handle(method, path, data) {
  var key = method + ' ' + path
  var handler = mockHandlers[key]
  if (!handler) {
    return { code: -1, message: 'mock: 未定义的接口 ' + key }
  }
  return handler(data)
}

module.exports = {
  handle: handle,
  MOCK_TOKEN: MOCK_TOKEN,
  enabled: false
}
