"""
Project/task ID comparison helpers.

Kept in a non-Cython module so ws_engine and IPC handlers work even when an older
activity_controller.pyd is bundled (compile_native.py does not compile this file).
"""

from __future__ import annotations

from typing import Any, Optional


def normalize_task_id_for_compare(task_id: Optional[str]) -> str:
    if task_id is None or task_id == "":
        return "0"
    return str(task_id)


def project_task_changed(
    activity: Any,
    new_project_id: Optional[str],
    new_task_id: Optional[str],
) -> bool:
    """
    Return True if project or task differs from activity's current values.

    Uses ActivityController._project_task_changed when present (fresh .py / .pyd);
    otherwise compares current_project_id / current_task_id directly.
    """
    fn = getattr(activity, "_project_task_changed", None)
    if callable(fn):
        return bool(fn(new_project_id, new_task_id))

    old_p = str(getattr(activity, "current_project_id", None) or "1001")
    new_p = str(new_project_id) if new_project_id is not None else "1001"
    old_t = normalize_task_id_for_compare(getattr(activity, "current_task_id", None))
    new_t = normalize_task_id_for_compare(
        str(new_task_id) if new_task_id is not None else None
    )
    return old_p != new_p or old_t != new_t
