"""Shopify's ProductGroup JSON-LD schema, used by many current themes for
products with variants. Verified against a real page (cordori.com.au):
name/description/brand/category sit at the same top level a plain Product
uses, but price/currency/availability live one level down, in the first
entry of hasVariant[].offers, since the group itself has none."""
from app.services.catalog.crawl import to_source_product
from app.services.content.product_extraction import extract_from_jsonld

REF = "shop.example.com"

# Trimmed from the real block fetched off cordori.com.au/products/nimbus-wool-bomber.
PRODUCT_GROUP_PAGE = {
    "url": "https://shop.example.com/products/nimbus-wool-bomber",
    "title": "Nimbus Wool Bomber",
    "jsonld": [{
        "@context": "http://schema.org/",
        "@type": "ProductGroup",
        "name": "Nimbus Wool Bomber",
        "description": "A 100% wool bomber with a tactile surface.",
        "brand": {"@type": "Brand", "name": "Cordori"},
        "category": "Bomber Jackets",
        "hasVariant": [
            {
                "@type": "Product",
                "name": "Nimbus Wool Bomber - Cocoa Brown / S",
                "image": "https://shop.example.com/cdn/nimbus.png",
                "offers": {
                    "@type": "Offer",
                    "price": "649.00",
                    "priceCurrency": "AUD",
                    "availability": "http://schema.org/InStock",
                },
            },
            {
                "@type": "Product",
                "name": "Nimbus Wool Bomber - Cocoa Brown / M",
                "offers": {"price": "649.00", "priceCurrency": "AUD"},
            },
        ],
    }],
}


def test_extract_from_jsonld_recognises_a_productgroup():
    (product,) = extract_from_jsonld(PRODUCT_GROUP_PAGE)
    assert product["name"] == "Nimbus Wool Bomber"
    assert product["description"] == "A 100% wool bomber with a tactile surface."
    assert product["category"] == "Bomber Jackets"


def test_crawled_productgroup_gets_brand_price_and_currency():
    (product,) = extract_from_jsonld(PRODUCT_GROUP_PAGE)
    sp = to_source_product(product, REF)

    assert sp["brand"] == "Cordori"
    assert sp["variants"][0]["price"] == 649.00
    assert sp["currency"] == "AUD"
    assert sp["variants"][0]["available"] is True
    assert sp["product_type"] == "Bomber Jackets"


def test_a_productgroup_with_no_variants_still_yields_a_product_with_no_price():
    page = {
        "url": "https://shop.example.com/products/bare",
        "jsonld": [{"@type": "ProductGroup", "name": "Bare Group",
                    "brand": {"name": "Acme"}, "hasVariant": []}],
    }
    (product,) = extract_from_jsonld(page)
    sp = to_source_product(product, REF)

    assert sp["brand"] == "Acme"
    assert sp["variants"][0]["price"] is None


def test_a_plain_product_type_is_still_handled_unchanged():
    page = {
        "url": "https://shop.example.com/products/shirt",
        "jsonld": [{"@type": "Product", "name": "Oxford Shirt",
                    "brand": "Brookfield",
                    "offers": {"price": "59.00", "priceCurrency": "USD"}}],
    }
    (product,) = extract_from_jsonld(page)
    sp = to_source_product(product, REF)

    assert sp["brand"] == "Brookfield"
    assert sp["variants"][0]["price"] == 59.00
    assert sp["currency"] == "USD"


def test_an_unrelated_jsonld_type_is_still_ignored():
    page = {
        "url": "https://shop.example.com/",
        "jsonld": [{"@type": "Organization", "name": "Shop Example"}],
    }
    assert extract_from_jsonld(page) == []
