import requests


def load_all_products(client_id: str, api_key: str):
    headers = {
        "Client-Id": client_id,
        "Api-Key": api_key
    }

    list_url = "https://api-seller.ozon.ru/v3/product/list"
    details_url = "https://api-seller.ozon.ru/v4/product/info/attributes"
    result, last_id = [], ""

    while True:
        response = requests.post(list_url, headers=headers, json={
            "filter": {"visibility": "ALL"},
            "last_id": last_id,
            "limit": 100
        })

        response.raise_for_status()
        data = response.json()

        product_ids = [item["product_id"] for item in data["result"]["items"]]
        if not product_ids:
            break

        details_response = requests.post(details_url, headers=headers, json={
            "filter": {"product_id": product_ids, "visibility": "ALL"},
            "limit": 100,
            "sort_dir": "ASC"
        })

        details_response.raise_for_status()
        details_data = details_response.json()

        for product in details_data["result"]:
            attributes_by_id = {
                attribute["id"]: attribute
                for attribute in product["attributes"]
            }

            brands = attributes_by_id.get(5076)
            colors = attributes_by_id.get(10096)
            compositions = attributes_by_id.get(6733)

            result.append({
                "name": product.get("name"),
                "article": str(product["sku"]),
                "barcodes": product["barcodes"],
                "brand": ", ".join(brand["value"] for brand in brands['values']) if brands else None,
                "color": ", ".join(color["value"] for color in colors['values']) if colors else None,
                "composition": ", ".join(color["value"] for color in compositions['values']) if compositions else None,
                "imageUrl": product.get("primary_image")
            })

        if not data["result"].get("last_id"):
            break

        last_id = data["result"]["last_id"]

    return result
