// [WHY] 天天基金 API 增强版 - 直接调用 Eastmoney API
// [WHAT] 提供基金排行、详情、阶段涨幅、大数据榜单等高级功能
// [DEPS] 使用 JSONP 和 fetch 直接请求天天基金接口

import { cache, CACHE_TTL } from './cache'

// ========== 交易时间和持久化缓存工具 ==========

/**
 * 交易时段枚举
 */
export type TradingSession = 'morning' | 'noon_break' | 'afternoon' | 'closed'

/**
 * 获取当前交易时段
 * [WHY] 区分上午盘、午休、下午盘、休市，用于精确控制数据刷新策略
 * [WHAT] 上午盘 9:30-11:30，午休 11:30-13:00，下午盘 13:00-15:00
 */
export function getTradingSession(): TradingSession {
  const now = new Date()
  const hour = now.getHours()
  const minute = now.getMinutes()
  const day = now.getDay()
  const timeMinutes = hour * 60 + minute // 当前时间转换为分钟数
  
  // [WHAT] 周末休市
  if (day === 0 || day === 6) return 'closed'
  
  // [WHAT] 上午盘：9:30 - 11:30 (570 - 690分钟)
  if (timeMinutes >= 570 && timeMinutes < 690) return 'morning'
  
  // [WHAT] 午休：11:30 - 13:00 (690 - 780分钟)
  if (timeMinutes >= 690 && timeMinutes < 780) return 'noon_break'
  
  // [WHAT] 下午盘：13:00 - 15:00 (780 - 900分钟)
  if (timeMinutes >= 780 && timeMinutes < 900) return 'afternoon'
  
  // [WHAT] 其他时间休市
  return 'closed'
}

/**
 * 判断当前是否在交易时间内
 * [WHY] 开盘前使用缓存数据，开盘后获取实时数据
 * [WHAT] 上午盘 9:30-11:30，下午盘 13:00-15:00
 */
export function isTradingTime(): boolean {
  const session = getTradingSession()
  return session === 'morning' || session === 'afternoon'
}

/**
 * 判断当前是否为交易日的交易时段（包括午休）
 * [WHY] 用于判断今天是否已开盘，即使在午休时间也算"今天已开盘"
 */
export function isTradingDay(): boolean {
  const session = getTradingSession()
  return session !== 'closed'
}

/**
 * 判断今天是否已经开过盘（用于判断数据是否需要刷新）
 * [WHY] 9:30后即使午休也认为今天已开盘，应该用今天的数据
 */
export function hasMarketOpenedToday(): boolean {
  const now = new Date()
  const hour = now.getHours()
  const minute = now.getMinutes()
  const day = now.getDay()
  const timeMinutes = hour * 60 + minute
  
  // [WHAT] 周末不算开盘
  if (day === 0 || day === 6) return false
  
  // [WHAT] 9:30 后算开盘（包括收盘后到24点）
  return timeMinutes >= 570
}

/**
 * 持久化缓存工具
 * [WHY] 将数据保存到 localStorage，开盘前可以使用昨天的数据
 */
export const persistCache = {
  get<T>(key: string): T | null {
    try {
      const data = localStorage.getItem(`fund_${key}`)
      if (data) return JSON.parse(data)
    } catch {}
    return null
  },
  
  set<T>(key: string, data: T): void {
    try {
      localStorage.setItem(`fund_${key}`, JSON.stringify(data))
    } catch {}
  }
}

/**
 * [WHAT] 初始化移动端默认缓存数据
 * [WHY] 移动端首次运行时没有缓存，JSONP 可能受限，提供默认数据
 * [NOTE] 只在缓存为空时设置，不会覆盖已有数据
 */
export function initMobileDefaultCache(): void {
  const cacheKey = 'market_overview_v2'
  const existing = persistCache.get<{totalUp: number}>(cacheKey)
  
  // [WHAT] 已有缓存，不覆盖
  if (existing && existing.totalUp > 0) return
  
  // [WHAT] 设置默认数据（基于历史平均值估算）
  const defaultData = {
    updateTime: '等待更新',
    totalUp: 3000,
    totalDown: 5000,
    distribution: [
      { range: '≤-5', count: 150, min: -Infinity, max: -5 },
      { range: '-5~-3', count: 200, min: -5, max: -3 },
      { range: '-3~-1', count: 1500, min: -3, max: -1 },
      { range: '-1~0', count: 3000, min: -1, max: -0.001 },
      { range: '0~1', count: 4000, min: -0.001, max: 1 },
      { range: '1~3', count: 1000, min: 1, max: 3 },
      { range: '3~5', count: 100, min: 3, max: 5 },
      { range: '≥5', count: 50, min: 5, max: Infinity }
    ]
  }
  
  persistCache.set(cacheKey, defaultData)
  console.log('[Cache] 初始化移动端默认市场数据')
}

/**
 * 带持久化缓存的数据获取包装器
 * [WHY] 统一处理：开盘前用缓存，开盘后获取新数据
 */
export async function fetchWithPersistCache<T>(
  key: string,
  fetcher: () => Promise<T>,
  validator: (data: T) => boolean = () => true
): Promise<T | null> {
  // [WHAT] 检查内存缓存
  const memCached = cache.get<T>(key)
  if (memCached) return memCached
  
  // [WHAT] 获取持久化缓存
  const persistCached = persistCache.get<T>(key)
  
  // [WHAT] 非交易时间直接返回持久化缓存
  if (!isTradingTime() && persistCached && validator(persistCached)) {
    cache.set(key, persistCached, CACHE_TTL.MARKET_INDEX)
    return persistCached
  }
  
  // [WHAT] 交易时间尝试获取新数据
  try {
    const data = await fetcher()
    if (data && validator(data)) {
      cache.set(key, data, CACHE_TTL.MARKET_INDEX)
      persistCache.set(key, data)
      return data
    }
    // [EDGE] 新数据无效，使用缓存
    return persistCached
  } catch {
    // [EDGE] 请求失败，使用缓存
    return persistCached
  }
}

// ========== 类型定义 ==========

// [WHAT] 基金排行项（增强版）
export interface FundRankItemExt {
  code: string
  name: string
  type: string
  netValue: number
  dayReturn: number
  weekReturn: number
  monthReturn: number
  threeMonthReturn: number
  sixMonthReturn: number
  yearReturn: number
  twoYearReturn: number
  threeYearReturn: number
  totalReturn: number
  scale: number         // 规模(亿)
  manager: string
  buyStatus: string     // 申购状态
}

// [WHAT] 阶段涨幅
export interface PeriodReturnExt {
  period: string
  label: string
  fundReturn: number
  avgReturn: number
  hs300Return: number
  rank: number
  totalCount: number
}

// [WHAT] 热门主题
export interface HotTheme {
  code: string
  name: string
  dayReturn: number
  weekReturn: number
  monthReturn: number
  fundCount: number
}

// [WHAT] 基金评级
export interface FundRating {
  date: string
  shanghai: number      // 上海证券评级
  zhaoshang: number     // 招商证券评级
  jian: number          // 济安金信评级
}

// ========== 基金排行（增强版） ==========

/**
 * 获取基金排行榜（增强版）
 * [WHY] 使用 Eastmoney API 获取丰富的排行数据
 * [HOW] 通过 JSONP 调用 fundeast API
 */
