"""Contains all protection utils."""

import logging
from pathlib import Path

import requests

_logger = logging.getLogger(__name__)


def protect_wheel_wrapper(  # noqa: PLR0913
    *, wheel_file: Path, identifier: str, api_endpoint: str, access_key_id: str, access_secret_key: str, replace: bool
) -> None:
    """Protect a Python wheel file using the protector service.

    Args:
        wheel_file: Path to the wheel file to protect
        identifier: Protection identifier (e.g., 'dev-software-py-package-1')
        api_endpoint: The protector API endpoint
        access_key_id: Access key ID for authentication
        access_secret_key: Access secret key for authentication
        replace: If True, replace the original wheel file with the encrypted version
    """
    url = f"{api_endpoint.rstrip('/')}/encrypt/python"
    params = {"identifier": identifier}
    headers = {"access_key_id": access_key_id, "access_secret_key": access_secret_key}

    _logger.info("Sending wheel '%s' to '%s' for encryption", wheel_file, url)

    with wheel_file.open("rb") as file_handler:
        files = {"source": file_handler}
        response = requests.post(url, headers=headers, params=params, files=files, timeout=60)
        response.raise_for_status()

    output_file = wheel_file if replace else wheel_file.parent / f"{wheel_file.stem}_encrypted{wheel_file.suffix}"

    _logger.info("Writing protected wheel to file '%s'", output_file)

    with output_file.open("wb") as file_handler:
        file_handler.write(response.content)
