/**
 * 登录模块 Mock 数据
 */

import type { LoginData, UserInfo } from '@/types/api'
import { UserRole } from '@/types/api'

/**
 * Mock 用户账号
 */
export interface MockAccount {
  key: string
  label: string
  username: string
  password: string
  role: UserRole
  userInfo: UserInfo
}

/**
 * Mock 账号列表
 */
export const MOCK_ACCOUNTS: MockAccount[] = [
  {
    key: 'super',
    label: '超级管理员',
    username: 'super',
    password: '123456',
    role: UserRole.SUPER_ADMIN,
    userInfo: {
      id: 1,
      username: 'Super',
      phone: '13800138000',
      user_type: 1,
      user_type_name: '超级管理员'
    }
  },
  {
    key: 'admin',
    label: '平台管理员',
    username: 'admin',
    password: '123456',
    role: UserRole.ADMIN,
    userInfo: {
      id: 2,
      username: 'admin',
      phone: '13800138001',
      user_type: 2,
      user_type_name: '平台管理员'
    }
  },
  {
    key: 'agent',
    label: '代理商',
    username: 'agent',
    password: '123456',
    role: UserRole.AGENT,
    userInfo: {
      id: 3,
      username: 'agent',
      phone: '13800138002',
      user_type: 3,
      user_type_name: '代理商'
    }
  },
  {
    key: 'enterprise',
    label: '企业客户',
    username: 'enterprise',
    password: '123456',
    role: UserRole.ENTERPRISE,
    userInfo: {
      id: 4,
      username: 'enterprise',
      phone: '13800138003',
      user_type: 4,
      user_type_name: '企业客户'
    }
  }
]

/**
 * Mock 登录响应
 */
export const mockLogin = (username: string, password: string): LoginData | null => {
  const account = MOCK_ACCOUNTS.find(
    (acc) => acc.username === username && acc.password === password
  )

  if (!account) {
    return null
  }

  // 生成 Mock Token
  const mockToken = `mock_token_${account.key}_${Date.now()}`
  const mockRefreshToken = `mock_refresh_${account.key}_${Date.now()}`

  return {
    access_token: mockToken,
    refresh_token: mockRefreshToken,
    expires_in: 7200, // 2小时
    user: account.userInfo
  }
}

/**
 * Mock 获取用户信息
 */
export const mockGetUserInfo = (token: string): UserInfo | null => {
  // 从 token 中提取用户类型
  const match = token.match(/mock_token_(\w+)_/)
  if (!match) {
    return null
  }

  const accountKey = match[1]
  const account = MOCK_ACCOUNTS.find((acc) => acc.key === accountKey)

  return account ? account.userInfo : null
}

/**
 * 获取 Mock 账号选项（用于登录页下拉框）
 */
export const getMockAccountOptions = () => {
  return MOCK_ACCOUNTS.map((acc) => ({
    key: acc.key,
    label: acc.label,
    username: acc.username,
    password: acc.password
  }))
}