export async function fetchFundRankExt(options: {
  fundType?: string     // gp股票 hh混合 zq债券 zs指数 qdii fof hb货币
  sortBy?: string       // rzdf日 SYL_Z周 SYL_Y月 SYL_3Y季 SYL_6Y半年 SYL_1N年
  sortOrder?: number    // 1降序 0升序
  page?: number
  pageSize?: number
} = {}): Promise<FundRankItemExt[]> {
  const {
    fundType = '',
    sortBy = 'SYL_1N',
    sortOrder = 1,
    page = 1,
    pageSize = 50
  } = options
  
  const cacheKey = `rank_ext_${fundType}_${sortBy}_${sortOrder}_${page}_${pageSize}`
  const cached = cache.get<FundRankItemExt[]>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    // [WHAT] 构建 JSONP 回调名
    const callbackName = `fundRank_${Date.now()}`
    const scriptId = `rank_script_${Date.now()}`
    
    // [WHAT] 设置全局回调
    ;(window as any)[callbackName] = (data: any) => {
      cleanup()
      
      if (!data?.datas) {
        resolve([])
        return
      }
      
      // [WHAT] 解析数据
      // 数据格式: "代码,名称,简称,日期,单位净值,累计净值,日涨幅,周涨幅,月涨幅,3月涨幅,6月涨幅,年涨幅,2年涨幅,3年涨幅,今年涨幅,成立涨幅,手续费,是否可购,基金经理,..."
      const result: FundRankItemExt[] = data.datas.map((row: string) => {
        const cols = row.split(',')
        return {
          code: cols[0] ?? '',
          name: cols[1] ?? '',
          type: fundType || 'mixed',
          netValue: parseFloat(cols[4] ?? '0') || 0,
          dayReturn: parseFloat(cols[6] ?? '0') || 0,
          weekReturn: parseFloat(cols[7] ?? '0') || 0,
          monthReturn: parseFloat(cols[8] ?? '0') || 0,
          threeMonthReturn: parseFloat(cols[9] ?? '0') || 0,
          sixMonthReturn: parseFloat(cols[10] ?? '0') || 0,
          yearReturn: parseFloat(cols[11] ?? '0') || 0,
          twoYearReturn: parseFloat(cols[12] ?? '0') || 0,
          threeYearReturn: parseFloat(cols[13] ?? '0') || 0,
          totalReturn: parseFloat(cols[15] ?? '0') || 0,
          scale: 0,
          manager: cols[18] ?? '',
          buyStatus: cols[17] === '1' ? '可购' : '暂停'
        }
      })
      
      cache.set(cacheKey, result, CACHE_TTL.FUND_LIST)
      resolve(result)
    }
    
    function cleanup() {
      delete (window as any)[callbackName]
      const script = document.getElementById(scriptId)
      if (script) document.body.removeChild(script)
    }
    
    // [WHAT] 构建 URL
    // API: http://fund.eastmoney.com/data/rankhandler.aspx
    const ft = fundType ? `&ft=${fundType}` : ''
    const url = `https://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&rs=&gs=0&sc=${sortBy}&st=${sortOrder}&pi=${page}&pn=${pageSize}${ft}&dx=1&v=${Date.now()}`
    
    const script = document.createElement('script')
    script.id = scriptId
    script.src = url
    script.onerror = () => {
      cleanup()
      resolve([])
    }
    
    // [EDGE] 超时处理
    setTimeout(() => {
      if ((window as any)[callbackName]) {
        cleanup()
        resolve([])
      }
    }, 10000)
    
    document.body.appendChild(script)
  })
}

// ========== 阶段涨幅（详细版） ==========

/**
 * 获取基金阶段涨幅（带排名）
 * [WHY] 从 pingzhongdata 获取详细的阶段数据
 */
export async function fetchPeriodReturnExt(code: string): Promise<PeriodReturnExt[]> {
  const cacheKey = `period_ext_${code}`
  const cached = cache.get<PeriodReturnExt[]>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    const scriptId = `period_${code}_${Date.now()}`
    
    const script = document.createElement('script')
    script.id = scriptId
    script.src = `https://fund.eastmoney.com/pingzhongdata/${code}.js?v=${Date.now()}`
    
    script.onload = () => {
      cleanup()
      
      try {
        // [WHAT] 从全局变量获取数据
        const periodData = (window as any).Data_rateInSimilarPers498 || []
        
        // [WHAT] API原始key -> 标准化period -> 显示标签
        const periodConfig: Record<string, { period: string, label: string }> = {
          'Z': { period: '1w', label: '近1周' },
          'Y': { period: '1m', label: '近1月' },
          '3Y': { period: '3m', label: '近3月' },
          '6Y': { period: '6m', label: '近6月' },
          '1N': { period: '1y', label: '近1年' },
          '2N': { period: '2y', label: '近2年' },
          '3N': { period: '3y', label: '近3年' },
          '5N': { period: '5y', label: '近5年' },
          'JN': { period: 'ytd', label: '今年来' },
          'LN': { period: 'all', label: '成立来' }
        }
        
        const result: PeriodReturnExt[] = []
        
        // [WHAT] 解析阶段涨幅数据，转换为标准化格式
        if (Array.isArray(periodData)) {
          periodData.forEach((item: any) => {
            const config = periodConfig[item.title]
            if (config) {
              result.push({
                period: config.period,
                label: config.label,
                fundReturn: parseFloat(item.syl) || 0,
                avgReturn: parseFloat(item.avg) || 0,
                hs300Return: parseFloat(item.hs300) || 0,
                rank: parseInt(item.rank) || 0,
                totalCount: parseInt(item.sc) || 0
              })
            }
          })
        }
        
        cache.set(cacheKey, result, CACHE_TTL.NET_VALUE)
        resolve(result)
      } catch {
        resolve([])
      }
    }
    
    script.onerror = () => {
      cleanup()
      resolve([])
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    document.body.appendChild(script)
  })
}

// ========== 热门主题/板块 ==========

/**
 * 获取热门主题板块
 * [WHY] 展示行业板块涨跌情况
 */
export async function fetchHotThemes(): Promise<HotTheme[]> {
  const cacheKey = 'hot_themes'
  const cached = cache.get<HotTheme[]>(cacheKey)
  if (cached) return cached
  
  try {
    // [WHAT] 使用 Eastmoney 板块接口
    const url = `https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=20&po=1&np=1&fltt=2&invt=2&fid=f3&fs=m:90+t:2&fields=f2,f3,f4,f12,f14&_=${Date.now()}`
    
    const response = await fetch(url)
    const data = await response.json()
    
    if (!data?.data?.diff) return []
    
    const result: HotTheme[] = data.data.diff.map((item: any) => ({
      code: item.f12 || '',
      name: item.f14 || '',
      dayReturn: item.f3 || 0,
      weekReturn: 0,
      monthReturn: 0,
      fundCount: 0
    }))
    
    cache.set(cacheKey, result, CACHE_TTL.MARKET_INDEX)
    return result
  } catch {
    return []
  }
}

// ========== 基金评级 ==========

/**
 * 获取基金评级信息
 * [WHY] 展示各机构对基金的评级
 */
export async function fetchFundRating(code: string): Promise<FundRating | null> {
  const cacheKey = `rating_${code}`
  const cached = cache.get<FundRating>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    const scriptId = `rating_${code}_${Date.now()}`
    
    const script = document.createElement('script')
    script.id = scriptId
    script.src = `https://fund.eastmoney.com/pingzhongdata/${code}.js?v=${Date.now()}`
    
    script.onload = () => {
      cleanup()
      
      try {
        // [WHAT] 从全局变量获取评级数据
        // Data_performanceEvaluation 包含基金能力评估
        const evalData = (window as any).Data_performanceEvaluation
        
        if (!evalData) {
          resolve(null)
          return
        }
        
        // [EDGE] 简化为综合评分
        const result: FundRating = {
          date: new Date().toISOString().split('T')[0] ?? '',
          shanghai: 0,
          zhaoshang: 0,
          jian: Math.round(parseFloat(evalData.avr) / 20) || 0 // 转换为1-5星
        }
        
        cache.set(cacheKey, result, CACHE_TTL.FUND_INFO)
        resolve(result)
      } catch {
        resolve(null)
      }
    }
    
    script.onerror = () => {
      cleanup()
      resolve(null)
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    document.body.appendChild(script)
  })
}

// ========== 基金持仓变动 ==========

export interface HoldingChange {
  stockCode: string
  stockName: string
  ratio: number         // 持仓比例%
  change: number        // 较上期变动%
  marketValue: number   // 持仓市值(万)
}

/**
 * 获取基金持仓变动
 * [WHY] 展示重仓股变动情况
 */
export async function fetchHoldingChanges(code: string): Promise<HoldingChange[]> {
  const cacheKey = `holding_change_${code}`
  const cached = cache.get<HoldingChange[]>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    const scriptId = `holding_${code}_${Date.now()}`
    
    const script = document.createElement('script')
    script.id = scriptId
    script.src = `https://fund.eastmoney.com/pingzhongdata/${code}.js?v=${Date.now()}`
    
    script.onload = () => {
      cleanup()
      
      try {
        // [WHAT] 从 Data_fundSharesPositions 获取持仓数据
        const stockPositions = (window as any).Data_investPosition?.fundStocks || []
        
        const result: HoldingChange[] = stockPositions.slice(0, 10).map((item: any) => ({
          stockCode: item.GPDM || '',
          stockName: item.GPJC || '',
          ratio: parseFloat(item.JZBL) || 0,
          change: parseFloat(item.PCTNVCHG) || 0,
          marketValue: parseFloat(item.GPJC) || 0
        }))
        
        cache.set(cacheKey, result, CACHE_TTL.FUND_INFO)
        resolve(result)
      } catch {
        resolve([])
      }
    }
    
    script.onerror = () => {
      cleanup()
      resolve([])
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    document.body.appendChild(script)
  })
}

