from __future__ import annotations

import sys
import unittest
from pathlib import Path

SITE_ROOT = Path(__file__).resolve().parents[1]
REPO_ROOT = SITE_ROOT.parents[1]
PIPELINE_ROOT = REPO_ROOT / "cloudflare-pipeline"
if str(PIPELINE_ROOT) not in sys.path:
    sys.path.insert(0, str(PIPELINE_ROOT))

from static_pages.models import ProductRecord  # noqa: E402
from static_pages.rendering import build_meta_description, build_page_title  # noqa: E402


def make_record(**overrides) -> ProductRecord:
    data = {
        "sku": "R577F23007",
        "slug": "r577f23007",
        "manufacturer": "Radiall USA, Inc.",
        "description": "DPDT RAMSES SMA 26.5GHZ FAILSAFE",
        "stock": 10,
        "spq": 1,
        "moq": "1",
        "series": "-",
        "status": "生産継続中",
        "lead_time": "お問合せ",
        "category": "RF Switch",
        "specs_basic": {},
        "specs_param": {},
        "specs_env": {},
        "specs_other": {},
        "stock_is_estimated": False,
    }
    data.update(overrides)
    return ProductRecord(**data)


class TitleMetaStrategyTestCase(unittest.TestCase):
    def test_title_uses_short_manufacturer_and_explicit_stock(self) -> None:
        record = make_record()

        title = build_page_title(record)

        self.assertEqual(title, "R577F23007 | Radiall 在庫10個・即納見積 - Inotoday")
        self.assertLessEqual(len(title), 60)

    def test_title_hides_estimated_stock(self) -> None:
        record = make_record(
            sku="FX5-CNV-IFC",
            slug="fx5-cnv-ifc",
            manufacturer="Mitsubishi Electric",
            description="FX5UC用コネクタ変換ユニット",
            stock=10,
            stock_is_estimated=True,
        )

        title = build_page_title(record)

        self.assertEqual(title, "FX5-CNV-IFC | 三菱電機 在庫確認・見積り - Inotoday")
        self.assertNotIn("在庫10個", title)

    def test_long_manufacturer_title_is_compressed(self) -> None:
        record = make_record(
            sku="0022E0111-9",
            slug="0022e0111-9",
            manufacturer="TE Connectivity Raychem Cable Protection",
            description="Industrial cable protection component",
            category="Cable Protection",
            specs_param={"RoHS Status": "RoHS Compliant"},
        )

        title = build_page_title(record)

        self.assertIn("TE Connectivity", title)
        self.assertNotIn("Raychem", title)
        self.assertLessEqual(len(title), 60)

    def test_meta_description_uses_product_context_without_old_sales_suffix(self) -> None:
        record = make_record(
            specs_param={"Package / Case": "SMA", "RoHS Status": "RoHS Compliant"},
        )

        meta = build_meta_description(record)

        self.assertIn("Radiall R577F23007 在庫10点。", meta)
        self.assertIn("DPDT RAMSES SMA 26.5GHz", meta)
        self.assertIn("価格・納期確認", meta)
        self.assertNotIn("お見積もりは24時間以内にお届け", meta)
        self.assertNotIn("柔軟な返品ポリシー", meta)
        self.assertLessEqual(len(meta), 160)

    def test_meta_description_hides_estimated_stock_and_uses_fallback_cta(self) -> None:
        record = make_record(
            sku="FX5-CNV-IFC",
            slug="fx5-cnv-ifc",
            manufacturer="Mitsubishi Electric",
            description="FX5UC用コネクタ変換ユニット",
            stock=10,
            stock_is_estimated=True,
        )

        meta = build_meta_description(record)

        self.assertTrue(meta.startswith("三菱電機 FX5-CNV-IFC の在庫・納期・価格を確認。"))
        self.assertIn("FX5UC用コネクタ変換ユニット", meta)
        self.assertNotIn("在庫10点", meta)
        self.assertLessEqual(len(meta), 160)


if __name__ == "__main__":
    unittest.main()
