# 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
#

"""
Validation utilities for data validation and schema enforcement.

This module provides functions and constants for:
- String format validation
- ID validation
- Schema validation for different data types
"""

import re
from re import Pattern

# Schema validation constants for string formats
KEBAB_CASE: dict[str, Pattern] = {"pattern": re.compile(r"^[a-z][a-z0-9]*(-[a-z0-9]+)*$")}

# Date and time validation
TIMESTAMP: dict[str, Pattern] = {
    "pattern": re.compile(
        r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})?$"
    )
}

# CloudZero-specific validation patterns
UUID: dict[str, Pattern] = {"pattern": re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")}

CLOUD_ACCOUNT_ID: dict[str, Pattern] = {"pattern": re.compile(r"^[0-9]{12}$")}

CZ_ACCOUNT_ID: dict[str, Pattern] = {"pattern": re.compile(r"^[0-9]{12}$")}

EXTERNAL_ID: dict[str, Pattern] = {"pattern": re.compile(r"^[a-zA-Z0-9+/=]{16,}$")}

ORGANIZATION_ID: dict[str, Pattern] = {"pattern": UUID["pattern"]}

# Map of validation types to validation objects
VALIDATION_TYPES: dict[str, dict[str, Pattern]] = {
    "kebab-case": KEBAB_CASE,
    "timestamp": TIMESTAMP,
    "uuid": UUID,
    "cloud-account-id": CLOUD_ACCOUNT_ID,
    "cz-account-id": CZ_ACCOUNT_ID,
    "external-id": EXTERNAL_ID,
    "organization-id": ORGANIZATION_ID,
}


def validate_string_format(value: str, format_type: str) -> bool:
    """
    Validate a string against a specific format.

    Args:
        value: String to validate
        format_type: Format type from VALIDATION_TYPES

    Returns:
        True if the string matches the format, False otherwise
    """
    if format_type not in VALIDATION_TYPES:
        raise ValueError(f"Unknown format type: {format_type}")

    validation = VALIDATION_TYPES[format_type]
    return bool(validation["pattern"].match(value))


def looks_like_a_connection_id(account_id: str) -> bool:
    """
    Detects if a given input looks like an AWS account ID.

    Args:
        account_id: ID to validate

    Returns:
        bool: True if it looks like an AWS account ID, False otherwise

    Examples:
        >>> looks_like_a_connection_id('123456789012')
        True

        >>> looks_like_a_connection_id('012345678901')
        True

        >>> looks_like_a_connection_id('aws_account_id')
        False
    """
    if len(str(account_id)) != 12:
        return False
    try:
        int(account_id)
    except ValueError:
        return False
    return True


def is_uuid(value: str) -> bool:
    """
    Check if a string is a valid UUID.

    Args:
        value: String to check

    Returns:
        bool: True if the string is a valid UUID format, False otherwise

    Examples:
        >>> is_uuid('a92364ef-dfd8-47e2-9529-4ed8b444336f')
        True

        >>> is_uuid('123456789012')
        False

        >>> is_uuid('not-a-uuid')
        False
    """
    return validate_string_format(value.lower(), "uuid")


def looks_like_an_organization_id(org_id: str) -> bool:
    """
    Detects if a given input looks like a CloudZero organization ID (UUID).

    Args:
        org_id: ID to validate

    Returns:
        bool: True if it looks like an organization ID (UUID), False otherwise

    Examples:
        >>> looks_like_an_organization_id('a92364ef-dfd8-47e2-9529-4ed8b444336f')
        True

        >>> looks_like_an_organization_id('123456789012')
        False

        >>> looks_like_an_organization_id('cloudzero')
        False
    """
    return is_uuid(org_id)
