import { defineStore } from 'pinia'
import { ref } from 'vue'
import { evolutionApi } from '@/api/evolution'
import type {
  Experience,
  EvolutionMetrics,
  TrendPoint,
  PitfallWarning,
  PathOptimization,
} from '@/api/evolution'

export const useEvolutionStore = defineStore('evolution', () => {
  // --- State ---
  const experiences = ref<Experience[]>([])
  const metrics = ref<EvolutionMetrics | null>(null)
  const trends = ref<TrendPoint[]>([])
  const pitfalls = ref<PitfallWarning[]>([])
  const optimizations = ref<PathOptimization[]>([])
  const period = ref<string>('7d')
  const isLoading = ref(false)
  const error = ref<string | null>(null)

  // --- Actions ---

  /** Load recent experiences */
  async function loadExperiences(taskType?: string): Promise<void> {
    isLoading.value = true
    error.value = null
    try {
      const data = await evolutionApi.getExperiences({
        task_type: taskType,
        limit: 50,
      })
      experiences.value = data.experiences
    } catch (err) {
      error.value = err instanceof Error ? err.message : '加载经验记录失败'
      console.error('Failed to load experiences:', err)
    } finally {
      isLoading.value = false
    }
  }

  /** Load evolution metrics */
  async function loadMetrics(periodParam?: string): Promise<void> {
    isLoading.value = true
    error.value = null
    const p = periodParam || period.value
    try {
      const data = await evolutionApi.getMetrics(p)
      metrics.value = data.metrics
      trends.value = data.trends
      period.value = p
    } catch (err) {
      error.value = err instanceof Error ? err.message : '加载指标数据失败'
      console.error('Failed to load metrics:', err)
    } finally {
      isLoading.value = false
    }
  }

  /** Check pitfall warnings */
  async function checkPitfalls(taskType: string, steps: string[]): Promise<void> {
    isLoading.value = true
    error.value = null
    try {
      const data = await evolutionApi.checkPitfalls({ task_type: taskType, steps })
      pitfalls.value = data.warnings
    } catch (err) {
      error.value = err instanceof Error ? err.message : '检查避坑预警失败'
      console.error('Failed to check pitfalls:', err)
    } finally {
      isLoading.value = false
    }
  }

  /** Load path optimization history */
  async function loadOptimizations(taskType?: string): Promise<void> {
    isLoading.value = true
    error.value = null
    try {
      const data = await evolutionApi.getPathOptimizations({
        task_type: taskType,
        limit: 20,
      })
      optimizations.value = data.optimizations
    } catch (err) {
      error.value = err instanceof Error ? err.message : '加载路径优化记录失败'
      console.error('Failed to load optimizations:', err)
    } finally {
      isLoading.value = false
    }
  }

  return {
    // State
    experiences,
    metrics,
    trends,
    pitfalls,
    optimizations,
    period,
    isLoading,
    error,
    // Actions
    loadExperiences,
    loadMetrics,
    checkPitfalls,
    loadOptimizations,
  }
})
