import { mockDate, mockId } from '../index'
import type { OperationLog } from '@/api/system'

const methods = ['GET', 'POST', 'PUT', 'DELETE']
const clients = [
  { id: 'gateway', name: 'API 网关' },
  { id: 'plant', name: 'Plant 服务' },
  { id: 'radio', name: 'Radio 服务' },
]
const operators = [
  { id: 'u1', name: '超级管理员' },
  { id: 'u2', name: '张三' },
  { id: 'u3', name: '李四' },
]
const apis = [
  { method: 'POST', path: '/api/auth/login', title: '用户登录', status: 200 },
  { method: 'GET', path: '/api/auth/captcha', title: '获取验证码', status: 200 },
  { method: 'POST', path: '/api/user/getUserList', title: '查询用户列表', status: 200 },
  { method: 'POST', path: '/api/user/save', title: '创建用户', status: 200 },
  { method: 'POST', path: '/api/user/update', title: '更新用户', status: 200 },
  { method: 'POST', path: '/api/user/delete', title: '删除用户', status: 200 },
  { method: 'POST', path: '/api/user/grantRole', title: '分配角色', status: 200 },
  { method: 'POST', path: '/api/role/getRoleList', title: '查询角色列表', status: 200 },
  { method: 'POST', path: '/api/role/grantMenu', title: '角色授权菜单', status: 200 },
  { method: 'POST', path: '/api/menu/getAllMenuTree', title: '查询菜单树', status: 200 },
  { method: 'POST', path: '/api/oss/upload', title: '上传文件', status: 200 },
  { method: 'POST', path: '/api/plant/wiki/list', title: '百科列表', status: 200 },
  { method: 'POST', path: '/api/plant/wiki/save', title: '新增百科', status: 200 },
  { method: 'POST', path: '/api/plant/banner/list', title: '轮播图列表', status: 200 },
  { method: 'POST', path: '/api/radio/channel/list', title: '频道列表', status: 200 },
  { method: 'POST', path: '/api/radio/program/save', title: '新增节目', status: 200 },
  { method: 'GET', path: '/api/radio/subscription/list', title: '订阅列表', status: 200 },
  { method: 'POST', path: '/api/user/changePassword', title: '修改密码', status: 200 },
  { method: 'GET', path: '/api/client/getClientList', title: '客户端列表', status: 200 },
  { method: 'POST', path: '/api/auth/logout', title: '用户登出', status: 200 },
  { method: 'POST', path: '/api/user/save', title: '创建用户', status: 400 },
  { method: 'POST', path: '/api/oss/upload', title: '上传文件', status: 500 },
]

const ips = ['192.168.1.100', '10.0.0.15', '172.16.0.42', '192.168.2.88']
const uas = [
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
  'PostmanRuntime/7.37.3',
]

function rand<T>(arr: T[]): T { return arr[Math.floor(Math.random() * arr.length)] }

export const mockLogs: OperationLog[] = Array.from({ length: 120 }, (_, i) => {
  const api = rand(apis)
  const client = rand(clients)
  const op = rand(operators)
  return {
    id: mockId(),
    operatorId: op.id,
    operatorName: op.name,
    clientId: client.id,
    clientName: client.name,
    method: api.method,
    path: api.path,
    title: api.title,
    statusCode: api.status,
    duration: Math.floor(Math.random() * 500) + 5,
    ip: rand(ips),
    userAgent: rand(uas),
    requestBody: api.method === 'GET' ? undefined : '{"page":1}',
    responseBody: api.status === 200 ? '{"code":0,"msg":"ok"}' : '{"code":1,"msg":"error"}',
    createdAt: mockDate(i * 0.3),
  }
}).sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
