from __future__ import annotations

from flow_xhs.platform import REDNOTE_PLATFORM
from flow_xhs.runtime.accounts.repository import FileAccountRepository
from flow_xhs.errors import InvalidArgsError


def execute_account_activate(account_uid: str) -> dict:
    repo = FileAccountRepository()
    account = repo.get_account(platform=REDNOTE_PLATFORM, account_uid=account_uid)
    if not account:
        raise InvalidArgsError(f"账号不存在: {account_uid}")
    if not bool(account.get("is_logged_in")):
        raise InvalidArgsError(f"账号未登录，无法激活环境: {account_uid}")
    changed = repo.activate(platform=REDNOTE_PLATFORM, account_uid=account_uid)
    return {
        "changed": changed,
        "platform": REDNOTE_PLATFORM,
        "account_uid": account_uid,
        "is_current": True,
    }


def execute_account_delete(account_uid: str) -> dict:
    repo = FileAccountRepository()
    cleared_current = repo.get_active_account_uid(platform=REDNOTE_PLATFORM) == str(account_uid or "").strip()
    deleted = repo.delete(platform=REDNOTE_PLATFORM, account_uid=account_uid)
    return {
        "deleted": deleted,
        "platform": REDNOTE_PLATFORM,
        "account_uid": account_uid,
        "cleared_current": cleared_current,
    }
