import { requestClient } from '#/api/request'

// 登录配置接口
export interface LoginConfig {
  methods?: {
    username_enabled: boolean
    mobile_enabled: boolean
    email_enabled: boolean
    sms_enabled: boolean
    oauth_enabled: boolean
    guest_enabled: boolean
  }
  security?: {
    captcha_enabled: boolean
    captcha_type: 'image' | 'slide' | 'click'
    max_fail_attempts: number
    lock_duration: number
    password_strength_enabled: boolean
    password_min_length: number
    password_complexity: string[]
    password_expire_enabled: boolean
    password_expire_days: number
    single_sign_on_enabled: boolean
  }
  oauth?: {
    wechat_enabled: boolean
    wechat_app_id: string
    wechat_secret: string
    wechat_redirect_uri: string
    qq_enabled: boolean
    qq_app_id: string
    qq_secret: string
    qq_redirect_uri: string
    github_enabled: boolean
    github_client_id: string
    github_secret: string
    github_redirect_uri: string
  }
  register?: {
    register_enabled: boolean
    register_methods: string[]
    register_verification: 'none' | 'email' | 'sms' | 'manual'
    register_captcha_enabled: boolean
    agreement_required: boolean
    agreement_content: string
    default_role: string
    reward_enabled: boolean
    reward_points: number
    reward_balance: number
  }
}

// 登录统计接口
export interface LoginStats {
  total_logins: number
  today_logins: number
  failed_logins: number
  locked_accounts: number
  oauth_logins: number
  guest_logins: number
}

// 登录记录接口
export interface LoginRecord {
  id: number
  user_id: number
  username: string
  login_type: 'username' | 'mobile' | 'email' | 'sms' | 'oauth' | 'guest'
  login_ip: string
  login_location: string
  user_agent: string
  login_time: string
  logout_time?: string
  status: 'success' | 'failed' | 'locked'
  fail_reason?: string
}

// 在线用户接口
export interface OnlineUser {
  id: number
  user_id: number
  username: string
  nickname: string
  avatar: string
  login_ip: string
  login_location: string
  login_time: string
  last_activity: string
  device_type: string
  browser: string
  os: string
}

// 获取登录配置
export function getLoginConfigApi() {
  return requestClient.get<LoginConfig>('/api/common/login/config')
}

// 更新登录配置
export function updateLoginConfigApi(data: {
  type: 'methods' | 'security' | 'oauth' | 'register'
  config: any
}) {
  return requestClient.put('/api/common/login/config', data)
}

// 重置登录配置
export function resetLoginConfigApi(type: 'methods' | 'security' | 'oauth' | 'register') {
  return requestClient.post('/api/common/login/config/reset', { type })
}

// 测试登录配置
export function testLoginConfigApi(data: {
  type: 'captcha' | 'oauth' | 'sms'
  config: any
}) {
  return requestClient.post('/api/common/login/config/test', data)
}

// 获取登录统计
export function getLoginStatsApi() {
  return requestClient.get<LoginStats>('/api/common/login/stats')
}

// 获取登录记录
export function getLoginRecordsApi(params?: {
  page?: number
  limit?: number
  user_id?: number
  login_type?: string
  status?: string
  start_time?: string
  end_time?: string
}) {
  return requestClient.get<{
    list: LoginRecord[]
    total: number
    page: number
    limit: number
  }>('/api/common/login/records', { params })
}

// 获取在线用户
export function getOnlineUsersApi(params?: {
  page?: number
  limit?: number
  username?: string
  device_type?: string
}) {
  return requestClient.get<{
    list: OnlineUser[]
    total: number
    page: number
    limit: number
  }>('/api/common/login/online', { params })
}

// 强制下线用户
export function forceLogoutApi(userId: number) {
  return requestClient.post('/api/common/login/force-logout', { user_id: userId })
}

// 批量强制下线用户
export function batchForceLogoutApi(userIds: number[]) {
  return requestClient.post('/api/common/login/batch-force-logout', { user_ids: userIds })
}

// 解锁账户
export function unlockAccountApi(userId: number) {
  return requestClient.post('/api/common/login/unlock-account', { user_id: userId })
}

// 批量解锁账户
export function batchUnlockAccountApi(userIds: number[]) {
  return requestClient.post('/api/common/login/batch-unlock-account', { user_ids: userIds })
}

// 清理登录记录
export function cleanLoginRecordsApi(data: {
  days?: number
  status?: string
}) {
  return requestClient.post('/api/common/login/clean-records', data)
}

// 导出登录记录
export function exportLoginRecordsApi(params?: {
  user_id?: number
  login_type?: string
  status?: string
  start_time?: string
  end_time?: string
}) {
  return requestClient.get('/api/common/login/export-records', { 
    params,
    responseType: 'blob'
  })
}

// 验证登录配置
export function validateLoginConfigApi(data: {
  type: 'methods' | 'security' | 'oauth' | 'register'
  config: any
}) {
  return requestClient.post('/api/common/login/config/validate', data)
}

// 获取登录配置模板
export function getLoginConfigTemplateApi(type: 'methods' | 'security' | 'oauth' | 'register') {
  return requestClient.get(`/api/common/login/config/template/${type}`)
}