import { buildQuery, request } from './client'

export interface MCPTokenScope {
  tools?: string[] | null
  project_ids?: string[] | null
}

export interface MCPToken {
  id: string
  name: string
  issuer: 'admin' | 'system'
  scope: MCPTokenScope
  expires_at?: string | null
  last_used_at?: string | null
  revoked_at?: string | null
  created_at: string
}

export interface CreateTokenReq {
  user_id?: string
  name: string
  scope?: MCPTokenScope
  expires_at: string
}

export interface CreateTokenResp {
  id: string
  token: string
  expires_at: string
}

export const mcpTokensApi = {
  list: (params?: { active_only?: boolean }) =>
    request<MCPToken[]>(
      `/api/v1/mcp/tokens${buildQuery(params ?? {})}`,
    ),

  create: (req: CreateTokenReq) =>
    request<CreateTokenResp>('/api/v1/mcp/tokens', { method: 'POST', body: req }),

  revoke: (id: string) =>
    request<void>(`/api/v1/mcp/tokens/${encodeURIComponent(id)}`, { method: 'DELETE' }),
}
