# Copyright (c) CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

"""
Organization Status Utilities

Centralized logic for determining and formatting organization enabled/disabled status.
This consolidates all organization status-related logic into reusable functions.
"""

from typing import Any


def normalize_organization_data(org_data: dict[str, Any]) -> dict[str, Any]:
    """
    Normalize organization data to use consistent field names.

    Maps various field name variations to standardized names:
    - Uses organization_enabled as the single source of truth for enabled status
    - Handles backward compatibility with organizations_enabled and is_enabled

    Args:
        org_data: Raw organization data dictionary

    Returns:
        Normalized organization data dictionary
    """
    normalized = org_data.copy()

    # Primary enabled status mapping - organization_enabled is the source of truth
    if "organization_enabled" not in normalized:
        if "organizations_enabled" in normalized:
            # Legacy field name mapping
            normalized["organization_enabled"] = normalized["organizations_enabled"]
        elif "is_enabled" in normalized:
            # Legacy API uses is_enabled
            normalized["organization_enabled"] = normalized["is_enabled"]
        else:
            # Default to False if no enabled field exists
            normalized["organization_enabled"] = False

    return normalized


def is_organization_enabled(org_data: dict[str, Any]) -> bool:
    """
    Determine if an organization is enabled based on its data.

    Primary logic: An organization is enabled if organization_enabled is True.
    This is the single source of truth for "enabled" determination.

    Args:
        org_data: Organization data dictionary (raw or normalized)

    Returns:
        True if organization is enabled, False otherwise
    """
    # Normalize data to handle field name variations
    normalized = normalize_organization_data(org_data)

    # Primary enabled gate using organization_enabled field
    return normalized.get("organization_enabled", False)


def format_organization_status_consolidated(org_data: dict[str, Any]) -> str:
    """
    Return the plain-text enabled/disabled status for an organization.

    Args:
        org_data: Organization data dictionary

    Returns:
        "Enabled" or "Disabled"
    """
    return "Enabled" if is_organization_enabled(org_data) else "Disabled"


def should_include_in_enabled_filter(org_data: dict[str, Any], enabled_only: bool) -> bool:
    """
    Determine if an organization should be included when filtering by enabled status.

    Args:
        org_data: Organization data dictionary
        enabled_only: True to include only enabled organizations

    Returns:
        True if organization should be included in results
    """
    if not enabled_only:
        return True  # Include all organizations if not filtering

    return is_organization_enabled(org_data)
