"""Parity contract between the OWL JobFacetedFilters component
(origin) and the widget's SSR faceted-filters template (widget).

The two implementations render the SAME visual UI from the SAME ES data
but live in two engines (OWL XML for origin, QWeb for the widget). Until
one is migrated to consume the other, we pin the **visible class set**
and **JS hook selectors** that both must expose so a designer's change
to one side fails CI loudly instead of silently diverging.

This test reads the source files (not the rendered output) so it stays
fast and catches changes at the template level.

When the two are eventually consolidated (G2 in the gap analysis), this
test can be removed.
"""

import os

from odoo.tests import TransactionCase, tagged


_OWL_XML = os.path.realpath(os.path.join(
    os.path.dirname(__file__), "..", "..",
    "kj_custom_website_theme", "static", "src", "xml",
    "job_faceted_filters.xml",
))
_SSR_XML = os.path.realpath(os.path.join(
    os.path.dirname(__file__), "..",
    "views", "templates_ssr.xml",
))

# Class names + attributes that must appear in BOTH templates. Order
# doesn't matter; missing any in either side fails the test. Add an
# entry here when you add a new shared chrome element; remove one when
# you intentionally deprecate it from both sides.
SHARED_CHIP_BAR_TOKENS = (
    "kj-faceted-filters",
    "kj-chip-bar-mode",
    "kj-chip-bar-wrapper",
    "kj-chip-bar",
    "kj-weitere-filter-btn",
    "kj-chip-scroll-container",
    "kj-chip-scroll-inner",
    "kj-results-count",
    "kj-dropdown-panels",
    "kj-dropdown-panel",
    "kj-all-filters-grid",
    "kj-facet-section",
)

# Selectors the widget JS clones — only relevant for the SSR side.
# Pinned separately so an OWL refactor never has to know about them.
SSR_ONLY_HOOKS = (
    'data-kj-fc="root"',
    "data-kj-fc-toggle",
    "data-kj-fc-chips",
    "data-kj-fc-count",
    "data-kj-fc-panel",
    "data-kj-fc-grid",
    'data-kj-tpl="section"',
    'data-kj-tpl="row"',
    'data-kj-tpl="chip"',
    'data-kj-tpl="clear-all"',
)


@tagged("post_install", "-at_install", "kj_affiliate_widget")
class TestOwlSsrParity(TransactionCase):

    def setUp(self):
        super().setUp()
        with open(_OWL_XML, encoding="utf-8") as f:
            self.owl_src = f.read()
        with open(_SSR_XML, encoding="utf-8") as f:
            self.ssr_src = f.read()

    def test_owl_contains_shared_chip_bar_tokens(self):
        missing = [t for t in SHARED_CHIP_BAR_TOKENS if t not in self.owl_src]
        self.assertFalse(
            missing,
            "OWL faceted_filters.xml is missing shared chip-bar tokens: "
            f"{missing!r}. Either restore them or remove them from "
            "SHARED_CHIP_BAR_TOKENS in this test (intentional deprecation).",
        )

    def test_ssr_contains_shared_chip_bar_tokens(self):
        missing = [t for t in SHARED_CHIP_BAR_TOKENS if t not in self.ssr_src]
        self.assertFalse(
            missing,
            "Widget templates_ssr.xml is missing shared chip-bar tokens: "
            f"{missing!r}. The widget SSR template diverged from origin.",
        )

    def test_ssr_contains_widget_js_hooks(self):
        missing = [h for h in SSR_ONLY_HOOKS if h not in self.ssr_src]
        self.assertFalse(
            missing,
            "Widget templates_ssr.xml is missing JS-hook selectors: "
            f"{missing!r}. kj_widget.js queries these — removing them "
            "breaks the chip-bar / panel / chip rendering.",
        )
