/**
 * 测试数据 Fixtures
 * 提供各模块的模拟数据
 */

import type { AdminInfo, LoginResponse, MachineDTO, CameraInfoDTO, ChannelInfoDTO, DashboardStatsDTO } from '@/types'

// ==================== 用户/认证数据 ====================

export const mockAdminInfo: AdminInfo = {
  id: 1,
  username: 'admin',
  nickname: '管理员',
  role: 'admin',
  lastLoginAt: '2024-01-01T00:00:00Z'
}

export const mockLoginResponse: LoginResponse = {
  token: 'mock-jwt-token-12345',
  tokenType: 'Bearer',
  expiresIn: 3600,
  refreshToken: 'mock-refresh-token-67890',
  admin: mockAdminInfo
}

export const mockUsers = [
  { ...mockAdminInfo },
  {
    id: 2,
    username: 'operator1',
    nickname: '操作员1',
    role: 'operator',
    lastLoginAt: '2024-01-02T00:00:00Z'
  },
  {
    id: 3,
    username: 'viewer1',
    nickname: '观察员1',
    role: 'viewer',
    lastLoginAt: '2024-01-03T00:00:00Z'
  }
]

// ==================== 机器数据 ====================

export const mockMachines: MachineDTO[] = [
  {
    id: 1,
    machineId: 'machine-001',
    name: '一号机',
    location: '一楼大厅',
    description: '主入口监控',
    enabled: true,
    cameraCount: 3,
    createdAt: '2024-01-01 00:00:00',
    updatedAt: '2024-01-01 00:00:00'
  },
  {
    id: 2,
    machineId: 'machine-002',
    name: '二号机',
    location: '二楼走廊',
    description: '走廊监控',
    enabled: true,
    cameraCount: 2,
    createdAt: '2024-01-02 00:00:00',
    updatedAt: '2024-01-02 00:00:00'
  },
  {
    id: 3,
    machineId: 'machine-003',
    name: '三号机',
    location: '停车场',
    description: '停车场监控',
    enabled: false,
    cameraCount: 4,
    createdAt: '2024-01-03 00:00:00',
    updatedAt: '2024-01-03 00:00:00'
  }
]

// ==================== 通道数据 ====================

export const mockChannels: ChannelInfoDTO[] = [
  {
    id: 1,
    channelId: 'ch-001',
    name: '通道1-主视角',
    rtspUrl: 'rtsp://192.168.1.100:554/stream1',
    defaultView: true,
    status: 'ONLINE'
  },
  {
    id: 2,
    channelId: 'ch-002',
    name: '通道2-侧视角',
    rtspUrl: 'rtsp://192.168.1.100:554/stream2',
    defaultView: false,
    status: 'ONLINE'
  },
  {
    id: 3,
    channelId: 'ch-003',
    name: '通道3-广角',
    rtspUrl: 'rtsp://192.168.1.100:554/stream3',
    defaultView: false,
    status: 'OFFLINE'
  }
]

// ==================== 摄像头数据 ====================

export const mockCameras: CameraInfoDTO[] = [
  {
    id: 1,
    cameraId: 'cam-001',
    name: '大厅摄像头1',
    ip: '192.168.1.100',
    port: 80,
    username: 'admin',
    brand: 'hikvision',
    capability: 'ptz_enabled',
    status: 'ONLINE',
    machineId: 'machine-001',
    machineName: '一号机',
    enabled: true,
    channels: [mockChannels[0], mockChannels[1]],
    createdAt: '2024-01-01 00:00:00',
    updatedAt: '2024-01-01 00:00:00'
  },
  {
    id: 2,
    cameraId: 'cam-002',
    name: '大厅摄像头2',
    ip: '192.168.1.101',
    port: 80,
    username: 'admin',
    brand: 'dahua',
    capability: 'switch_only',
    status: 'ONLINE',
    machineId: 'machine-001',
    machineName: '一号机',
    enabled: true,
    channels: [mockChannels[2]],
    createdAt: '2024-01-02 00:00:00',
    updatedAt: '2024-01-02 00:00:00'
  },
  {
    id: 3,
    cameraId: 'cam-003',
    name: '走廊摄像头',
    ip: '192.168.1.102',
    port: 80,
    username: 'admin',
    brand: 'hikvision',
    capability: 'ptz_enabled',
    status: 'OFFLINE',
    machineId: 'machine-002',
    machineName: '二号机',
    enabled: false,
    channels: [],
    createdAt: '2024-01-03 00:00:00',
    updatedAt: '2024-01-03 00:00:00'
  }
]

// ==================== 统计数据 ====================

export const mockDashboardStats: DashboardStatsDTO = {
  machineTotal: 3,
  machineEnabled: 2,
  cameraTotal: 9,
  cameraOnline: 7,
  cameraOffline: 2,
  channelTotal: 18
}

// ==================== API 响应包装 ====================

/**
 * 包装单个数据响应 (IBaseResponse<T>)
 */
export function wrapResponse<T>(data: T, success = true, errMessage?: string) {
  return {
    success,
    data,
    errCode: success ? undefined : 'ERROR',
    errMessage: success ? undefined : errMessage
  }
}

/**
 * 包装分页列表响应 (IPageResponse<T>) - 新版分页格式
 */
export function wrapPageResponse<T>(list: T[], page = 1, size = 20) {
  const total = list.length
  const totalPages = Math.ceil(total / size)
  return {
    success: true,
    data: {
      list,
      page,
      size,
      total,
      totalPages,
      hasNext: page < totalPages,
      hasPrevious: page > 1
    }
  }
}

/**
 * 包装数组列表响应 (IListResponse<T>) - 不分页
 */
export function wrapArrayResponse<T>(list: T[]) {
  return {
    success: true,
    data: list
  }
}

/**
 * @deprecated 使用 wrapPageResponse 代替
 */
export function wrapListResponse<T>(list: T[], total?: number) {
  return {
    success: true,
    data: {
      list,
      total: String(total ?? list.length)
    }
  }
}

/**
 * 包装错误响应
 */
export function wrapErrorResponse(errMessage: string, errCode = 'ERROR') {
  return {
    success: false,
    errCode,
    errMessage,
    data: undefined
  }
}
