# -*- coding: utf-8 -*-
"""业务层 · 工作台聚合服务（各端口 dashboard）。"""
from __future__ import annotations

from ..infrastructure.repositories import Database


class DashboardService:
    """OPC 工作台聚合：统计/进行中任务/智能体建议/月度收入/最新消息。"""

    def __init__(self, db: Database):
        self.db = db

    async def opc_dashboard(self, uid: str) -> dict:
        profile = await self.db.opc_profiles.get(uid)
        task_counts = await self.db.opc_tasks.counts(uid)
        opc_tasks = await self.db.opc_tasks.list_by_user(uid)
        active_tasks = [t for t in opc_tasks if t["status"] in ("in_progress", "urgent")][:3]

        policy_count = len(
            [c for c in await self.db.content.list(ctype="policy", status="published")]
        )
        agent_tips = [
            {"title": "政策匹配提醒", "desc": f"{policy_count} 项新政策与您高度匹配", "type": "policy"},
            {"title": "报税截止提醒", "desc": "本月报税截止 7月31日", "type": "tax"},
            {"title": "技能提升推荐", "desc": "推荐参加 UI 设计研修班", "type": "skill"},
        ]
        return {
            "stats": {
                "inProgressTasks": task_counts["in_progress"],
                "completedTasks": task_counts["completed"],
                "totalEarnings": await self.db.finance.total_income(uid),
                "creditScore": (profile or {}).get("credit_score", 80),
            },
            "activeTasks": active_tasks,
            "agentTips": agent_tips,
            "monthlyIncome": await self.db.finance.monthly_income(uid),
            "recentMessages": await self.db.messages.recent(uid, limit=4),
        }
