import axios from 'axios'
import request from '@/utils/http'
import { useUserStore } from '@/store/modules/user'

export interface Application { id:string;code:string;name:string;description:string;published_version?:number }
export interface Knowledge { id:string;name:string;description:string;document_count:number;chunk_count:number }
export interface Tool { id:string;code:string;name:string;description:string;input_schema:Record<string,unknown> }
export interface PromptVersion { version:number;content:string;variables:Array<{name:string;label?:string;required:boolean}> }
export interface Prompt { id:string;name:string;description:string;category_name:string;tags:string[];favorite:boolean;current?:PromptVersion }
export interface Catalog { applications:Application[];knowledge:Knowledge[];tools:Tool[];prompts:Prompt[] }
export interface Model { provider_code:string;id:string;owned_by:string }
export interface ModelRequest { id:string;provider_code:string;model:string;reason:string;requested_rpm:number;requested_monthly_tokens:number;status:string;decision_note:string;created_at:string }
export interface Stats { days:number;requests:number;failed_requests:number;prompt_tokens:number;completion_tokens:number;total_tokens:number;cost_microunits:number;avg_latency_ms?:number }
export interface AuditEvent { id:string;request_id:string;provider_code?:string;model?:string;protocol:string;status_code?:number;prompt_tokens?:number;completion_tokens?:number;cost_microunits?:number;latency_ms?:number;recorded_at:string }

export const fetchCatalog=()=>request.get<Catalog>({url:'/api/v1/portal/catalog'})
export const fetchPrompts=(q='')=>request.get<Prompt[]>({url:'/api/v1/portal/prompts',params:{q}})
export const setPromptFavorite=(id:string,value:boolean)=>value?request.post({url:`/api/v1/portal/prompts/${id}/favorite`}):request.del({url:`/api/v1/portal/prompts/${id}/favorite`})
export const fetchModels=()=>request.get<Model[]>({url:'/api/v1/portal/model-requests/available'})
export const fetchModelRequests=()=>request.get<ModelRequest[]>({url:'/api/v1/portal/model-requests'})
export const createModelRequest=(params:Pick<ModelRequest,'provider_code'|'model'|'reason'|'requested_rpm'|'requested_monthly_tokens'>)=>request.post<ModelRequest>({url:'/api/v1/portal/model-requests',params})
export const fetchStats=(days:number)=>request.get<Stats>({url:'/api/v1/portal/stats',params:{days}})
export const fetchLogs=(days:number,limit=50)=>request.get<{items:AuditEvent[]}>({url:'/api/v1/portal/logs',params:{days,limit}})
export const changePassword=(params:{old_password:string;new_password:string})=>request.post({url:'/api/v1/portal/password',params})

// --- 个人文件仓库（M8 对象存储）---
export interface FileObject { id:string;original_name:string;content_type:string;size_bytes:number;content_sha256:string;scope:string;created_at:string;updated_at:string }
export const fetchMyFiles=()=>request.get<FileObject[]>({url:'/api/v1/portal/files'})
export const uploadMyFile=(file:File)=>{
  const form=new FormData()
  form.append('file',file)
  return request.post<FileObject>({url:'/api/v1/portal/files',data:form,timeout:120000})
}
export const deleteMyFile=(id:string)=>request.del({url:`/api/v1/portal/files/${id}`})
export async function downloadMyFile(id:string,filename:string){
  const { accessToken } = useUserStore()
  const token = accessToken.startsWith('Bearer ')?accessToken:`Bearer ${accessToken}`
  const res = await axios.get(`/api/v1/portal/files/${id}/download`,{
    baseURL:import.meta.env.VITE_API_URL,
    headers:{Authorization:token},
    responseType:'blob'
  })
  const url = URL.createObjectURL(res.data)
  const anchor = document.createElement('a')
  anchor.href = url
  anchor.download = filename
  anchor.click()
  URL.revokeObjectURL(url)
}


// --- 资源市场 ---
export interface MarketItem { type:string;code:string;name:string;description:string;category_id?:string;category_name:string;tags:string[];department_ids:string[];updated_at:string }
export interface MarketplaceCategory { id:string;name:string;description:string;resource_type:string;sort_order:number }
export const fetchMarketplace=(params?:{type?:string;category_id?:string;tag?:string;q?:string})=>request.get<MarketItem[]>({url:'/api/v1/portal/marketplace',params})
export const fetchMarketplaceCategories=()=>request.get<MarketplaceCategory[]>({url:'/api/v1/portal/marketplace/categories'})
export const fetchMarketplaceInstalled=()=>request.get<MarketItem[]>({url:'/api/v1/portal/marketplace/installed'})
export const fetchMarketplaceDetail=(type:string,code:string)=>request.get<{item:MarketItem;detail:Record<string,unknown>}>({url:`/api/v1/portal/marketplace/${type}/${code}`})
export const marketplaceInstall=(type:string,code:string)=>request.post<{installed:boolean;created:boolean}>({url:`/api/v1/portal/marketplace/${type}/${code}/install`})
export const marketplaceUninstall=(type:string,code:string)=>request.del<{installed:boolean}>({url:`/api/v1/portal/marketplace/${type}/${code}/install`})
