# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from datetime import datetime, timezone
from email.utils import format_datetime
from urllib.parse import quote, urlencode

import httpx
from generated.example.conflict import Conflict
from generated.example.item_details import ItemDetails
from generated.example.service_unavailable import ServiceUnavailable
from generated.example.shelf_item_output import ShelfItemOutput
from generated.example.shelf_sku_output import ShelfSkuOutput
from generated.warehouse_api_client.warehouse_api.client.client_response import parse_client_response
from generated.warehouse_api_client.warehouse_api.client.operation_bindings import OPERATION_HTTP_BINDINGS
from pydantic import Field  # noqa: F401


def _encode_path_label(value: object, *, greedy: bool = False) -> str:
    return quote(str(value), safe="/" if greedy else "")


def _serialize_query_value(value: object, *, timestamp_format: str = "date-time") -> str:
    if isinstance(value, bool):
        return str(value).lower()
    if isinstance(value, datetime):
        if timestamp_format == "epoch-seconds":
            return str(value.timestamp())
        if timestamp_format == "http-date":
            if value.tzinfo is None:
                value = value.replace(tzinfo=timezone.utc)
            return format_datetime(value.astimezone(timezone.utc), usegmt=True)
        return value.isoformat()
    return str(value)


class WarehouseApiClient:
    def __init__(self, client: httpx.Client, base_url: str) -> None:
        self._client = client
        self._base_url = base_url.rstrip("/")

    def assign_shelf_sku(
        self,
        request_id: str | None,
        shelf_id: str,
        tenant: str,
        preview: bool | None,
        tags: list[str] | None,
        modified_since: datetime | None,
        windows: list[datetime] | None,
        sku: str,
    ) -> ShelfSkuOutput | ServiceUnavailable:
        """POST with @httpHeader, one @httpLabel, and a primitive payload body member."""
        headers: dict[str, str] = {}
        if request_id is not None:
            headers["X-Request-Id"] = str(request_id)
        _encoded_shelf_id = _encode_path_label(shelf_id)
        query_params: list[tuple[str, str]] = []
        query_params.append(("tenant", _serialize_query_value(tenant)))
        if preview is not None:
            query_params.append(("preview", _serialize_query_value(preview)))
        if tags is not None:
            for value in tags:
                query_params.append(("tag", _serialize_query_value(value)))
        if modified_since is not None:
            query_params.append(
                ("if-modified-since", _serialize_query_value(modified_since, timestamp_format="http-date"))
            )
        if windows is not None:
            for value in windows:
                query_params.append(("window", _serialize_query_value(value, timestamp_format="http-date")))
        query_string = urlencode(query_params, quote_via=quote)
        request_url = f"{self._base_url}/shelves/{_encoded_shelf_id}/skus"
        if query_string:
            request_url = f"{request_url}?{query_string}"
        response = self._client.request(
            "POST",
            request_url,
            headers=headers,
            json=sku,
        )
        return parse_client_response(
            response,
            OPERATION_HTTP_BINDINGS["assign_shelf_sku"],
        )

    def create_shelf_item(
        self,
        content_type: str | None,
        idempotency_key: str | None,
        warehouse_id: str,
        shelf_id: str,
        details: ItemDetails,
    ) -> ShelfItemOutput | Conflict | ServiceUnavailable:
        """POST with @httpHeader, @httpLabel path labels, and a nested structure @httpPayload body."""
        headers: dict[str, str] = {}
        if content_type is not None:
            headers["content-type"] = str(content_type)
        if idempotency_key is not None:
            headers["X-Idempotency-Key"] = str(idempotency_key)
        _encoded_warehouse_id = _encode_path_label(warehouse_id)
        _encoded_shelf_id = _encode_path_label(shelf_id)
        request_url = f"{self._base_url}/warehouses/{_encoded_warehouse_id}/shelves/{_encoded_shelf_id}/items"
        response = self._client.request(
            "POST",
            request_url,
            headers=headers,
            json=details.model_dump(mode="json", exclude_none=True),
        )
        return parse_client_response(
            response,
            OPERATION_HTTP_BINDINGS["create_shelf_item"],
        )
