import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  mcpTokensApi,
  type MCPToken,
  type CreateTokenReq,
  type CreateTokenResp,
} from '@/api/mcpTokens'

export const useMcpTokensStore = defineStore('mcpTokens', () => {
  const tokens = ref<MCPToken[]>([])
  const loading = ref(false)

  async function fetch(params?: { active_only?: boolean }) {
    loading.value = true
    try {
      tokens.value = await mcpTokensApi.list(params)
    } finally {
      loading.value = false
    }
  }

  async function create(req: CreateTokenReq): Promise<CreateTokenResp> {
    const resp = await mcpTokensApi.create(req)
    await fetch()
    return resp
  }

  async function revoke(id: string) {
    await mcpTokensApi.revoke(id)
    await fetch()
  }

  return { tokens, loading, fetch, create, revoke }
})