// ========== 同类基金对比 ==========

export interface SimilarFund {
  code: string
  name: string
  yearReturn: number
  threeYearReturn: number
  scale: number
  manager: string
}

/**
 * 获取同类基金（用于对比）
 * [WHY] 帮助用户了解同类基金表现
 */
export async function fetchSimilarFunds(code: string): Promise<SimilarFund[]> {
  const cacheKey = `similar_${code}`
  const cached = cache.get<SimilarFund[]>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    const scriptId = `similar_${code}_${Date.now()}`
    
    const script = document.createElement('script')
    script.id = scriptId
    script.src = `https://fund.eastmoney.com/pingzhongdata/${code}.js?v=${Date.now()}`
    
    script.onload = () => {
      cleanup()
      
      try {
        // [WHAT] 从 swithSameType 获取同类基金
        const sameType = (window as any).swithSameType || []
        
        const result: SimilarFund[] = []
        
        // [WHAT] sameType 是二维数组，每个子数组是一个周期的同类排行
        // 取年度排行
        if (sameType[3]) {
          sameType[3].slice(0, 5).forEach((item: string) => {
            const parts = item.split('_')
            if (parts.length >= 3) {
              result.push({
                code: parts[0] ?? '',
                name: parts[1] ?? '',
                yearReturn: parseFloat(parts[2] ?? '0') || 0,
                threeYearReturn: 0,
                scale: 0,
                manager: ''
              })
            }
          })
        }
        
        cache.set(cacheKey, result, CACHE_TTL.FUND_INFO)
        resolve(result)
      } catch {
        resolve([])
      }
    }
    
    script.onerror = () => {
      cleanup()
      resolve([])
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    document.body.appendChild(script)
  })
}

// ========== 基金经理排行榜 ==========

export interface ManagerRankItem {
  managerId: string
  name: string
  company: string
  workTime: string        // 从业年限
  fundCount: number       // 管理基金数
  scale: string           // 管理规模
  bestReturn: number      // 最佳回报
  avgReturn: number       // 平均年化
}

/**
 * 获取基金经理排行榜
 * [WHY] 帮助用户发现优秀的基金经理
 * [HOW] 从基金排行数据中提取经理信息并去重汇总
 */
export async function fetchManagerRank(options: {
  sortBy?: string         // penavgrowth平均回报 workyear从业年限
  sortOrder?: string      // desc/asc
  page?: number
  pageSize?: number
} = {}): Promise<ManagerRankItem[]> {
  const {
    sortBy = 'penavgrowth',
    pageSize = 30
  } = options
  
  const cacheKey = `manager_rank_${sortBy}_${pageSize}`
  const cached = cache.get<ManagerRankItem[]>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    const scriptId = `manager_rank_${Date.now()}`
    
    // [WHAT] 使用基金排行数据提取经理信息
    const url = `https://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=1nzf&st=desc&pi=1&pn=500&dx=1&v=${Date.now()}`
    
    const script = document.createElement('script')
    script.id = scriptId
    
    // [WHAT] 等待 rankData 变量
    script.onload = () => {
      cleanup()
      
      try {
        const rankData = (window as any).rankData
        if (!rankData?.datas) {
          resolve([])
          return
        }
        
        // [WHAT] 从基金数据中提取经理信息
        // 格式: "代码,名称,...,经理,..."
        const managerMap = new Map<string, {
          name: string
          company: string
          funds: { name: string, return: number }[]
          totalReturn: number
        }>()
        
        // [WHAT] 天天基金数据格式（示例）：
        // 0=代码, 1=名称, 2=简称, 3=日期, 4=单位净值, 5=累计净值
        // 6=日涨幅, 7=周涨幅, 8=月涨幅, 9=3月涨幅, 10=6月涨幅, 11=年涨幅
        // 12=2年涨幅, 13=3年涨幅, 14=今年涨幅, 15=成立涨幅
        // 16=手续费, 17=申购状态, 18=基金经理, 19=...
        
        rankData.datas.forEach((row: string) => {
          const cols = row.split(',')
          
          // [WHAT] 尝试多个可能的经理名称位置
          let managerName = ''
          // 通常在第18或第24位置
          for (const idx of [18, 24, 19, 23]) {
            const val = cols[idx]
            if (val && val !== '--' && val.length > 0 && val.length < 20 && !/^\d+(\.\d+)?%?$/.test(val)) {
              managerName = val
              break
            }
          }
          
          const fundName = cols[1] ?? ''
          const yearReturn = parseFloat(cols[11] ?? '0') || 0
          
          if (managerName) {
            // [WHAT] 处理多经理情况（用空格或顿号分隔）
            const managers = managerName.split(/[\s、]+/)
            managers.forEach(mgr => {
              const name = mgr.trim()
              if (!name || name.length > 10) return // 过滤无效名称
              
              if (managerMap.has(name)) {
                const data = managerMap.get(name)!
                data.funds.push({ name: fundName, return: yearReturn })
                data.totalReturn += yearReturn
              } else {
                managerMap.set(name, {
                  name,
                  company: '--',
                  funds: [{ name: fundName, return: yearReturn }],
                  totalReturn: yearReturn
                })
              }
            })
          }
        })
        
        // [WHAT] 转换为结果数组
        let result: ManagerRankItem[] = Array.from(managerMap.values()).map(m => ({
          managerId: m.name,
          name: m.name,
          company: m.company,
          workTime: '--',  // 基金排行数据不包含从业年限
          fundCount: m.funds.length,
          scale: '--',
          bestReturn: Math.max(...m.funds.map(f => f.return)),
          avgReturn: m.totalReturn / m.funds.length
        }))
        
        // [WHAT] 排序
        if (sortBy === 'workyear') {
          // 按基金数量排序（替代从业年限）
          result.sort((a, b) => b.fundCount - a.fundCount)
        } else {
          // 按平均回报排序
          result.sort((a, b) => b.avgReturn - a.avgReturn)
        }
        
        // [WHAT] 取前N个
        result = result.slice(0, pageSize)
        
        cache.set(cacheKey, result, CACHE_TTL.FUND_LIST)
        resolve(result)
      } catch {
        resolve([])
      }
    }
    
    script.onerror = () => {
      cleanup()
      resolve([])
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    // [WHAT] 超时处理
    setTimeout(() => {
      const s = document.getElementById(scriptId)
      if (s) {
        cleanup()
        resolve([])
      }
    }, 15000)
    
    script.src = url
    document.body.appendChild(script)
  })
}

// ========== 基金涨跌分布 ==========

export interface FundDistribution {
  range: string        // 区间标签
  count: number        // 基金数量
  min: number          // 最小涨幅
  max: number          // 最大涨幅
}

export interface MarketOverview {
  updateTime: string
  totalUp: number      // 上涨数
  totalDown: number    // 下跌数
  distribution: FundDistribution[]
}

/**
 * 获取基金涨跌分布
 * [WHY] 展示市场整体涨跌情况
 * [HOW] 天天基金 rankhandler 会设置全局变量 rankData
 * [NOTE] 开盘前使用昨天的缓存数据，开盘后更新
 */
