/**
 * 等级与奖章 API
 */

import request from '@/api/request'

// 类型定义
export interface LevelInfo {
  user_id: number
  level: number
  exp: number
  total_exp: number
  title: string
  color: string
  login_streak: number
  max_login_streak: number
  last_checkin_at: string | null
  next_level_exp: number
  exp_to_next_level: number
  is_max_level: boolean
}

export interface ExpHistoryItem {
  id: number
  exp_change: number
  exp_type: string
  description: string
  level_before: number | null
  level_after: number | null
  created_at: string
}

export interface LeaderboardItem {
  rank: number
  user_id: number
  username: string
  full_name: string | null
  avatar_url: string | null
  level: number
  title: string
  color: string
  total_exp: number
  login_streak: number
}

export interface Badge {
  id: number
  code: string
  name: string
  description: string
  icon: string
  category: string
  condition_type?: string
  condition_value?: number
  exp_reward: number
  unlocked?: boolean
  unlocked_at?: string | null
}

export interface CheckinResult {
  success: boolean
  message: string
  exp_gained: number
  base_exp?: number
  bonus_exp?: number
  login_streak: number
  leveled_up?: boolean
  new_level?: number | null
  already_checked_in?: boolean
  new_badges?: Badge[]
}

// API 函数

/**
 * 获取当前用户等级信息
 */
export function getMyLevel() {
  return request.get<LevelInfo>('/level/me')
}

/**
 * 获取指定用户等级信息
 */
export function getUserLevel(userId: number) {
  return request.get<LevelInfo>(`/level/user/${userId}`)
}

/**
 * 每日签到
 */
export function dailyCheckin() {
  return request.post<CheckinResult>('/level/checkin')
}

/**
 * 获取经验值历史
 */
export function getExpHistory(params?: {
  limit?: number
  offset?: number
  exp_type?: string
}) {
  return request.get<{
    items: ExpHistoryItem[]
    total: number
    limit: number
    offset: number
  }>('/level/exp-history', { params })
}

/**
 * 获取等级排行榜
 */
export function getLeaderboard(params?: {
  limit?: number
  offset?: number
}) {
  return request.get<{
    items: LeaderboardItem[]
    total: number
    limit: number
    offset: number
    my_rank: number
    my_level_info: LevelInfo
  }>('/level/leaderboard', { params })
}

/**
 * 获取所有奖章定义
 */
export function getAllBadges() {
  return request.get<Badge[]>('/level/badges/all')
}

/**
 * 获取用户奖章（含解锁状态）
 */
export function getMyBadges() {
  return request.get<{
    badges: Badge[]
    total: number
    unlocked_count: number
  }>('/level/badges/me')
}

/**
 * 获取未通知的新奖章
 */
export function getUnnotifiedBadges() {
  return request.get<Badge[]>('/level/badges/unnotified')
}

/**
 * 标记奖章为已通知
 */
export function markBadgesNotified(badgeIds?: number[]) {
  return request.post('/level/badges/mark-notified', badgeIds)
}

/**
 * 手动检查并授予奖章
 */
export function checkAndAwardBadges() {
  return request.post<{
    new_badges: Badge[]
    count: number
  }>('/level/check-badges')
}

export default {
  getMyLevel,
  getUserLevel,
  dailyCheckin,
  getExpHistory,
  getLeaderboard,
  getAllBadges,
  getMyBadges,
  getUnnotifiedBadges,
  markBadgesNotified,
  checkAndAwardBadges
}
