"""Project role resolution for ProjectService."""

from __future__ import annotations

from noteflow.domain.identity import ProjectMembership, ProjectRole, WorkspaceRole


class ProjectRoleResolverMixin:
    """Resolve effective project roles from workspace roles and memberships."""

    def resolve_project_role(
        self,
        workspace_role: WorkspaceRole,
        project_membership: ProjectMembership | None,
    ) -> ProjectRole:
        """Resolve the effective project role for a user.

        Workspace OWNER and ADMIN always have ProjectRole.ADMIN.
        Otherwise, use explicit project membership role.
        Default to VIEWER if no explicit membership.

        Args:
            workspace_role: User's role in the parent workspace.
            project_membership: Explicit project membership (optional).

        Returns:
            Effective project role.
        """
        if workspace_role in (WorkspaceRole.OWNER, WorkspaceRole.ADMIN):
            return ProjectRole.ADMIN

        return project_membership.role if project_membership else ProjectRole.VIEWER
