"""HTTP tests for GET /api/widget/templates.

The templates endpoint serves the SSR shells that the widget JS clones
for every interactive component (faceted filter, apply modal,
autocomplete). It MUST stay shape-compatible — the widget JS uses
`[data-kj-tpl="..."]` selectors that the templates must continue to
expose.
"""

import json

from odoo.tests import tagged

from odoo.addons.kj_affiliate_widget.tests.common import WidgetTestCommon


@tagged("post_install", "-at_install", "kj_affiliate_widget")
class TestWidgetTemplates(WidgetTestCommon):

    def test_returns_three_named_templates(self):
        r = self.url_open("/api/widget/templates")
        self.assertEqual(r.status_code, 200)
        body = r.json()
        for key in ("faceted_filters", "apply_modal", "autocomplete"):
            self.assertIn(key, body, f"missing template {key!r}")
            self.assertTrue(body[key], f"empty template {key!r}")

    def test_faceted_filter_template_has_clone_slots(self):
        body = self.url_open("/api/widget/templates").json()
        html = body["faceted_filters"]
        # Widget JS clones these — each must appear in the template.
        for slot in ('data-kj-tpl="section"',
                     'data-kj-tpl="row"',
                     'data-kj-tpl="chip"',
                     'data-kj-tpl="clear-all"'):
            self.assertIn(slot, html, f"faceted filter missing {slot}")
        # And the chip-bar shell selectors widget JS queries:
        for sel in ('data-kj-fc="root"',
                    'data-kj-fc-toggle',
                    'data-kj-fc-chips',
                    'data-kj-fc-count',
                    'data-kj-fc-panel',
                    'data-kj-fc-grid'):
            self.assertIn(sel, html, f"faceted filter missing {sel}")

    def test_apply_modal_template_has_form_anchors(self):
        body = self.url_open("/api/widget/templates").json()
        html = body["apply_modal"]
        for anchor in ('data-kj-ap="root"',
                       'data-kj-ap-form',
                       'data-kj-ap-submit',
                       'data-kj-ap-cancel',
                       'data-kj-ap-close',
                       'data-kj-ap-status',
                       'data-kj-ap-position',
                       'data-kj-ap-job-name'):
            self.assertIn(anchor, html, f"apply modal missing {anchor}")

    def test_autocomplete_template_uses_odoo_classes(self):
        """Sharing Odoo's class names lets KJ's `search_autocomplete.scss`
        style the widget dropdown — pixel parity with the origin without
        a second copy of the CSS."""
        body = self.url_open("/api/widget/templates").json()
        html = body["autocomplete"]
        for cls in ("o_dropdown_menu",
                    "o_search_result_item",
                    "o_image_64_contain",
                    "fa-briefcase",
                    "Alle Ergebnisse",
                    'data-kj-tpl="ac-row"'):
            self.assertIn(cls, html, f"autocomplete missing {cls}")

    def test_autocomplete_dropdown_not_pre_shown(self):
        """The autocomplete dropdown must NOT ship the Bootstrap `show`
        class. `.show.o_dropdown_menu` forces display:block, which
        pinned the dropdown open with an empty list — "Alle Ergebnisse"
        was permanently visible under every search box. Visibility is
        owned by the widget's own `is-open` toggle.
        """
        html = self.url_open("/api/widget/templates").json()["autocomplete"]
        self.assertNotIn(
            "o_dropdown_menu show", html,
            "autocomplete dropdown ships the `show` class — it will "
            "render open by default. Remove `show` from templates_ssr.xml.",
        )

    def test_long_cache_control(self):
        """Templates change rarely → long browser TTL."""
        r = self.url_open("/api/widget/templates")
        self.assertIn("max-age=300", r.headers.get("Cache-Control", ""))
