"""Contains all integration tests for the protect wheel CLI."""

import os
from pathlib import Path
from unittest.mock import MagicMock, patch

from typer.testing import CliRunner

from voraus_pipeline_utils.cli.main import app


@patch.dict(
    os.environ,
    {
        "PROTECTOR_ACCESS_KEY_ID": "test_key_id",
        "PROTECTOR_ACCESS_SECRET_KEY": "test_secret_key",
    },
)
@patch("voraus_pipeline_utils.cli.protect.protect_wheel_wrapper")
def test_protect_wheel_success(protect_wheel_wrapper_mock: MagicMock, cli_runner: CliRunner) -> None:
    result = cli_runner.invoke(app=app, args=["protect", "wheel", "/path/to/wheel.whl", "--identifier=test-package-1"])
    assert result.exit_code == 0

    protect_wheel_wrapper_mock.assert_called_once_with(
        wheel_file=Path("/path/to/wheel.whl"),
        identifier="test-package-1",
        api_endpoint="https://protector.vorausrobotik.com",
        access_key_id="test_key_id",
        access_secret_key="test_secret_key",  # noqa: S106
        replace=True,
    )


@patch.dict(
    os.environ,
    {
        "PROTECTOR_ACCESS_KEY_ID": "test_key_id",
        "PROTECTOR_ACCESS_SECRET_KEY": "test_secret_key",
    },
)
@patch("voraus_pipeline_utils.cli.protect.protect_wheel_wrapper")
def test_protect_wheel_with_custom_endpoint(protect_wheel_wrapper_mock: MagicMock, cli_runner: CliRunner) -> None:
    result = cli_runner.invoke(
        app=app,
        args=[
            "protect",
            "wheel",
            "/path/to/wheel.whl",
            "--identifier=test-package-1",
            "--api-endpoint=https://custom.endpoint.com",
        ],
    )
    assert result.exit_code == 0

    protect_wheel_wrapper_mock.assert_called_once_with(
        wheel_file=Path("/path/to/wheel.whl"),
        identifier="test-package-1",
        api_endpoint="https://custom.endpoint.com",
        access_key_id="test_key_id",
        access_secret_key="test_secret_key",  # noqa: S106
        replace=True,
    )


@patch.dict(
    os.environ,
    {
        "PROTECTOR_ACCESS_KEY_ID": "test_key_id",
        "PROTECTOR_ACCESS_SECRET_KEY": "test_secret_key",
    },
)
@patch("voraus_pipeline_utils.cli.protect.protect_wheel_wrapper")
def test_protect_wheel_no_replace(protect_wheel_wrapper_mock: MagicMock, cli_runner: CliRunner) -> None:
    result = cli_runner.invoke(
        app=app,
        args=["protect", "wheel", "/path/to/wheel.whl", "--identifier=test-package-1", "--no-replace"],
    )
    assert result.exit_code == 0

    protect_wheel_wrapper_mock.assert_called_once_with(
        wheel_file=Path("/path/to/wheel.whl"),
        identifier="test-package-1",
        api_endpoint="https://protector.vorausrobotik.com",
        access_key_id="test_key_id",
        access_secret_key="test_secret_key",  # noqa: S106
        replace=False,
    )


@patch("voraus_pipeline_utils.cli.protect.protect_wheel_wrapper")
def test_protect_wheel_with_explicit_credentials(protect_wheel_wrapper_mock: MagicMock, cli_runner: CliRunner) -> None:
    result = cli_runner.invoke(
        app=app,
        args=[
            "protect",
            "wheel",
            "/path/to/wheel.whl",
            "--identifier=test-package-1",
            "--access-key-id=explicit_key",
            "--access-secret-key=explicit_secret",
        ],
    )
    assert result.exit_code == 0

    protect_wheel_wrapper_mock.assert_called_once_with(
        wheel_file=Path("/path/to/wheel.whl"),
        identifier="test-package-1",
        api_endpoint="https://protector.vorausrobotik.com",
        access_key_id="explicit_key",
        access_secret_key="explicit_secret",  # noqa: S106
        replace=True,
    )


@patch.dict(
    os.environ,
    {
        "PROTECTOR_API_ENDPOINT": "https://env-endpoint.example.com",
        "PROTECTOR_ACCESS_KEY_ID": "test_key_id",
        "PROTECTOR_ACCESS_SECRET_KEY": "test_secret_key",
    },
)
@patch("voraus_pipeline_utils.cli.protect.protect_wheel_wrapper")
def test_protect_wheel_with_endpoint_from_env(protect_wheel_wrapper_mock: MagicMock, cli_runner: CliRunner) -> None:
    result = cli_runner.invoke(app=app, args=["protect", "wheel", "/path/to/wheel.whl", "--identifier=test-package-1"])
    assert result.exit_code == 0

    protect_wheel_wrapper_mock.assert_called_once_with(
        wheel_file=Path("/path/to/wheel.whl"),
        identifier="test-package-1",
        api_endpoint="https://env-endpoint.example.com",
        access_key_id="test_key_id",
        access_secret_key="test_secret_key",  # noqa: S106
        replace=True,
    )
