"""Reading Seoul (The Shilla Duty Free) off its server-rendered product page.

Pinned from the probe of 2026-09-05 (`.logs/runs/shilla-probe-2026-09-05.log`): the
page states three USD price tiers and only one is published; the sold-out block is
in every page's markup and means sold out only when it is shown; the barcode sits in
"REF.NO" beside a retailer SKU that is not one; and brand and name share one heading.
The fixture is the NARS setting powder page as the sidecar received it, facts only.
"""

from pathlib import Path

from app.services.collectors.registry import COLLECTORS
from app.services.collectors.shilla import (
    PRICE_TYPE,
    Shilla,
    follows_rail,
    in_stock_of,
    listing_from_page,
    price_tiers,
    product_code,
    product_urls_in,
)

FIXTURES = Path(__file__).parent / "fixtures"
PAGE = (FIXTURES / "shilla_product.html").read_text()
URL = "https://www.shilladfs.com/estore/kr/en/p/5581103"


class TestPriceTiers:
    def test_the_discount_tier_is_published_and_the_list_price_is_the_was_price(self):
        row = listing_from_page(PAGE, URL)
        assert (row.price, row.was_price, row.currency) == (25.37, 43.0, "USD")
        assert row.price_type == PRICE_TYPE == "discount"

    def test_all_three_tiers_are_kept_in_raw_and_the_member_one_is_never_the_price(self):
        assert price_tiers(PAGE) == {"list": 43.0, "discount": 25.37, "member": 25.37}
        page = PAGE.replace('class="pd-discount">25.37', 'class="pd-discount">20.00')
        row = listing_from_page(page, URL)
        assert row.price == 25.37 and row.raw["tiers"]["member"] == 20.0

    def test_a_page_without_a_price_is_skipped(self):
        page = PAGE.replace('id="prdPriceDollar" value="25.37"', 'id="prdPriceDollar" value=""')
        page = page.replace('<em class="pd-brand-cost">25.37</em>', "")
        assert listing_from_page(page, URL) is None


class TestIdentityAndStock:
    def test_the_ref_no_is_the_barcode_and_the_sku_no_is_not(self):
        row = listing_from_page(PAGE, URL)
        assert row.gtin == "0607845058946"
        assert row.raw["sku_no"] == "096311001883" and row.source_sku == "5581103"

    def test_brand_and_name_split_from_one_heading(self):
        row = listing_from_page(PAGE, URL)
        assert row.brand == "NARS"
        assert row.name == "NARS LIGHT REFLECTING SETTING POWDER-PRESSED (NEW)"

    def test_sold_out_means_shown_not_present(self):
        """The block is in every page's markup; on this in-stock product it is hidden."""
        assert in_stock_of(PAGE) is True
        shown = PAGE.replace('class="sold_out pd-soldout-sms-desc" style="display: none"', 'class="sold_out pd-soldout-sms-desc"')
        assert in_stock_of(shown) is False
        assert listing_from_page(shown, URL).in_stock is False

    def test_category_tree_and_vertical(self):
        row = listing_from_page(PAGE, URL)
        assert row.feed_categories == ["Makeup", "Base", "Powder/Compact Foundation"]
        assert row.vertical == "beauty"
        assert row.quantity_ml is None  # "10G" is a weight, not a volume
        assert (row.quantity.value, row.quantity.unit) == (10, "g")
        assert row.quantity_text == "10G"

    def test_a_volume_spec_row_yields_a_millilitre_quantity(self):
        page = PAGE.replace("<td>10G</td>", "<td>100ML</td>")
        row = listing_from_page(page, URL)
        assert row.quantity.unit == "ml"
        assert row.quantity_text == "100ML"

    def test_a_non_product_page_is_none(self):
        assert listing_from_page("<html><body class='pageType-CategoryPage'>x</body></html>", URL) is None


class TestWalk:
    def test_rails_are_followed_only_from_the_categories_we_collect(self):
        row = listing_from_page(PAGE, URL)
        assert follows_rail(row)  # Makeup
        row.feed_categories = ["Bags/Shoes/Fashion accessories"]
        assert not follows_rail(row)

    def test_product_links_are_absolute_and_de_duplicated(self):
        html = '<a href="/estore/kr/en/p/1"></a><a href="/estore/kr/en/p/2?x=1"></a><a href="/estore/kr/en/p/1"></a>'
        assert product_urls_in(html) == [
            "https://www.shilladfs.com/estore/kr/en/p/1",
            "https://www.shilladfs.com/estore/kr/en/p/2",
        ]
        assert product_code(URL) == "5581103"

    def test_registered(self):
        assert isinstance(COLLECTORS["shilla-icn"], Shilla)
