from __future__ import annotations

from ebay_client.core.http.transport import ApiTransport
from ebay_client.generated.notification.models import (
    CreateSubscriptionRequest,
    DestinationRequest,
    DestinationSearchResponse,
    PublicKey,
    SubscriptionSearchResponse,
    Topic,
    TopicSearchResponse,
)

NOTIFICATION_SCOPE = "https://api.ebay.com/oauth/api_scope"
NOTIFICATION_SUBSCRIPTION_SCOPE = "https://api.ebay.com/oauth/api_scope/commerce.notification.subscription"
NOTIFICATION_SUBSCRIPTION_READ_SCOPE = "https://api.ebay.com/oauth/api_scope/commerce.notification.subscription.readonly"


class NotificationClient:
    def __init__(self, transport: ApiTransport) -> None:
        self.transport = transport

    def get_topics(self, *, limit: int | None = None, continuation_token: str | None = None) -> TopicSearchResponse:
        return self.transport.request_model(
            TopicSearchResponse,
            "GET",
            "/commerce/notification/v1/topic",
            scopes=[NOTIFICATION_SCOPE],
            params={"limit": limit, "continuation_token": continuation_token},
        )

    def get_topic(self, topic_id: str) -> Topic:
        return self.transport.request_model(
            Topic,
            "GET",
            f"/commerce/notification/v1/topic/{topic_id}",
            scopes=[NOTIFICATION_SCOPE],
        )

    def get_destinations(self, *, limit: int | None = None, continuation_token: str | None = None) -> DestinationSearchResponse:
        return self.transport.request_model(
            DestinationSearchResponse,
            "GET",
            "/commerce/notification/v1/destination",
            scopes=[NOTIFICATION_SCOPE],
            params={"limit": limit, "continuation_token": continuation_token},
        )

    def create_destination(self, payload: DestinationRequest) -> dict[str, object] | None:
        return self.transport.request_json(
            "POST",
            "/commerce/notification/v1/destination",
            scopes=[NOTIFICATION_SCOPE],
            headers={"Content-Type": "application/json"},
            json_body=payload.model_dump(by_alias=True, exclude_none=True),
        )

    def get_subscriptions(self, *, limit: int | None = None, continuation_token: str | None = None) -> SubscriptionSearchResponse:
        return self.transport.request_model(
            SubscriptionSearchResponse,
            "GET",
            "/commerce/notification/v1/subscription",
            scopes=[NOTIFICATION_SCOPE, NOTIFICATION_SUBSCRIPTION_READ_SCOPE],
            params={"limit": limit, "continuation_token": continuation_token},
        )

    def create_subscription(self, payload: CreateSubscriptionRequest) -> dict[str, object] | None:
        return self.transport.request_json(
            "POST",
            "/commerce/notification/v1/subscription",
            scopes=[NOTIFICATION_SCOPE, NOTIFICATION_SUBSCRIPTION_SCOPE],
            headers={"Content-Type": "application/json"},
            json_body=payload.model_dump(by_alias=True, exclude_none=True),
        )

    def test_subscription(self, subscription_id: str) -> None:
        self.transport.request_json(
            "POST",
            f"/commerce/notification/v1/subscription/{subscription_id}/test",
            scopes=[NOTIFICATION_SCOPE, NOTIFICATION_SUBSCRIPTION_SCOPE],
        )

    def get_public_key(self, public_key_id: str) -> PublicKey:
        return self.transport.request_model(
            PublicKey,
            "GET",
            f"/commerce/notification/v1/public_key/{public_key_id}",
            scopes=[NOTIFICATION_SCOPE],
        )
