"""Payments credentials domain — sync and async mixins."""

from __future__ import annotations

from typing import Any

from ...exceptions import api_error_handler, async_api_error_handler
from ..._api.generated.payments.payments__api__payments.models import (
    PaginatedProviderCredentialListList,
    ProviderCredentialCreateRequest,
    ProviderCredentialDetail,
    ProviderCredentialTestResponse,
    ProviderCredentialTestResponseRequest,
)


class _CredentialsMixin:
    """Sync credentials methods."""

    _api: Any

    @api_error_handler
    def list_credentials(self, *, page: int = 1, page_size: int = 20) -> PaginatedProviderCredentialListList:
        """List provider credentials."""
        return self._api.credentials_list(page=page, page_size=page_size)

    @api_error_handler
    def get_credential(self, credential_id: str) -> ProviderCredentialDetail:
        """Get credential details."""
        return self._api.credentials_retrieve(credential_id)

    @api_error_handler
    def create_credential(
        self,
        *,
        provider_type: str = "nowpayments",
        name: str,
        credential_key: str,
        is_sandbox: bool = False,
        is_default: bool = True,
        metadata: dict[str, Any] | None = None,
    ) -> ProviderCredentialDetail:
        """Create a new provider credential."""
        request = ProviderCredentialCreateRequest(
            provider_type=provider_type,  # type: ignore
            name=name,
            credential_key=credential_key,
            is_sandbox=is_sandbox,
            is_default=is_default,
            metadata=metadata,
        )
        return self._api.credentials_create(request)

    @api_error_handler
    def test_credential(self, credential_id: str) -> ProviderCredentialTestResponse:
        """Test credential connection with provider."""
        request = ProviderCredentialTestResponseRequest(success=True, message="test")
        return self._api.credentials_test_create(credential_id, request)

    @api_error_handler
    def set_default_credential(self, credential_id: str) -> ProviderCredentialDetail:
        """Set credential as default for its provider."""
        return self._api.credentials_set_default_create(credential_id)

    @api_error_handler
    def delete_credential(self, credential_id: str) -> bool:
        """Deactivate a credential (soft delete)."""
        self._api.credentials_destroy(credential_id)
        return True


class _AsyncCredentialsMixin:
    """Async credentials methods."""

    _api: Any

    @async_api_error_handler
    async def list_credentials(self, *, page: int = 1, page_size: int = 20) -> PaginatedProviderCredentialListList:
        """List provider credentials."""
        return await self._api.credentials_list(page=page, page_size=page_size)

    @async_api_error_handler
    async def get_credential(self, credential_id: str) -> ProviderCredentialDetail:
        """Get credential details."""
        return await self._api.credentials_retrieve(credential_id)

    @async_api_error_handler
    async def create_credential(
        self,
        *,
        provider_type: str = "nowpayments",
        name: str,
        credential_key: str,
        is_sandbox: bool = False,
        is_default: bool = True,
        metadata: dict[str, Any] | None = None,
    ) -> ProviderCredentialDetail:
        """Create a new provider credential."""
        request = ProviderCredentialCreateRequest(
            provider_type=provider_type,  # type: ignore
            name=name,
            credential_key=credential_key,
            is_sandbox=is_sandbox,
            is_default=is_default,
            metadata=metadata,
        )
        return await self._api.credentials_create(request)

    @async_api_error_handler
    async def test_credential(self, credential_id: str) -> ProviderCredentialTestResponse:
        """Test credential connection with provider."""
        request = ProviderCredentialTestResponseRequest(success=True, message="test")
        return await self._api.credentials_test_create(credential_id, request)

    @async_api_error_handler
    async def set_default_credential(self, credential_id: str) -> ProviderCredentialDetail:
        """Set credential as default for its provider."""
        return await self._api.credentials_set_default_create(credential_id)

    @async_api_error_handler
    async def delete_credential(self, credential_id: str) -> bool:
        """Deactivate a credential (soft delete)."""
        await self._api.credentials_destroy(credential_id)
        return True
