import importlib.util
from pathlib import Path
import sys
from types import ModuleType
import unittest

PLUGIN_ROOT = Path(__file__).resolve().parents[1]
ENTRYPOINT_PATH = PLUGIN_ROOT / "entrypoint.py"


def load_entrypoint_module():
    fake_fender = ModuleType("ocyan.core.fender")
    fake_fender.config = ModuleType("config")
    fake_fender.config.is_webshop = False

    fake_plugins = ModuleType("ocyan.core.plugins")

    class FakeOcyanPluginConfig:
        pass

    fake_plugins.OcyanPluginConfig = FakeOcyanPluginConfig

    fake_utils = ModuleType("ocyan.core.utils")
    fake_utils.merge_dicts = lambda left, right, overwrite=False: {**left, **right}

    sys.modules["ocyan.core.fender"] = fake_fender
    sys.modules["ocyan.core.plugins"] = fake_plugins
    sys.modules["ocyan.core.utils"] = fake_utils

    spec = importlib.util.spec_from_file_location(
        "carbasa_entrypoint_for_tests", ENTRYPOINT_PATH
    )
    module = importlib.util.module_from_spec(spec)
    assert spec and spec.loader
    spec.loader.exec_module(module)
    return module


class CarbasaSmokeTests(unittest.TestCase):
    def test_entrypoint_is_available(self):
        entrypoint = load_entrypoint_module()
        CarbasaConfig = entrypoint.CarbasaConfig
        self.assertEqual(CarbasaConfig.__name__, "CarbasaConfig")

    def test_tabs_template_uses_unique_ids(self):
        template = (
            PLUGIN_ROOT / "templates" / "carbasa" / "blocks" / "tabsblock.html"
        ).read_text()
        self.assertNotIn('id="pills-home-tab"', template)
        self.assertIn('aria-controls="{{ tab_id }}"', template)
        self.assertIn('aria-labelledby="{{ tab_button_id }}"', template)

    def test_accordion_template_uses_button_control(self):
        template = (
            PLUGIN_ROOT
            / "templates"
            / "coyote"
            / "blocks"
            / "content"
            / "accordion.html"
        ).read_text()
        self.assertIn("<button", template)
        self.assertIn('class="accordion-button', template)
        self.assertNotIn('<p class="accordion-header', template)

    def test_dropdown_template_uses_button_toggle(self):
        template = (
            PLUGIN_ROOT / "templates" / "partials" / "dropdown.html"
        ).read_text()
        self.assertIn("<button", template)
        self.assertNotIn('<a class="nav-link toggle"', template)

    def test_newsletter_block_has_no_inline_style(self):
        template = (
            PLUGIN_ROOT
            / "templates"
            / "roadrunner_bs4"
            / "blocks"
            / "newsletter_block.html"
        ).read_text()
        self.assertNotIn("<style", template)

    def test_layout_still_loads_runtime_carbasa_javascript(self):
        template = (PLUGIN_ROOT / "templates" / "layout.html").read_text()
        self.assertIn("carbasa/js/carbasa.js", template)

    def test_readme_documents_runtime_javascript_path(self):
        readme = (PLUGIN_ROOT.parents[2] / "README.md").read_text()
        self.assertIn("static/carbasa/js/carbasa.js", readme)

    def test_readme_documents_legacy_config_typo_key(self):
        readme = (PLUGIN_ROOT.parents[2] / "README.md").read_text()
        self.assertIn("same_hight_product_image", readme)


if __name__ == "__main__":
    unittest.main()
