"""Tests for Magento async bulk API client helpers."""

from magento.magento_api import MagentoRestClient
from magento.oauth_client import MagentoOAuthClient


class _FakeOAuth:
    def __init__(self, rest_base_path="V1", base_url="https://example.com/rest"):
        self.rest_base_path = rest_base_path
        self.base_url = base_url
        self.calls = []

    def async_bulk_url(self, resource_path: str) -> str:
        # Reuse real helper logic via a thin MagentoOAuthClient
        client = MagentoOAuthClient(
            base_url=self.base_url,
            consumer_key="k",
            consumer_secret="s",
            access_token="t",
            access_token_secret="ts",
            rest_base_path=self.rest_base_path,
        )
        return client.async_bulk_url(resource_path)

    def _request_url(self, method, full_url, **kwargs):
        self.calls.append((method, full_url, kwargs.get("json")))
        return 200, {"bulk_uuid": "u-1", "request_items": [], "errors": False}, None

    def _request(self, method, path, json=None, params=None):
        self.calls.append((method, path, json))
        return 200, {"operations_list": []}, None


def test_async_bulk_url_without_store_code():
    client = MagentoOAuthClient(
        base_url="https://example.com/rest",
        consumer_key="k",
        consumer_secret="s",
        access_token="t",
        access_token_secret="ts",
        rest_base_path="V1",
    )
    assert client.async_bulk_url("products/bySku") == (
        "https://example.com/rest/async/bulk/V1/products/bySku"
    )


def test_async_bulk_url_with_store_code():
    client = MagentoOAuthClient(
        base_url="https://example.com/rest",
        consumer_key="k",
        consumer_secret="s",
        access_token="t",
        access_token_secret="ts",
        rest_base_path="us_eng/V1",
    )
    assert client.async_bulk_url("products") == (
        "https://example.com/rest/us_eng/async/bulk/V1/products"
    )


def test_put_products_bulk_async_posts_array_payload():
    oauth = _FakeOAuth()
    api = MagentoRestClient(oauth)
    status, body, err = api.put_products_bulk_async(
        [{"sku": "A", "name": "A"}, {"sku": "B", "name": "B"}]
    )
    assert status == 200
    assert body["bulk_uuid"] == "u-1"
    assert err is None
    method, url, payload = oauth.calls[0]
    assert method == "PUT"
    assert url.endswith("/async/bulk/V1/products/bySku")
    assert payload == [{"product": {"sku": "A", "name": "A"}}, {"product": {"sku": "B", "name": "B"}}]


def test_post_products_bulk_async_posts_array_payload():
    oauth = _FakeOAuth(rest_base_path="us_eng/V1")
    api = MagentoRestClient(oauth)
    status, body, err = api.post_products_bulk_async([{"sku": "N", "name": "New"}])
    assert status == 200
    method, url, payload = oauth.calls[0]
    assert method == "POST"
    assert url.endswith("/us_eng/async/bulk/V1/products")
    assert payload == [{"product": {"sku": "N", "name": "New"}}]


def test_get_bulk_detailed_status_uses_v1_path():
    oauth = _FakeOAuth()
    api = MagentoRestClient(oauth)
    api.get_bulk_detailed_status("abc-123")
    assert oauth.calls[0][:2] == ("GET", "bulk/abc-123/detailed-status")
