import { defineStore } from 'pinia'
import axios from 'axios'

// 定义算法类型
interface Algorithm {
  id: string
  name: string
  description: string
  type: string
  status: string
  versions: AlgorithmVersion[]
  created_at: string
  updated_at: string
}

// 定义算法版本类型
interface AlgorithmVersion {
  id: string
  algorithm_id: string
  version: string
  url: string
  params: any
  input_schema: any
  output_schema: any
  is_default: boolean
  created_at: string
  updated_at: string
}

// 定义算法调用请求类型
interface AlgorithmCallRequest {
  algorithm_id: string
  version_id: string
  input_data: any
  params: any
}

// 定义算法调用结果类型
interface AlgorithmCallResult {
  id: string
  status: string
  output_data: any
  response_time: number
  error_message: string
  created_at: string
}

// 定义算法存储
export const useAlgorithmStore = defineStore('algorithm', {
  state: () => ({
    algorithms: [] as Algorithm[],
    currentAlgorithm: null as Algorithm | null,
    currentVersion: null as AlgorithmVersion | null,
    callResult: null as AlgorithmCallResult | null,
    loading: false,
    error: null as string | null
  }),
  
  getters: {
    getAlgorithmById: (state) => (id: string) => {
      return state.algorithms.find(algorithm => algorithm.id === id)
    }
  },
  
  actions: {
    // 获取算法列表
    async fetchAlgorithms(type?: string) {
      this.loading = true
      this.error = null

      try {
        const params = type ? { type } : {}
        const response = await axios.get('/algorithms', { params })
        this.algorithms = response.data.algorithms
        return true
      } catch (error: any) {
        this.error = error.response?.data?.detail || '获取算法列表失败'
        return false
      } finally {
        this.loading = false
      }
    },

    // 获取算法详情
    async fetchAlgorithmDetail(id: string) {
      this.loading = true
      this.error = null

      try {
        const response = await axios.get(`/algorithms/${id}`)
        this.currentAlgorithm = response.data
        return true
      } catch (error: any) {
        this.error = error.response?.data?.detail || '获取算法详情失败'
        return false
      } finally {
        this.loading = false
      }
    },

    // 获取算法版本列表
    async fetchAlgorithmVersions(algorithmId: string) {
      this.loading = true
      this.error = null

      try {
        const response = await axios.get(`/algorithms/${algorithmId}/versions`)
        if (this.currentAlgorithm) {
          this.currentAlgorithm.versions = response.data
        }
        return true
      } catch (error: any) {
        this.error = error.response?.data?.detail || '获取算法版本列表失败'
        return false
      } finally {
        this.loading = false
      }
    },

    // 调用算法
    async callAlgorithm(request: AlgorithmCallRequest) {
      this.loading = true
      this.error = null

      try {
        const response = await axios.post('/algorithms/call', request)
        this.callResult = response.data
        return true
      } catch (error: any) {
        this.error = error.response?.data?.detail || '调用算法失败'
        return false
      } finally {
        this.loading = false
      }
    },

    // 获取算法调用结果
    async fetchCallResult(callId: string) {
      this.loading = true
      this.error = null

      try {
        const response = await axios.get(`/algorithms/calls/${callId}`)
        this.callResult = response.data
        return true
      } catch (error: any) {
        this.error = error.response?.data?.detail || '获取调用结果失败'
        return false
      } finally {
        this.loading = false
      }
    },

    // 生成仿真输入数据
    async generateSimulationData(prompt: string, dataType: string = 'text') {
      this.loading = true
      this.error = null

      try {
        const response = await axios.post('/openai/generate-data', { prompt, data_type: dataType })
        return response.data
      } catch (error: any) {
        this.error = error.response?.data?.detail || '生成仿真数据失败'
        return null
      } finally {
        this.loading = false
      }
    },
    
    // 创建算法
    async createAlgorithm(algorithmData: any) {
      this.loading = true
      this.error = null

      try {
        const response = await axios.post('/algorithms', algorithmData)
        return response.data
      } catch (error: any) {
        this.error = error.response?.data?.detail || '创建算法失败'
        return null
      } finally {
        this.loading = false
      }
    },

    // 重置状态
    reset() {
      this.currentAlgorithm = null
      this.currentVersion = null
      this.callResult = null
      this.error = null
    }
  }
})
