from __future__ import annotations

import json
import re
from pathlib import Path
from typing import Any

_SESSION_ID_PATTERN = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
_OP_TYPE_PATTERN = re.compile(r"^[a-zA-Z0-9._-]+$")


def validate_session_id(session_id: str) -> None:
    if not _SESSION_ID_PATTERN.match(session_id):
        raise ValueError(f"Invalid session ID format: {session_id}")


def validate_payload(payload: dict[str, Any]) -> None:
    if not isinstance(payload, dict):
        raise TypeError("Payload must be a dictionary")

    payload_str = json.dumps(payload)
    if len(payload_str) > 10_000_000:
        raise ValueError("Payload too large")


def sanitize_op_type(op_type: str) -> str:
    if not _OP_TYPE_PATTERN.match(op_type):
        raise ValueError(f"Invalid operation type: {op_type}")
    return op_type


def validate_file_path(path: str) -> Path:
    return Path(path).expanduser().resolve()
