"""Product search/read operations for D365 CSU."""

from __future__ import annotations

from typing import Any

from d365_csu.client import CSUClient


def search_products(
    client: CSUClient,
    keyword: str,
    *,
    channel_id: int = 0,
    catalog_id: int = 0,
    top: int = 10,
    skip: int = 0,
) -> Any:
    """Search the product catalog by keyword."""
    body = {
        "productSearchCriteria": {
            "SearchCondition": keyword,
            "Context": {"ChannelId": channel_id, "CatalogId": catalog_id},
        },
        **client.build_query_result_settings(top, skip),
    }
    return client.post("Products/Search", body)


def get_product(client: CSUClient, product_id: int) -> Any:
    """Get a single product by record ID."""
    return client.get(f"Products({product_id})")


def get_product_price(client: CSUClient, product_id: int) -> Any:
    """Get the active price for a product."""
    body = client.build_query_result_settings(1, 0)
    return client.post(f"Products({product_id})/GetPrice", body)


def get_dimension_values(
    client: CSUClient,
    product_id: int,
    dimension_type: int = 1,
    *,
    top: int = 50,
    skip: int = 0,
) -> Any:
    """Get available dimension values for a product."""
    body = {
        "dimensionType": dimension_type,
        **client.build_query_result_settings(top, skip),
    }
    return client.post(f"Products({product_id})/GetDimensionValues", body)
