from __future__ import annotations

from app.core.config import clear_runtime_settings_cache
from app.schemas.settings import SettingsCacheClearItemRead, SettingsCacheClearRead
from app.services.application_location_semantics import clear_application_location_semantic_caches
from app.services.knowledge_rag_local import clear_local_knowledge_index_cache
from app.services.ocr import OcrService
from app.services.runtime_chat import clear_runtime_chat_failure_cache


class SystemCacheService:
    def clear_all(self) -> SettingsCacheClearRead:
        items = [
            SettingsCacheClearItemRead(
                cacheKey="ocr_result_cache",
                label="OCR 识别结果缓存",
                clearedCount=OcrService.clear_result_cache(),
            ),
            SettingsCacheClearItemRead(
                cacheKey="runtime_settings_cache",
                label="运行时配置缓存",
                clearedCount=clear_runtime_settings_cache(),
            ),
            SettingsCacheClearItemRead(
                cacheKey="runtime_chat_failure_cache",
                label="模型调用失败冷却缓存",
                clearedCount=clear_runtime_chat_failure_cache(),
            ),
            SettingsCacheClearItemRead(
                cacheKey="knowledge_local_index_cache",
                label="知识库本地索引缓存",
                clearedCount=clear_local_knowledge_index_cache(),
            ),
            SettingsCacheClearItemRead(
                cacheKey="application_location_semantic_cache",
                label="地点语义分析缓存",
                clearedCount=clear_application_location_semantic_caches(),
            ),
        ]
        total_cleared = sum(item.clearedCount for item in items)
        return SettingsCacheClearRead(totalCleared=total_cleared, items=items)
