import { agentApplication, aiModel, knowledge, resource, storageProvider } from '@repo/api-service'

export interface AgentBasicConfig {
	system_prompt?: string
	context_template?: string
}

export interface AgentModelConfig {
	model_id?: string
	rerank_model_id?: string
	temperature?: number
	max_completion_tokens?: number
	thinking?: boolean
}

export interface AgentSettingConfig {
	max_iterations?: number
	llm_call_timeout?: number
	allowed_tools?: string[]
	mcp_selection_mode?: string
	mcp_services?: string[]
	skills_selection_mode?: string
	selected_skills?: string[]
}

export interface AgentKnowledgeConfig {
	kb_selection_mode?: string
	knowledge_bases?: string[]
	retrieve_kb_only_when_mentioned?: boolean
	supported_file_types?: string[]
}

export interface AgentImageConfig {
	image_upload_enabled?: boolean
	vlm_model_id?: string
	image_storage_provider?: string
	audio_upload_enabled?: boolean
	asr_model_id?: string
}

export interface AgentFaqConfig {
	faq_priority_enabled?: boolean
	faq_direct_answer_threshold?: number
	faq_score_boost?: number
}

export interface AgentWebSearchConfig {
	web_search_enabled?: boolean
	web_search_max_results?: number
	web_search_provider_id?: string
	web_fetch_enabled?: boolean
	web_fetch_top_n?: number
}

export interface AgentMultipleConfig {
	multi_turn_enabled?: boolean
	history_turns?: number
}

export interface AgentSearchConfig {
	embedding_top_k?: number
	keyword_threshold?: number
	vector_threshold?: number
	rerank_top_k?: number
	rerank_threshold?: number
}

export interface AgentAdvancedConfig {
	enable_query_expansion?: boolean
	enable_rewrite?: boolean
	rewrite_prompt_system?: string
	rewrite_prompt_user?: string
	fallback_strategy?: string
	fallback_response?: string
	fallback_prompt?: string
}

export interface AgentConfig {
	basic_config?: AgentBasicConfig
	model_config?: AgentModelConfig
	setting_config?: AgentSettingConfig
	kb_config?: AgentKnowledgeConfig
	img_vlm_config?: AgentImageConfig
	faq_config?: AgentFaqConfig
	web_search_config?: AgentWebSearchConfig
	multiple_config?: AgentMultipleConfig
	search_config?: AgentSearchConfig
	advanced_config?: AgentAdvancedConfig
}

export interface AgentFormConfig {
	basic_config: Required<AgentBasicConfig>
	model_config: Required<AgentModelConfig>
	setting_config: Required<AgentSettingConfig>
	kb_config: Required<AgentKnowledgeConfig>
	img_vlm_config: Required<AgentImageConfig>
	faq_config: Required<AgentFaqConfig>
	web_search_config: Required<AgentWebSearchConfig>
	multiple_config: Required<AgentMultipleConfig>
	search_config: Required<AgentSearchConfig>
	advanced_config: Required<AgentAdvancedConfig>
}

export interface AgentFormData {
	name: string
	description: string
	avatar: string
	mode: string
	config: AgentFormConfig
}

export type AgentPageResponse = Awaited<ReturnType<typeof agentApplication.postAiAgentPageList>>
export type AgentPageResult = NonNullable<AgentPageResponse['result']>
export type AgentItem = AgentPageResult['model'][number] & {
	status?: string
	config?: AgentConfig
}

export type ModelPageResponse = Awaited<ReturnType<typeof aiModel.postModelPageList>>
export type ModelItem = NonNullable<ModelPageResponse['result']>['model'][number]

export type KnowledgePageResponse = Awaited<
	ReturnType<typeof knowledge.postKnowledgeBasePageList>
>
export type KnowledgeItem = NonNullable<KnowledgePageResponse['result']>['model'][number]

export interface AgentSelectOption {
	label: string
	value: string
	description?: string
}

export type StorageProviderEnginesResponse = Awaited<
	ReturnType<typeof storageProvider.postStorageProviderEngines>
>
export type StorageProviderEngineItem = NonNullable<StorageProviderEnginesResponse['result']>[number]

export type McpPageResponse = Awaited<ReturnType<typeof resource.postMcpPageList>>
export type McpItem = NonNullable<McpPageResponse['result']>['model'][number]

export type SkillsListResponse = Awaited<ReturnType<typeof resource.postSkillsList>>
export type SkillItem = NonNullable<SkillsListResponse['result']>[number]
