from __future__ import annotations

from ebay_client.core.http.transport import ApiTransport
from ebay_client.generated.fulfillment.models import (
    Order,
    OrderSearchPagedCollection,
    ShippingFulfillment,
    ShippingFulfillmentPagedCollection,
)

FULFILLMENT_READ_SCOPE = "https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly"


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

    def get_order(self, order_id: str) -> Order:
        return self.transport.request_model(
            Order,
            "GET",
            f"/sell/fulfillment/v1/order/{order_id}",
            scopes=[FULFILLMENT_READ_SCOPE],
        )

    def get_orders(self, *, limit: int | None = None, offset: int | None = None) -> OrderSearchPagedCollection:
        return self.transport.request_model(
            OrderSearchPagedCollection,
            "GET",
            "/sell/fulfillment/v1/order",
            scopes=[FULFILLMENT_READ_SCOPE],
            params={"limit": limit, "offset": offset},
        )

    def get_shipping_fulfillments(self, order_id: str) -> ShippingFulfillmentPagedCollection:
        return self.transport.request_model(
            ShippingFulfillmentPagedCollection,
            "GET",
            f"/sell/fulfillment/v1/order/{order_id}/shipping_fulfillment",
            scopes=[FULFILLMENT_READ_SCOPE],
        )

    def get_shipping_fulfillment(self, order_id: str, fulfillment_id: str) -> ShippingFulfillment:
        return self.transport.request_model(
            ShippingFulfillment,
            "GET",
            f"/sell/fulfillment/v1/order/{order_id}/shipping_fulfillment/{fulfillment_id}",
            scopes=[FULFILLMENT_READ_SCOPE],
        )
