"""Personal / enterprise account-space model for NexaOffice.

The product direction is similar to a personal chat app plus an enterprise chat
app: separate spaces, linked identities. This module is intentionally small so
it can be used by UI and future server sync without coupling to Tkinter.
"""

from __future__ import annotations

from datetime import datetime
from typing import Dict, Any


def default_account_spaces(settings: Dict[str, Any] | None = None, enterprise: Dict[str, Any] | None = None) -> Dict[str, Any]:
    settings = settings or {}
    enterprise = enterprise or {}
    now = datetime.now().strftime("%Y-%m-%d %H:%M")
    return {
        "version": "v2026.06.19.07",
        "identity_link": {
            "puid": settings.get("puid", ""),
            "personal_puid": settings.get("personal_puid", ""),
            "enterprise_puid": settings.get("enterprise_puid", ""),
            "workspace_puid": enterprise.get("workspace_puid", ""),
            "updated_at": now,
        },
        "spaces": {
            "personal": {
                "model": "wechat_like",
                "name_key": "account_space_personal",
                "features": ["personal_chat", "friends", "groups", "files", "notes"],
                "read_receipts": False,
                "linked_by_puid": True,
            },
            "enterprise": {
                "model": "wecom_like",
                "name_key": "account_space_enterprise",
                "features": ["enterprise_chat", "staff_directory", "external_contacts", "groups", "files", "crm", "audit"],
                "read_receipts": True,
                "linked_by_workspace_puid": True,
            },
        },
    }


def sync_account_space_identity(model: Dict[str, Any], settings: Dict[str, Any] | None = None, enterprise: Dict[str, Any] | None = None) -> Dict[str, Any]:
    settings = settings or {}
    enterprise = enterprise or {}
    model.setdefault("identity_link", {})
    link = model["identity_link"]
    link["puid"] = settings.get("puid", link.get("puid", ""))
    link["personal_puid"] = settings.get("personal_puid", link.get("personal_puid", ""))
    link["enterprise_puid"] = settings.get("enterprise_puid", link.get("enterprise_puid", ""))
    link["workspace_puid"] = enterprise.get("workspace_puid", link.get("workspace_puid", ""))
    link["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M")
    return model
