"""Sales order operations for D365 CSU."""

from __future__ import annotations

from typing import Any

from d365_csu.client import CSUClient


def search_orders(
    client: CSUClient,
    *,
    customer_account: str | None = None,
    search_type: int = 0,
    top: int = 10,
    skip: int = 0,
) -> Any:
    """Search sales orders.

    Args:
        customer_account: Filter by customer account number.
        search_type: 0 = SalesOrder, 1 = SalesInvoice, etc.
    """
    criteria: dict[str, Any] = {"SearchType": search_type}
    if customer_account:
        criteria["CustomerAccountNumber"] = customer_account
    body = {
        "orderSearchCriteria": criteria,
        **client.build_query_result_settings(top, skip),
    }
    return client.post("SalesOrders/Search", body)


def get_order(client: CSUClient, order_id: str) -> Any:
    """Get a sales order by ID."""
    return client.get(f"SalesOrders('{order_id}')")
