/**
 * 摄像头种子数据脚本
 * 在浏览器控制台中运行此脚本，添加70条多样化的摄像头数据
 *
 * 使用方法：
 * 1. 登录到 http://localhost:3000 (或生产环境)
 * 2. 打开浏览器开发者工具 (F12)
 * 3. 复制此脚本内容到控制台运行
 */

const API_BASE = window.location.origin.includes('localhost') ? 'https://tg-live-game.pwtk.cc/api' : '/api'

// 从 cookie 获取 token
function getToken() {
  const match = document.cookie.match(new RegExp('(^| )token=([^;]+)'))
  return match ? decodeURIComponent(match[2]) : null
}

// 发送 API 请求
async function apiPost(path, data) {
  const token = getToken()
  if (!token) {
    throw new Error('未登录，请先登录系统')
  }

  const response = await fetch(`${API_BASE}${path}`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`
    },
    body: JSON.stringify(data)
  })

  return response.json()
}

// 品牌列表
const brands = [
  'Hikvision',
  'Dahua',
  'Axis',
  'Sony',
  'Bosch',
  'Uniview',
  'Hanwha',
  'Pelco',
  'Vivotek',
  'Tiandy',
  'Ezviz',
  'Reolink',
  'Amcrest',
  'Lorex',
  'Swann'
]

// 机器ID列表 (模拟不同的部署位置)
const machineIds = [
  'FACTORY-A1',
  'FACTORY-A2',
  'FACTORY-B1',
  'WAREHOUSE-01',
  'WAREHOUSE-02',
  'WAREHOUSE-03',
  'OFFICE-HQ',
  'OFFICE-BRANCH1',
  'OFFICE-BRANCH2',
  'RETAIL-STORE1',
  'RETAIL-STORE2',
  'PARKING-MAIN',
  'PARKING-EAST',
  'DATACENTER-01',
  'DATACENTER-02'
]

// 生成70条多样化摄像头数据
const cameras = [
  // 工厂区域 (15台)
  {
    name: '生产线A-入口监控',
    ip: '192.168.1.101',
    brand: 'Hikvision',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-A1',
    location: 'factory'
  },
  {
    name: '生产线A-装配区',
    ip: '192.168.1.102',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'FACTORY-A1',
    location: 'factory'
  },
  {
    name: '生产线A-质检站',
    ip: '192.168.1.103',
    brand: 'Axis',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-A1',
    location: 'factory'
  },
  {
    name: '生产线A-包装区',
    ip: '192.168.1.104',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'FACTORY-A1',
    location: 'factory'
  },
  {
    name: '生产线A-出货口',
    ip: '192.168.1.105',
    brand: 'Uniview',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-A1',
    location: 'factory'
  },
  {
    name: '生产线B-原料区',
    ip: '192.168.1.111',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'FACTORY-A2',
    location: 'factory'
  },
  {
    name: '生产线B-加工中心',
    ip: '192.168.1.112',
    brand: 'Sony',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-A2',
    location: 'factory'
  },
  {
    name: '生产线B-焊接车间',
    ip: '192.168.1.113',
    brand: 'Bosch',
    capability: 'switch_only',
    machineId: 'FACTORY-A2',
    location: 'factory'
  },
  {
    name: '生产线B-喷漆房',
    ip: '192.168.1.114',
    brand: 'Hanwha',
    capability: 'switch_only',
    machineId: 'FACTORY-A2',
    location: 'factory'
  },
  {
    name: '生产线B-成品检验',
    ip: '192.168.1.115',
    brand: 'Hikvision',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-A2',
    location: 'factory'
  },
  {
    name: '厂房C-机械臂区',
    ip: '192.168.2.101',
    brand: 'Axis',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-B1',
    location: 'factory'
  },
  {
    name: '厂房C-物料暂存',
    ip: '192.168.2.102',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'FACTORY-B1',
    location: 'factory'
  },
  {
    name: '厂房C-AGV通道',
    ip: '192.168.2.103',
    brand: 'Uniview',
    capability: 'switch_only',
    machineId: 'FACTORY-B1',
    location: 'factory'
  },
  {
    name: '厂房C-配电室',
    ip: '192.168.2.104',
    brand: 'Tiandy',
    capability: 'switch_only',
    machineId: 'FACTORY-B1',
    location: 'factory'
  },
  {
    name: '厂房C-消防通道',
    ip: '192.168.2.105',
    brand: 'Hikvision',
    capability: 'ptz_enabled',
    machineId: 'FACTORY-B1',
    location: 'factory'
  },

  // 仓库区域 (15台)
  {
    name: '仓库1-入库区',
    ip: '192.168.3.101',
    brand: 'Dahua',
    capability: 'ptz_enabled',
    machineId: 'WAREHOUSE-01',
    location: 'warehouse'
  },
  {
    name: '仓库1-货架A区',
    ip: '192.168.3.102',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-01',
    location: 'warehouse'
  },
  {
    name: '仓库1-货架B区',
    ip: '192.168.3.103',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-01',
    location: 'warehouse'
  },
  {
    name: '仓库1-叉车通道',
    ip: '192.168.3.104',
    brand: 'Axis',
    capability: 'ptz_enabled',
    machineId: 'WAREHOUSE-01',
    location: 'warehouse'
  },
  {
    name: '仓库1-出库区',
    ip: '192.168.3.105',
    brand: 'Uniview',
    capability: 'ptz_enabled',
    machineId: 'WAREHOUSE-01',
    location: 'warehouse'
  },
  {
    name: '仓库2-危化品区',
    ip: '192.168.3.111',
    brand: 'Bosch',
    capability: 'ptz_enabled',
    machineId: 'WAREHOUSE-02',
    location: 'warehouse'
  },
  {
    name: '仓库2-冷藏区',
    ip: '192.168.3.112',
    brand: 'Sony',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-02',
    location: 'warehouse'
  },
  {
    name: '仓库2-高值品区',
    ip: '192.168.3.113',
    brand: 'Pelco',
    capability: 'ptz_enabled',
    machineId: 'WAREHOUSE-02',
    location: 'warehouse'
  },
  {
    name: '仓库2-分拣中心',
    ip: '192.168.3.114',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-02',
    location: 'warehouse'
  },
  {
    name: '仓库2-打包区',
    ip: '192.168.3.115',
    brand: 'Vivotek',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-02',
    location: 'warehouse'
  },
  {
    name: '仓库3-散货区',
    ip: '192.168.3.121',
    brand: 'Tiandy',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-03',
    location: 'warehouse'
  },
  {
    name: '仓库3-托盘区',
    ip: '192.168.3.122',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-03',
    location: 'warehouse'
  },
  {
    name: '仓库3-装车月台',
    ip: '192.168.3.123',
    brand: 'Dahua',
    capability: 'ptz_enabled',
    machineId: 'WAREHOUSE-03',
    location: 'warehouse'
  },
  {
    name: '仓库3-验收区',
    ip: '192.168.3.124',
    brand: 'Ezviz',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-03',
    location: 'warehouse'
  },
  {
    name: '仓库3-退货处理',
    ip: '192.168.3.125',
    brand: 'Reolink',
    capability: 'switch_only',
    machineId: 'WAREHOUSE-03',
    location: 'warehouse'
  },

  // 办公区域 (15台)
  {
    name: '总部大堂',
    ip: '192.168.10.101',
    brand: 'Axis',
    capability: 'ptz_enabled',
    machineId: 'OFFICE-HQ',
    location: 'office'
  },
  {
    name: '总部前台',
    ip: '192.168.10.102',
    brand: 'Sony',
    capability: 'switch_only',
    machineId: 'OFFICE-HQ',
    location: 'office'
  },
  {
    name: '总部会议室A',
    ip: '192.168.10.103',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'OFFICE-HQ',
    location: 'office'
  },
  {
    name: '总部会议室B',
    ip: '192.168.10.104',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'OFFICE-HQ',
    location: 'office'
  },
  {
    name: '总部电梯厅',
    ip: '192.168.10.105',
    brand: 'Uniview',
    capability: 'switch_only',
    machineId: 'OFFICE-HQ',
    location: 'office'
  },
  {
    name: '分公司1-入口',
    ip: '192.168.11.101',
    brand: 'Hanwha',
    capability: 'ptz_enabled',
    machineId: 'OFFICE-BRANCH1',
    location: 'office'
  },
  {
    name: '分公司1-开放办公区',
    ip: '192.168.11.102',
    brand: 'Bosch',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH1',
    location: 'office'
  },
  {
    name: '分公司1-茶水间',
    ip: '192.168.11.103',
    brand: 'Amcrest',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH1',
    location: 'office'
  },
  {
    name: '分公司1-走廊',
    ip: '192.168.11.104',
    brand: 'Lorex',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH1',
    location: 'office'
  },
  {
    name: '分公司1-档案室',
    ip: '192.168.11.105',
    brand: 'Swann',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH1',
    location: 'office'
  },
  {
    name: '分公司2-大厅',
    ip: '192.168.12.101',
    brand: 'Hikvision',
    capability: 'ptz_enabled',
    machineId: 'OFFICE-BRANCH2',
    location: 'office'
  },
  {
    name: '分公司2-培训室',
    ip: '192.168.12.102',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH2',
    location: 'office'
  },
  {
    name: '分公司2-财务室',
    ip: '192.168.12.103',
    brand: 'Axis',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH2',
    location: 'office'
  },
  {
    name: '分公司2-服务器机房',
    ip: '192.168.12.104',
    brand: 'Pelco',
    capability: 'ptz_enabled',
    machineId: 'OFFICE-BRANCH2',
    location: 'office'
  },
  {
    name: '分公司2-楼梯间',
    ip: '192.168.12.105',
    brand: 'Vivotek',
    capability: 'switch_only',
    machineId: 'OFFICE-BRANCH2',
    location: 'office'
  },

  // 零售门店 (10台)
  {
    name: '门店1-收银台',
    ip: '192.168.20.101',
    brand: 'Hikvision',
    capability: 'ptz_enabled',
    machineId: 'RETAIL-STORE1',
    location: 'retail'
  },
  {
    name: '门店1-商品陈列区',
    ip: '192.168.20.102',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'RETAIL-STORE1',
    location: 'retail'
  },
  {
    name: '门店1-试衣间走廊',
    ip: '192.168.20.103',
    brand: 'Uniview',
    capability: 'switch_only',
    machineId: 'RETAIL-STORE1',
    location: 'retail'
  },
  {
    name: '门店1-库房入口',
    ip: '192.168.20.104',
    brand: 'Tiandy',
    capability: 'switch_only',
    machineId: 'RETAIL-STORE1',
    location: 'retail'
  },
  {
    name: '门店1-出入口',
    ip: '192.168.20.105',
    brand: 'Ezviz',
    capability: 'ptz_enabled',
    machineId: 'RETAIL-STORE1',
    location: 'retail'
  },
  {
    name: '门店2-主通道',
    ip: '192.168.21.101',
    brand: 'Reolink',
    capability: 'ptz_enabled',
    machineId: 'RETAIL-STORE2',
    location: 'retail'
  },
  {
    name: '门店2-生鲜区',
    ip: '192.168.21.102',
    brand: 'Amcrest',
    capability: 'switch_only',
    machineId: 'RETAIL-STORE2',
    location: 'retail'
  },
  {
    name: '门店2-酒水区',
    ip: '192.168.21.103',
    brand: 'Lorex',
    capability: 'switch_only',
    machineId: 'RETAIL-STORE2',
    location: 'retail'
  },
  {
    name: '门店2-自助结账',
    ip: '192.168.21.104',
    brand: 'Swann',
    capability: 'ptz_enabled',
    machineId: 'RETAIL-STORE2',
    location: 'retail'
  },
  {
    name: '门店2-员工通道',
    ip: '192.168.21.105',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'RETAIL-STORE2',
    location: 'retail'
  },

  // 停车场 (8台)
  {
    name: '停车场A-入口闸机',
    ip: '192.168.30.101',
    brand: 'Dahua',
    capability: 'ptz_enabled',
    machineId: 'PARKING-MAIN',
    location: 'parking'
  },
  {
    name: '停车场A-出口闸机',
    ip: '192.168.30.102',
    brand: 'Hikvision',
    capability: 'ptz_enabled',
    machineId: 'PARKING-MAIN',
    location: 'parking'
  },
  {
    name: '停车场A-B1层',
    ip: '192.168.30.103',
    brand: 'Uniview',
    capability: 'switch_only',
    machineId: 'PARKING-MAIN',
    location: 'parking'
  },
  {
    name: '停车场A-B2层',
    ip: '192.168.30.104',
    brand: 'Axis',
    capability: 'switch_only',
    machineId: 'PARKING-MAIN',
    location: 'parking'
  },
  {
    name: '停车场B-车位引导',
    ip: '192.168.31.101',
    brand: 'Bosch',
    capability: 'ptz_enabled',
    machineId: 'PARKING-EAST',
    location: 'parking'
  },
  {
    name: '停车场B-充电桩区',
    ip: '192.168.31.102',
    brand: 'Sony',
    capability: 'switch_only',
    machineId: 'PARKING-EAST',
    location: 'parking'
  },
  {
    name: '停车场B-无障碍车位',
    ip: '192.168.31.103',
    brand: 'Hanwha',
    capability: 'switch_only',
    machineId: 'PARKING-EAST',
    location: 'parking'
  },
  {
    name: '停车场B-摩托车区',
    ip: '192.168.31.104',
    brand: 'Pelco',
    capability: 'switch_only',
    machineId: 'PARKING-EAST',
    location: 'parking'
  },

  // 数据中心 (7台)
  {
    name: '机房A-服务器机柜',
    ip: '10.0.1.101',
    brand: 'Axis',
    capability: 'ptz_enabled',
    machineId: 'DATACENTER-01',
    location: 'datacenter'
  },
  {
    name: '机房A-网络机柜',
    ip: '10.0.1.102',
    brand: 'Sony',
    capability: 'switch_only',
    machineId: 'DATACENTER-01',
    location: 'datacenter'
  },
  {
    name: '机房A-UPS室',
    ip: '10.0.1.103',
    brand: 'Bosch',
    capability: 'switch_only',
    machineId: 'DATACENTER-01',
    location: 'datacenter'
  },
  {
    name: '机房A-冷通道',
    ip: '10.0.1.104',
    brand: 'Hikvision',
    capability: 'switch_only',
    machineId: 'DATACENTER-01',
    location: 'datacenter'
  },
  {
    name: '机房B-热通道',
    ip: '10.0.2.101',
    brand: 'Dahua',
    capability: 'switch_only',
    machineId: 'DATACENTER-02',
    location: 'datacenter'
  },
  {
    name: '机房B-消防系统',
    ip: '10.0.2.102',
    brand: 'Pelco',
    capability: 'ptz_enabled',
    machineId: 'DATACENTER-02',
    location: 'datacenter'
  },
  {
    name: '机房B-门禁区',
    ip: '10.0.2.103',
    brand: 'Vivotek',
    capability: 'switch_only',
    machineId: 'DATACENTER-02',
    location: 'datacenter'
  }
]

// 生成通道数据
function generateChannels(cameraId, name, ip, capability) {
  const channels = []
  const channelCount =
    capability === 'ptz_enabled' ? Math.floor(Math.random() * 2) + 2 : Math.floor(Math.random() * 2) + 1

  const channelNames = {
    ptz_enabled: ['主画面', '全景视角', '细节特写', '广角监控'],
    switch_only: ['标清画面', '高清画面', '夜视模式']
  }

  const names = capability === 'ptz_enabled' ? channelNames.ptz_enabled : channelNames.switch_only

  for (let i = 0; i < channelCount; i++) {
    channels.push({
      channelId: `${cameraId}-ch${i + 1}`,
      name: names[i] || `通道${i + 1}`,
      rtspUrl: `rtsp://${ip}:554/stream${i + 1}`,
      defaultView: i === 0
    })
  }

  return channels
}