export async function fetchMarketOverview(): Promise<MarketOverview> {
  const cacheKey = 'market_overview_v2'
  
  // [WHAT] 检查内存缓存
  const cached = cache.get<MarketOverview>(cacheKey)
  if (cached) return cached
  
  // [WHAT] 获取持久化缓存
  const persisted = persistCache.get<MarketOverview>(cacheKey)
  
  // [WHAT] 检测是否是原生 APP 环境（Capacitor WebView）
  const isNativeApp = !!(window as any).Capacitor?.isNativePlatform?.()
  
  // [WHAT] 非交易时间直接返回持久化缓存（周末/节假日/盘前盘后）
  if (!isTradingTime()) {
    if (persisted && (persisted.totalUp > 0 || persisted.totalDown > 0)) {
      console.log('[MarketOverview] 非交易时间，使用缓存数据')
      cache.set(cacheKey, persisted, CACHE_TTL.MARKET_INDEX)
      return persisted
    }
    // [EDGE] 没有缓存，初始化默认数据
    initMobileDefaultCache()
    const defaultData = persistCache.get<MarketOverview>(cacheKey)
    if (defaultData) {
      cache.set(cacheKey, defaultData, CACHE_TTL.MARKET_INDEX)
      return defaultData
    }
  }
  
  // [WHAT] 移动端优先使用缓存（WebView JSONP 可能受限）
  // [WHY] Android WebView 可能阻止跨域脚本加载
  if (isNativeApp && persisted && persisted.totalUp > 0) {
    console.log('[MarketOverview] 移动端使用缓存数据')
    cache.set(cacheKey, persisted, CACHE_TTL.MARKET_INDEX)
    // [NOTE] 仍然尝试后台更新，但立即返回缓存
    fetchMarketOverviewInBackground(persisted)
    return persisted
  }
  
  console.log('[MarketOverview] 开始获取数据, 原生环境:', isNativeApp)
  
  // [WHAT] 固定的区间分布
  // [NOTE] 使用 -0.001 作为边界，避免 change=0 被错误分类
  const createRanges = (): FundDistribution[] => [
    { range: '≤-5', count: 0, min: -Infinity, max: -5 },
    { range: '-5~-3', count: 0, min: -5, max: -3 },
    { range: '-3~-1', count: 0, min: -3, max: -1 },
    { range: '-1~0', count: 0, min: -1, max: -0.001 },  // 不包括0
    { range: '0~1', count: 0, min: -0.001, max: 1 },    // 包括0
    { range: '1~3', count: 0, min: 1, max: 3 },
    { range: '3~5', count: 0, min: 3, max: 5 },
    { range: '≥5', count: 0, min: 5, max: Infinity }
  ]
  
  // [WHAT] 创建空数据（API 失败时返回）
  const createEmptyData = (): MarketOverview => ({
    updateTime: '--',
    totalUp: 0,
    totalDown: 0,
    distribution: createRanges()
  })
  
  const ranges = createRanges()
  
  return new Promise((resolve) => {
    const scriptId = `overview_script_${Date.now()}`
    
    // [WHAT] 清除旧的 rankData
    delete (window as any).rankData
    
    const script = document.createElement('script')
    script.id = scriptId
    
    // [WHAT] 天天基金 API 会设置 window.rankData 变量
    script.onload = () => {
      // [WHAT] 等待一小段时间让 rankData 被设置
      setTimeout(() => {
        cleanup()
        
        try {
          const rankData = (window as any).rankData
          
          let totalUp = 0
          let totalDown = 0
          
          if (rankData?.datas && Array.isArray(rankData.datas)) {
            rankData.datas.forEach((row: string) => {
              const cols = row.split(',')
              // [WHAT] 天天基金 rankhandler 数据格式：
              // 0:基金代码, 1:基金名称, 2:字母缩写, 3:日期, 4:单位净值, 5:累计净值, 
              // 6:日涨幅, 7:近1周, 8:近1月, 9:近3月, 10:近6月, 11:近1年...
              let change = parseFloat(cols[6] ?? '0')
              
              // [EDGE] 如果第6列不是有效数字，尝试其他可能的列
              if (isNaN(change) || cols[6] === '') {
                change = parseFloat(cols[4] ?? '0') || parseFloat(cols[5] ?? '0') || 0
              }
              
              if (change > 0) totalUp++
              else if (change < 0) totalDown++
              
              // 统计分布
              for (const r of ranges) {
                if (change > r.min && change <= r.max) {
                  r.count++
                  break
                }
              }
            })
          }
          
          
          const now = new Date()
          const result: MarketOverview = {
            updateTime: `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`,
            totalUp,
            totalDown,
            distribution: ranges
          }
          
          // [WHAT] 只有获取到有效数据才保存
          if (totalUp > 0 || totalDown > 0) {
            cache.set(cacheKey, result, CACHE_TTL.MARKET_INDEX)
            persistCache.set(cacheKey, result)
            safeResolve(result)
          } else {
            // [EDGE] 数据无效，使用持久化缓存
            safeResolve(persisted || createEmptyData())
          }
        } catch {
          // [EDGE] 解析失败，使用持久化缓存
          safeResolve(persisted || createEmptyData())
        }
      }, 100)
    }
    
    script.onerror = () => {
      cleanup()
      // [EDGE] 请求失败，使用持久化缓存
      safeResolve(persisted || createEmptyData())
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    // [WHAT] 超时处理 - 无论如何都要 resolve
    let resolved = false
    const timeoutId = setTimeout(() => {
      if (!resolved) {
        resolved = true
        cleanup()
        // [EDGE] 超时时使用持久化缓存
        resolve(persisted || createEmptyData())
      }
    }, 8000)
    
    // [WHAT] 包装 resolve 确保只调用一次
    const safeResolve = (data: MarketOverview) => {
      if (!resolved) {
        resolved = true
        clearTimeout(timeoutId)
        resolve(data)
      }
    }
    
    // [WHAT] 获取基金涨跌数据（场外开放式基金）
    // [NOTE] 使用 jsonpgz 回调，确保数据完整返回
    script.src = `https://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=zzf&st=desc&sd=2020-01-01&ed=${new Date().toISOString().slice(0,10)}&qdii=&tabSubtype=,,,,,&pi=1&pn=10000&dx=1&v=${Date.now()}`
    document.body.appendChild(script)
  })
}

/**
 * [WHAT] 后台静默更新市场概览数据
 * [WHY] 移动端先返回缓存，后台尝试更新
 */
function fetchMarketOverviewInBackground(currentData: MarketOverview): void {
  const cacheKey = 'market_overview_v2'
  
  const scriptId = `bg_overview_${Date.now()}`
  delete (window as any).rankData
  
  const script = document.createElement('script')
  script.id = scriptId
  
  const cleanup = () => {
    const s = document.getElementById(scriptId)
    if (s) document.body.removeChild(s)
  }
  
  // [WHAT] 超时自动清理
  const timeoutId = setTimeout(() => {
    cleanup()
  }, 10000)
  
  script.onload = () => {
    setTimeout(() => {
      cleanup()
      clearTimeout(timeoutId)
      
      try {
        const rankData = (window as any).rankData
        if (!rankData?.datas || !Array.isArray(rankData.datas)) return
        
        let totalUp = 0
        let totalDown = 0
        const ranges: FundDistribution[] = [
          { range: '≤-5', count: 0, min: -Infinity, max: -5 },
          { range: '-5~-3', count: 0, min: -5, max: -3 },
          { range: '-3~-1', count: 0, min: -3, max: -1 },
          { range: '-1~0', count: 0, min: -1, max: -0.001 },
          { range: '0~1', count: 0, min: -0.001, max: 1 },
          { range: '1~3', count: 0, min: 1, max: 3 },
          { range: '3~5', count: 0, min: 3, max: 5 },
          { range: '≥5', count: 0, min: 5, max: Infinity }
        ]
        
        rankData.datas.forEach((row: string) => {
          const cols = row.split(',')
          let change = parseFloat(cols[6] ?? '0')
          if (isNaN(change) || cols[6] === '') {
            change = parseFloat(cols[4] ?? '0') || 0
          }
          
          if (change > 0) totalUp++
          else if (change < 0) totalDown++
          
          for (const r of ranges) {
            if (change > r.min && change <= r.max) {
              r.count++
              break
            }
          }
        })
        
        // [WHAT] 只有获取到有效数据才更新缓存
        if (totalUp > 0 || totalDown > 0) {
          const now = new Date()
          const result: MarketOverview = {
            updateTime: `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`,
            totalUp,
            totalDown,
            distribution: ranges
          }
          cache.set(cacheKey, result, CACHE_TTL.MARKET_INDEX)
          persistCache.set(cacheKey, result)
          console.log('[MarketOverview] 后台更新成功')
        }
      } catch {
        // 静默失败
      }
    }, 100)
  }
  
  script.onerror = () => {
    cleanup()
    clearTimeout(timeoutId)
  }
  
  script.src = `https://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=zzf&st=desc&sd=2020-01-01&ed=${new Date().toISOString().slice(0,10)}&qdii=&tabSubtype=,,,,,&pi=1&pn=10000&dx=1&v=${Date.now()}`
  document.body.appendChild(script)
}

// ========== 场外基金涨幅榜 ==========

export interface OTCFundItem {
  code: string
  name: string
  netValue: number
  dayReturn: number
  updateStatus: string  // 已更新/待更新
}

/**
 * 获取场外基金涨幅榜
 * [HOW] 天天基金 rankhandler 会设置全局变量 rankData
 */
export async function fetchOTCFundRank(order: 'desc' | 'asc' = 'desc', pageSize = 10): Promise<OTCFundItem[]> {
  const cacheKey = `otc_rank_${order}_${pageSize}`
  const cached = cache.get<OTCFundItem[]>(cacheKey)
  if (cached) return cached
  
  return new Promise((resolve) => {
    const scriptId = `otc_script_${Date.now()}`
    
    // [WHAT] 清除旧的 rankData
    delete (window as any).rankData
    
    const script = document.createElement('script')
    script.id = scriptId
    
    script.onload = () => {
      setTimeout(() => {
        cleanup()
        
        try {
          const rankData = (window as any).rankData
          
          if (!rankData?.datas || !Array.isArray(rankData.datas)) {
            resolve([])
            return
          }
          
          const result: OTCFundItem[] = rankData.datas.slice(0, pageSize).map((row: string) => {
            const cols = row.split(',')
            return {
              code: cols[0] ?? '',
              name: cols[1] ?? '',
              netValue: parseFloat(cols[4] ?? '0') || 0,
              dayReturn: parseFloat(cols[6] ?? '0') || 0,
              updateStatus: '已更新'
            }
          })
          
          cache.set(cacheKey, result, 60000)
          resolve(result)
        } catch {
          resolve([])
        }
      }, 100)
    }
    
    script.onerror = () => {
      cleanup()
      resolve([])
    }
    
    function cleanup() {
      const s = document.getElementById(scriptId)
      if (s) document.body.removeChild(s)
    }
    
    setTimeout(() => {
      const s = document.getElementById(scriptId)
      if (s) {
        cleanup()
        resolve([])
      }
    }, 10000)
    
    const st = order === 'desc' ? 'desc' : 'asc'
    script.src = `https://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=rzdf&st=${st}&pi=1&pn=${pageSize}&dx=1&v=${Date.now()}`
    document.body.appendChild(script)
  })
}

// ========== 板块及基金 ==========

export interface SectorFund {
  code: string
  name: string
  netValue: number
  dayReturn: number
}

export interface SectorInfo {
  code: string        // 板块代码
  name: string
  streak: string      // 连涨X天
  dayReturn: number
  funds: SectorFund[]
}

/**
 * 获取热门板块及基金列表
 */
export async function fetchSectorFunds(): Promise<SectorInfo[]> {
  const cacheKey = 'sector_funds'
  const cached = cache.get<SectorInfo[]>(cacheKey)
  if (cached) return cached
  
  try {
    // [WHAT] 获取行业板块
    const url = `https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=10&po=1&np=1&fltt=2&invt=2&fid=f3&fs=m:90+t:2&fields=f2,f3,f4,f12,f14&_=${Date.now()}`
    
    const response = await fetch(url)
    const data = await response.json()
    
    if (!data?.data?.diff) return []
    
    const sectors: SectorInfo[] = data.data.diff.slice(0, 6).map((item: any) => {
      // [WHAT] 确保 dayReturn 是数字类型
      const dayReturn = parseFloat(item.f3) || 0
      return {
        code: item.f12 || '',  // 板块代码
        name: item.f14 || '',
        streak: dayReturn > 0 ? '连涨1天' : (dayReturn < 0 ? '连跌1天' : ''),
        dayReturn,
        funds: [] // 先留空，后续可扩展
      }
    })
    
    cache.set(cacheKey, sectors, CACHE_TTL.MARKET_INDEX)
    return sectors
  } catch {
    return []
  }
}

// ========== 场内ETF ==========

export interface ETFItem {
  code: string
  name: string
  price: number
  dayReturn: number
}

/**
 * 获取场内ETF涨幅榜
 */
export async function fetchETFRank(pageSize = 10): Promise<ETFItem[]> {
  const cacheKey = `etf_rank_${pageSize}`
  const cached = cache.get<ETFItem[]>(cacheKey)
  if (cached) return cached
  
  try {
    // [WHAT] 获取ETF排行
    const url = `https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=${pageSize}&po=1&np=1&fltt=2&invt=2&fid=f3&fs=b:MK0021,b:MK0022&fields=f2,f3,f4,f12,f14&_=${Date.now()}`
    
    const response = await fetch(url)
    const data = await response.json()
    
    if (!data?.data?.diff) return []
    
    const result: ETFItem[] = data.data.diff.map((item: any) => ({
      code: item.f12 || '',
      name: item.f14 || '',
      price: item.f2 || 0,
      dayReturn: item.f3 || 0
    }))
    
    cache.set(cacheKey, result, 60000)
    return result
  } catch {
    return []
  }
}

// ========== 检查 API 可用性 ==========

/**
 * 检查 API 是否可用
 */
export async function checkApiAvailability(): Promise<boolean> {
  try {
    const response = await fetch(
      `https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=1&fid=f3&fs=b:MK0021&fields=f12&_=${Date.now()}`,
      { signal: AbortSignal.timeout(5000) }
    )
    return response.ok
  } catch {
    return false
  }
}

// ========== 财经资讯 ==========

export interface NewsItem {
  id: string
  title: string
  summary: string
  source: string
  time: string
  url: string
}

/**
 * 获取财经资讯列表
 * [WHY] 从多个数据源获取基金/财经相关新闻
 * [HOW] 尝试多个API，第一个成功就返回
 */
export async function fetchFinanceNews(pageSize = 10): Promise<NewsItem[]> {
  const cacheKey = `finance_news_${pageSize}`
  const cached = cache.get<NewsItem[]>(cacheKey)
  if (cached) return cached
  
  // [WHAT] 尝试东方财富7x24快讯（更活跃）
  try {
    const news = await fetchEastmoney7x24(pageSize)
    if (news.length > 0) {
      cache.set(cacheKey, news, 180000) // 3分钟缓存
      return news
    }
  } catch { /* 继续尝试下一个源 */ }
  
  // [WHAT] 备用：东方财富基金资讯
  try {
    const news = await fetchEastmoneyFundNews(pageSize)
    if (news.length > 0) {
      cache.set(cacheKey, news, 300000)
      return news
    }
  } catch { /* 继续 */ }
  
  return getDefaultNews()
}

// [WHAT] 东方财富7x24快讯（实时性强）
async function fetchEastmoney7x24(pageSize: number): Promise<NewsItem[]> {
  const callbackName = `news7x24_${Date.now()}`
  const url = `https://np-listapi.eastmoney.com/comm/web/getStockNews?cb=${callbackName}&_=${Date.now()}&type=0&pageSize=${pageSize}`
  
  const data = await jsonpRequest(url, callbackName, 5000)
  
  if (!data?.data?.list) return []
  
  return data.data.list.map((item: any) => ({
    id: String(item.art_id || Date.now() + Math.random()),
    title: item.title || '',
    summary: (item.digest || item.title || '').slice(0, 80),
    source: item.source || '7x24快讯',
    time: formatNewsTime(item.showtime || ''),
    url: item.url_unique || ''
  })).filter((n: NewsItem) => n.title)
}

// [WHAT] 东方财富基金资讯
async function fetchEastmoneyFundNews(pageSize: number): Promise<NewsItem[]> {
  const callbackName = `fundNews_${Date.now()}`
  const url = `https://np-listapi.eastmoney.com/comm/wap/getListInfo?cb=${callbackName}&client=wap&type=5&pageSize=${pageSize}&pageIndex=0&_=${Date.now()}`
  
  const data = await jsonpRequest(url, callbackName, 5000)
  
  if (!data?.data?.list) return []
  
  return data.data.list.map((item: any) => ({
    id: item.art_uniqueUrl || String(Date.now() + Math.random()),
    title: item.title || '',
    summary: (item.digest || item.title || '').slice(0, 80),
    source: item.source || '东方财富',
    time: formatNewsTime(item.showtime || item.time || ''),
    url: item.url || item.art_uniqueUrl || ''
  })).filter((n: NewsItem) => n.title)
}

// [WHAT] 通用JSONP请求函数
async function jsonpRequest(url: string, callbackName: string, timeout = 8000): Promise<any> {
  return new Promise((resolve, reject) => {
    const timeoutId = setTimeout(() => {
      cleanup()
      reject(new Error('timeout'))
    }, timeout)
    
    const cleanup = () => {
      clearTimeout(timeoutId)
      delete (window as any)[callbackName]
      const s = document.getElementById(`jsonp_${callbackName}`)
      if (s) s.remove()
    }
    
    ;(window as any)[callbackName] = (data: any) => {
      cleanup()
      resolve(data)
    }
    
    const script = document.createElement('script')
    script.id = `jsonp_${callbackName}`
    script.src = url
    script.onerror = () => {
      cleanup()
      reject(new Error('script error'))
    }
    document.head.appendChild(script)
  })
}

// [WHAT] 格式化资讯时间
function formatNewsTime(timeStr: string): string {
  if (!timeStr) return ''
  try {
    const date = new Date(timeStr)
    const now = new Date()
    const diff = now.getTime() - date.getTime()
    
    if (diff < 60000) return '刚刚'
    if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
    if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
    return (date.getMonth() + 1) + '-' + date.getDate()
  } catch {
    return timeStr
  }
}

// [WHAT] 默认资讯（API 失败时使用）
function getDefaultNews(): NewsItem[] {
  return [
    { 
      id: '1', 
      title: '基金投资需关注长期价值', 
      summary: '在市场波动中保持理性，坚持长期投资理念。分散投资降低风险，定期定额投资可平滑市场波动带来的影响。选择基金时应关注基金经理的投资能力和基金的历史业绩稳定性。', 
      source: '投资提示', 
      time: '今日', 
      url: '' 
    },
    { 
      id: '2', 
      title: 'A股市场投资策略分析', 
      summary: '当前市场呈现结构性行情，建议关注业绩确定性强的优质标的。科技创新、消费升级、绿色发展等主线值得重点关注。同时注意控制仓位，做好风险管理。', 
      source: '市场动态', 
      time: '今日', 
      url: '' 
    },
    { 
      id: '3', 
      title: '新能源行业投资机遇', 
      summary: '在"双碳"目标推动下，新能源产业迎来快速发展期。光伏、风电、储能、新能源汽车等细分领域均有较好的投资机会。建议通过相关主题基金参与投资。', 
      source: '行业资讯', 
      time: '今日', 
      url: '' 
    },
    { 
      id: '4', 
      title: '债券基金配置建议', 
      summary: '在当前利率环境下，债券基金可作为资产配置的重要组成部分。纯债基金风险较低，适合稳健型投资者；二级债基可获取一定的权益收益增强。', 
      source: '配置建议', 
      time: '今日', 
      url: '' 
    },
    { 
      id: '5', 
      title: '基金定投策略解读', 
      summary: '定投是一种简单有效的投资方式，通过分批买入平摊成本。建议选择波动较大的偏股型基金进行定投，长期坚持可获得较好的平均成本优势。', 
      source: '投资技巧', 
      time: '今日', 
      url: '' 
    },
    { 
      id: '6', 
      title: '基金交易注意事项', 
      summary: '基金交易时间为工作日9:30-15:00，15:00后提交的申购赎回按下一交易日净值计算。节假日前需提前规划资金安排，注意赎回到账时间。', 
      source: '交易提醒', 
      time: '今日', 
      url: '' 
    }
  ]
}

// ========== 分红记录 API ==========

/**
 * 分红记录类型
 * [WHAT] 每笔分红的详细信息
 */
export interface DividendRecord {
  date: string        // 权益登记日
  exDate: string      // 除息日  
  payDate: string     // 红利发放日
  amount: number      // 每份分红金额（元）
  type: '红利再投' | '现金分红'  // 分红方式
}

/**
 * 获取基金分红记录
 * [WHY] 投资者关心历史分红情况，评估基金收益分配能力
 * [HOW] 使用 JSONP 方式从天天基金 API 获取数据，避免 CORS 限制
 */
export async function fetchDividendRecords(fundCode: string): Promise<DividendRecord[]> {
  const cacheKey = `dividend_${fundCode}`
  const cached = cache.get<DividendRecord[]>(cacheKey)
  if (cached) return cached
  
  try {
    // [WHAT] 使用 JSONP 接口获取分红数据
    const cbName = `dividend_cb_${Date.now()}`
    const jsonUrl = `https://api.fund.eastmoney.com/f10/fhsp?fundcode=${fundCode}&callback=${cbName}`
    
    const jsonResp = await jsonpRequest(jsonUrl, cbName) as { 
      Datas?: { 
        fhspList?: Array<{ 
          DJRQ: string   // 登记日期
          FFRQ: string   // 发放日期
          CXRQ: string   // 除息日期
          FHFCZ: number  // 分红金额
          FHSP: string   // 分红说明
        }> 
      } 
    } | null
    
    const records: DividendRecord[] = []
    
    if (jsonResp?.Datas?.fhspList) {
      for (const item of jsonResp.Datas.fhspList) {
        records.push({
          date: item.DJRQ || '',
          exDate: item.CXRQ || '',
          payDate: item.FFRQ || '',
          amount: item.FHFCZ || 0,
          type: '现金分红'
        })
      }
    }
    
    cache.set(cacheKey, records, CACHE_TTL.LONG)
    return records
  } catch (error) {
    console.error('[API] 获取分红记录失败:', error)
    return []
  }
}

// ========== 费率查询 API ==========

/**
 * 基金费率信息
 * [WHAT] 申购、赎回、管理等各类费率
 */
export interface FundFeeInfo {
  // 申购费率（按金额分档）
  purchaseFees: Array<{ 
    minAmount: number   // 最小金额（万元）
    maxAmount: number   // 最大金额（万元）
    rate: number        // 原费率 (%)
    discountRate: number // 折扣后费率 (%)
  }>
  // 赎回费率（按持有天数分档）
  redemptionFees: Array<{
    minDays: number     // 最少持有天数
    maxDays: number     // 最多持有天数
    rate: number        // 费率 (%)
  }>
  // 管理费率（年化）
  managementFee: number
  // 托管费率（年化）
  custodianFee: number
  // 销售服务费（年化，C类基金）
  salesServiceFee: number
}

/**
 * 获取基金费率信息
 * [WHY] 投资者在买入/卖出前需要了解交易成本
 * [HOW] 根据基金代码后缀判断A/C类，返回对应费率结构
 * [NOTE] 由于天天基金接口有CORS限制，使用行业标准费率
 */
export async function fetchFundFees(fundCode: string): Promise<FundFeeInfo> {
  const cacheKey = `fees_${fundCode}`
  const cached = cache.get<FundFeeInfo>(cacheKey)
  if (cached) return cached
  
  // [WHAT] 判断基金类型（A类前端收费，C类销售服务费）
  // 通常：偶数结尾为A类，奇数结尾为C类；或名称含A/C
  const lastDigit = parseInt(fundCode.slice(-1))
  const isClassC = lastDigit % 2 === 1  // 简化判断：奇数为C类
  
  // [WHAT] A类基金费率（前端收费，无销售服务费）
  const classAFees: FundFeeInfo = {
    purchaseFees: [
      { minAmount: 0, maxAmount: 100, rate: 1.5, discountRate: 0.15 },
      { minAmount: 100, maxAmount: 300, rate: 1.2, discountRate: 0.12 },
      { minAmount: 300, maxAmount: 500, rate: 0.8, discountRate: 0.08 },
      { minAmount: 500, maxAmount: Infinity, rate: 1000, discountRate: 1000 }
    ],
    redemptionFees: [
      { minDays: 0, maxDays: 7, rate: 1.5 },
      { minDays: 7, maxDays: 30, rate: 0.5 },
      { minDays: 30, maxDays: 365, rate: 0.5 },
      { minDays: 365, maxDays: 730, rate: 0.25 },
      { minDays: 730, maxDays: Infinity, rate: 0 }
    ],
    managementFee: 1.5,
    custodianFee: 0.25,
    salesServiceFee: 0
  }
  
  // [WHAT] C类基金费率（无申购费，有销售服务费）
  const classCFees: FundFeeInfo = {
    purchaseFees: [
      { minAmount: 0, maxAmount: Infinity, rate: 0, discountRate: 0 }
    ],
    redemptionFees: [
      { minDays: 0, maxDays: 7, rate: 1.5 },
      { minDays: 7, maxDays: 30, rate: 0.5 },
      { minDays: 30, maxDays: Infinity, rate: 0 }
    ],
    managementFee: 1.5,
    custodianFee: 0.25,
    salesServiceFee: 0.4
  }
  
  const result = isClassC ? classCFees : classAFees
  cache.set(cacheKey, result, CACHE_TTL.LONG)
  return result
}

/**
 * 计算赎回费
 * [WHY] 根据持有天数和赎回金额计算实际赎回费用
 */
export function calculateRedemptionFee(
  holdingDays: number, 
  redemptionAmount: number,
  fees: FundFeeInfo['redemptionFees']
): { rate: number; fee: number } {
  // [WHAT] 找到对应的费率档位
  const tier = fees.find(f => holdingDays >= f.minDays && holdingDays < f.maxDays)
  const rate = tier?.rate || 0
  const fee = redemptionAmount * (rate / 100)
  
  return { rate, fee }
}

// ========== 基金公告 API ==========

/**
 * 基金公告类型
 */
export interface FundAnnouncement {
  id: string
  title: string
  date: string
  type: '分红公告' | '定期报告' | '人事变动' | '其他公告'
  url: string
}

/**
 * 获取基金公告列表
 * [WHY] 投资者需要了解基金的重大事项，如分红、换经理、持仓变化
 * [HOW] 由于CORS限制，直接返回默认公告数据
 */
export async function fetchFundAnnouncements(fundCode: string, _pageSize = 10): Promise<FundAnnouncement[]> {
  const cacheKey = `announcements_${fundCode}`
  const cached = cache.get<FundAnnouncement[]>(cacheKey)
  if (cached) return cached
  
  // [WHAT] 由于CORS限制，直接使用默认公告数据
  const announcements = getDefaultAnnouncements(fundCode)
  cache.set(cacheKey, announcements, CACHE_TTL.SHORT)
  return announcements
}

/**
 * 默认公告数据
 * [WHAT] 免责声明和风险提示
 */
function getDefaultAnnouncements(_fundCode: string): FundAnnouncement[] {
  const now = new Date()
  const formatDate = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
  
  return [
    {
      id: '1',
      title: '投资有风险，理财需谨慎',
      date: formatDate(now),
      type: '其他公告',
      url: ''
    },
    {
      id: '2', 
      title: '数据刷新有延迟，仅供学习和参考',
      date: formatDate(now),
      type: '其他公告',
      url: ''
    },
    {
      id: '3',
      title: '下载后请在24小时内删除',
      date: formatDate(now),
      type: '其他公告',
      url: ''
    }
  ]
}

// ========== 基金规模 API ==========

/**
 * 基金规模信息
 */
export interface FundScale {
  scale: number         // 基金规模（亿元）
  scaleDate: string     // 规模日期
  shareTotal: number    // 总份额（亿份）
  holderCount: number   // 持有人户数
  institutionRatio: number  // 机构持有占比 (%)
  personalRatio: number     // 个人持有占比 (%)
}

/**
 * 获取基金规模信息
 * [WHY] 规模影响基金运作效率，过大过小都有风险
 * [HOW] 使用JSONP从天天基金API获取数据
 */
export async function fetchFundScale(fundCode: string): Promise<FundScale> {
  const cacheKey = `scale_${fundCode}`
  const cached = cache.get<FundScale>(cacheKey)
  if (cached) return cached
  
  const defaultScale: FundScale = {
    scale: 0,
    scaleDate: '--',
    shareTotal: 0,
    holderCount: 0,
    institutionRatio: 0,
    personalRatio: 100
  }
  
  try {
    // [WHAT] 使用JSONP获取基金基本信息（包含规模）
    const cbName = `scale_cb_${Date.now()}`
    const url = `https://fundgz.1234567.com.cn/js/${fundCode}.js?rt=${Date.now()}`
    
    // [WHAT] 尝试从估值接口获取规模信息
    // 该接口返回的是js变量赋值，不是标准JSONP，需要特殊处理
    const response = await fetch(url).catch(() => null)
    if (response) {
      const text = await response.text()
      // [WHAT] 解析 jsonpgz({...}) 格式
      const match = text.match(/jsonpgz\(([\s\S]*)\)/)
      if (match) {
        const data = JSON.parse(match[1])
        // 估值接口不包含规模，返回默认值
        // 但可以确认基金存在
        if (data.fundcode) {
          cache.set(cacheKey, defaultScale, CACHE_TTL.LONG)
          return defaultScale
        }
      }
    }
    
    return defaultScale
  } catch (error) {
    console.error('[API] 获取基金规模失败:', error)
    return defaultScale
  }
}

// ========== 基金风格分析 API ==========

/**
 * 基金风格类型
 * [WHAT] 九宫格风格分类：大盘/中盘/小盘 × 价值/平衡/成长
 */
export interface FundStyle {
  /** 市值风格：大盘/中盘/小盘 */
  marketCap: 'large' | 'mid' | 'small' | 'unknown'
  /** 投资风格：价值/平衡/成长 */
  investStyle: 'value' | 'blend' | 'growth' | 'unknown'
  /** 风格标签文字 */
  styleLabel: string
  /** 股票仓位 */
  stockRatio: number
  /** 债券仓位 */
  bondRatio: number
  /** 现金仓位 */
  cashRatio: number
}

/**
 * 获取基金风格分析
 * [WHY] 了解基金的投资风格，辅助资产配置决策
 */
export async function fetchFundStyle(fundCode: string): Promise<FundStyle> {
  const cacheKey = `style_${fundCode}`
  const cached = cache.get<FundStyle>(cacheKey)
  if (cached) return cached
  
  const defaultStyle: FundStyle = {
    marketCap: 'unknown',
    investStyle: 'unknown',
    styleLabel: '未知',
    stockRatio: 0,
    bondRatio: 0,
    cashRatio: 0
  }
  
  try {
    // [WHAT] 从天天基金获取基金风格数据
    const url = `https://fund.eastmoney.com/pingzhongdata/${fundCode}.js?v=${Date.now()}`
    const response = await fetch(url)
    const text = await response.text()
    
    // [WHAT] 解析资产配置数据
    // 格式：var Data_assetAllocation = {...}
    const assetMatch = text.match(/var\s+Data_assetAllocation\s*=\s*(\{[\s\S]*?\});/)
    if (assetMatch) {
      const assetData = JSON.parse(assetMatch[1])
      // [WHAT] 获取最新一期的资产配置
      if (assetData.categories && assetData.series) {
        const latest = assetData.series.length - 1
        if (latest >= 0) {
          defaultStyle.stockRatio = assetData.series[0]?.data?.[latest] || 0
          defaultStyle.bondRatio = assetData.series[1]?.data?.[latest] || 0
          defaultStyle.cashRatio = assetData.series[2]?.data?.[latest] || 0
        }
      }
    }
    
    // [WHAT] 解析风格数据
    // 格式：var swithSameType = [...]
    const styleMatch = text.match(/var\s+swithSameType\s*=\s*(\[[\s\S]*?\]);/)
    if (styleMatch) {
      try {
        const styleData = JSON.parse(styleMatch[1])
        // [WHAT] 根据同类基金分类判断风格
        for (const item of styleData) {
          if (item[0] && typeof item[0] === 'string') {
            const name = item[0]
            // 判断市值风格
            if (name.includes('大盘')) defaultStyle.marketCap = 'large'
            else if (name.includes('中盘')) defaultStyle.marketCap = 'mid'
            else if (name.includes('小盘')) defaultStyle.marketCap = 'small'
            // 判断投资风格
            if (name.includes('价值')) defaultStyle.investStyle = 'value'
            else if (name.includes('成长')) defaultStyle.investStyle = 'growth'
            else if (name.includes('平衡')) defaultStyle.investStyle = 'blend'
          }
        }
      } catch {}
    }
    
    // [WHAT] 生成风格标签
    const capLabels = { large: '大盘', mid: '中盘', small: '小盘', unknown: '' }
    const investLabels = { value: '价值', blend: '平衡', growth: '成长', unknown: '' }
    defaultStyle.styleLabel = `${capLabels[defaultStyle.marketCap]}${investLabels[defaultStyle.investStyle]}`.trim() || '综合'
    
    cache.set(cacheKey, defaultStyle, CACHE_TTL.LONG)
    return defaultStyle
  } catch (error) {
    console.error('[API] 获取基金风格失败:', error)
    return defaultStyle
  }
}

// ========== 指数估值 API ==========

/**
 * 指数估值信息
 */
export interface IndexValuation {
  /** 指数代码 */
  code: string
  /** 指数名称 */
  name: string
  /** 当前PE */
  pe: number
  /** PE百分位（历史分位数） */
  pePercentile: number
  /** 当前PB */
  pb: number
  /** PB百分位 */
  pbPercentile: number
  /** 股息率 */
  dividendYield: number
  /** 估值状态：低估/正常/高估 */
  status: 'undervalued' | 'normal' | 'overvalued'
  /** 更新日期 */
  updateDate: string
}

/**
 * 获取主要指数估值
 * [WHY] 指数估值是判断市场位置的重要参考
 */
export async function fetchIndexValuations(): Promise<IndexValuation[]> {
  const cacheKey = 'index_valuations'
  const cached = cache.get<IndexValuation[]>(cacheKey)
  if (cached) return cached
  
  // [WHAT] 常见指数的默认估值数据（作为兜底）
  const defaultData: IndexValuation[] = [
    { code: '000300', name: '沪深300', pe: 12.5, pePercentile: 35, pb: 1.4, pbPercentile: 25, dividendYield: 2.8, status: 'normal', updateDate: '--' },
    { code: '000905', name: '中证500', pe: 22.0, pePercentile: 40, pb: 1.8, pbPercentile: 30, dividendYield: 1.5, status: 'normal', updateDate: '--' },
    { code: '000016', name: '上证50', pe: 10.5, pePercentile: 30, pb: 1.2, pbPercentile: 20, dividendYield: 3.2, status: 'undervalued', updateDate: '--' },
    { code: '399006', name: '创业板指', pe: 35.0, pePercentile: 45, pb: 4.5, pbPercentile: 40, dividendYield: 0.5, status: 'normal', updateDate: '--' },
    { code: '000922', name: '中证红利', pe: 6.5, pePercentile: 15, pb: 0.8, pbPercentile: 10, dividendYield: 5.5, status: 'undervalued', updateDate: '--' },
  ]
  
  try {
    // [WHAT] 尝试从乐估API获取实时估值数据
    const url = 'https://legulegu.com/api/stockdata/index-valuations'
    const response = await fetch(url, { mode: 'cors' }).catch(() => null)
    
    if (response && response.ok) {
      const data = await response.json()
      if (Array.isArray(data) && data.length > 0) {
        const result = data.map((item: Record<string, unknown>) => ({
          code: String(item.code || ''),
          name: String(item.name || ''),
          pe: Number(item.pe) || 0,
          pePercentile: Number(item.pe_percentile) || 50,
          pb: Number(item.pb) || 0,
          pbPercentile: Number(item.pb_percentile) || 50,
          dividendYield: Number(item.dividend_yield) || 0,
          status: getValuationStatus(Number(item.pe_percentile) || 50),
          updateDate: String(item.date || new Date().toISOString().split('T')[0])
        }))
        cache.set(cacheKey, result, CACHE_TTL.LONG)
        return result
      }
    }
    
    // [EDGE] API不可用时返回默认数据
    cache.set(cacheKey, defaultData, CACHE_TTL.LONG)
    return defaultData
  } catch (error) {
    console.error('[API] 获取指数估值失败:', error)
    return defaultData
  }
}

/**
 * 根据百分位判断估值状态
 */
function getValuationStatus(percentile: number): 'undervalued' | 'normal' | 'overvalued' {
  if (percentile <= 30) return 'undervalued'
  if (percentile >= 70) return 'overvalued'
  return 'normal'
}

// ========== 持有人结构 API ==========

/**
 * 持有人结构信息
 */
export interface HolderStructure {
  /** 机构持有比例(%) */
  institutionRatio: number
  /** 个人持有比例(%) */
  personalRatio: number
  /** 内部持有比例(%) */
  internalRatio: number
  /** 持有人户数 */
  holderCount: number
  /** 户均持有金额(元) */
  avgHolding: number
  /** 报告日期 */
  reportDate: string
}

/**
 * 获取基金持有人结构
 * [WHY] 机构持有比例高可能说明基金受专业投资者认可
 */
export async function fetchHolderStructure(fundCode: string): Promise<HolderStructure> {
  const cacheKey = `holder_${fundCode}`
  const cached = cache.get<HolderStructure>(cacheKey)
  if (cached) return cached
  
  const defaultData: HolderStructure = {
    institutionRatio: 0,
    personalRatio: 100,
    internalRatio: 0,
    holderCount: 0,
    avgHolding: 0,
    reportDate: '--'
  }
  
  try {
    // [WHAT] 从天天基金获取持有人结构
    const url = `https://fund.eastmoney.com/pingzhongdata/${fundCode}.js?v=${Date.now()}`
    const response = await fetch(url)
    const text = await response.text()
    
    // [WHAT] 解析持有人结构数据
    // 格式：var Data_holderStructure = {...}
    const match = text.match(/var\s+Data_holderStructure\s*=\s*(\{[\s\S]*?\});/)
    if (match) {
      const data = JSON.parse(match[1])
      if (data.series && data.categories) {
        const latestIdx = data.categories.length - 1
        if (latestIdx >= 0) {
          defaultData.reportDate = data.categories[latestIdx] || '--'
          // series[0] 机构, series[1] 个人, series[2] 内部
          defaultData.institutionRatio = data.series[0]?.data?.[latestIdx] || 0
          defaultData.personalRatio = data.series[1]?.data?.[latestIdx] || 100
          defaultData.internalRatio = data.series[2]?.data?.[latestIdx] || 0
        }
      }
    }
    
    cache.set(cacheKey, defaultData, CACHE_TTL.LONG)
    return defaultData
  } catch (error) {
    console.error('[API] 获取持有人结构失败:', error)
    return defaultData
  }
}

// ========== 基金业绩排名 API ==========

/**
 * 同类排名信息
 */
export interface FundRankInfo {
  /** 近1周排名 */
  rank1w: { rank: number; total: number; percentile: number }
  /** 近1月排名 */
  rank1m: { rank: number; total: number; percentile: number }
  /** 近3月排名 */
  rank3m: { rank: number; total: number; percentile: number }
  /** 近6月排名 */
  rank6m: { rank: number; total: number; percentile: number }
  /** 近1年排名 */
  rank1y: { rank: number; total: number; percentile: number }
  /** 成立以来排名 */
  rankTotal: { rank: number; total: number; percentile: number }
}

/**
 * 获取基金同类排名
 * [WHY] 排名是评估基金业绩的重要指标
 */
export async function fetchFundRankInfo(fundCode: string): Promise<FundRankInfo | null> {
  const cacheKey = `rankinfo_${fundCode}`
  const cached = cache.get<FundRankInfo>(cacheKey)
  if (cached) return cached
  
  try {
    const url = `https://fund.eastmoney.com/pingzhongdata/${fundCode}.js?v=${Date.now()}`
    const response = await fetch(url)
    const text = await response.text()
    
    // [WHAT] 解析同类排名数据
    // 格式：var Data_rateInSimilarType = [...]
    const match = text.match(/var\s+Data_rateInSimilarType\s*=\s*(\[[\s\S]*?\]);/)
    if (!match) return null
    
    const data = JSON.parse(match[1])
    
    const parseRank = (item: [string, number, number] | undefined) => {
      if (!item) return { rank: 0, total: 0, percentile: 0 }
      const rank = item[1] || 0
      const total = item[2] || 1
      const percentile = Math.round((1 - rank / total) * 100)
      return { rank, total, percentile }
    }
    
    const result: FundRankInfo = {
      rank1w: parseRank(data[0]),
      rank1m: parseRank(data[1]),
      rank3m: parseRank(data[2]),
      rank6m: parseRank(data[3]),
      rank1y: parseRank(data[4]),
      rankTotal: parseRank(data[5])
    }
    
    cache.set(cacheKey, result, CACHE_TTL.FUND_INFO)
    return result
  } catch (error) {
    console.error('[API] 获取基金排名失败:', error)
    return null
  }
}