// 执行添加摄像头
async function seedCameras() {
  console.log('开始添加摄像头数据...')
  console.log(`总计: ${cameras.length} 台摄像头`)

  let successCount = 0
  let failCount = 0
  const errors = []

  for (let i = 0; i < cameras.length; i++) {
    const cam = cameras[i]
    const cameraId = `CAM-${String(i + 1).padStart(3, '0')}`

    const cameraData = {
      cameraId: cameraId,
      name: cam.name,
      ip: cam.ip,
      port: 554,
      username: 'admin',
      password: 'admin123',
      brand: cam.brand,
      capability: cam.capability,
      machineId: cam.machineId,
      channels: generateChannels(cameraId, cam.name, cam.ip, cam.capability)
    }

    try {
      const result = await apiPost('/admin/cameras/add', cameraData)

      if (result.success) {
        successCount++
        console.log(`✅ [${i + 1}/${cameras.length}] ${cam.name} - 添加成功`)
      } else {
        failCount++
        errors.push({ camera: cam.name, error: result.errMessage || '未知错误' })
        console.log(`❌ [${i + 1}/${cameras.length}] ${cam.name} - 失败: ${result.errMessage}`)
      }
    } catch (err) {
      failCount++
      errors.push({ camera: cam.name, error: err.message })
      console.log(`❌ [${i + 1}/${cameras.length}] ${cam.name} - 异常: ${err.message}`)
    }

    // 添加小延迟避免请求过快
    await new Promise((resolve) => setTimeout(resolve, 100))
  }

  console.log('\n========== 添加完成 ==========')
  console.log(`✅ 成功: ${successCount} 台`)
  console.log(`❌ 失败: ${failCount} 台`)

  if (errors.length > 0) {
    console.log('\n失败详情:')
    errors.forEach((e) => console.log(`  - ${e.camera}: ${e.error}`))
  }

  return { successCount, failCount, errors }
}

// 运行脚本
seedCameras()
