# Copyright 2025 Criptomart
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from datetime import date
from datetime import timedelta

from odoo.tests.common import TransactionCase
from odoo.tests.common import tagged


@tagged("post_install", "-at_install")
class TestTemplatesRendering(TransactionCase):
    """Test suite to verify QWeb templates work with day_names context.

    This test covers the fix for the issue where _() function calls
    in QWeb t-value attributes caused TypeError: 'NoneType' object is not callable.
    The fix moves day_names definition to Python controller and passes it as context.
    """

    def setUp(self):
        """Set up test data: create a test group order."""
        super().setUp()

        # Create a test supplier
        self.supplier = self.env["res.partner"].create(
            {
                "name": "Test Supplier",
                "is_company": True,
            }
        )

        # Create test products
        self.product = self.env["product.product"].create(
            {
                "name": "Test Product",
                "type": "consu",  # consumable (consu), service, or storable
            }
        )

        # Create a test group
        self.group = self.env["res.partner"].create(
            {
                "name": "Test Group",
                "is_company": True,
            }
        )

        # Create a group order
        self.group_order = self.env["group.order"].create(
            {
                "name": "Test Order",
                "state": "open",
                "supplier_ids": [(6, 0, [self.supplier.id])],
                "product_ids": [(6, 0, [self.product.id])],
                "group_ids": [(6, 0, [self.group.id])],
                "start_date": date.today(),
                "end_date": date.today() + timedelta(days=7),
                "pickup_day": "5",  # Saturday
                "cutoff_day": "3",  # Thursday
            }
        )

    def test_eskaera_page_template_exists(self):
        """Test that eskaera_page template compiles without errors."""
        template = self.env.ref("website_sale_aplicoop.eskaera_page")
        self.assertIsNotNone(template)
        self.assertEqual(template.type, "qweb")

    def test_eskaera_shop_template_exists(self):
        """Test that eskaera_shop template compiles without errors."""
        template = self.env.ref("website_sale_aplicoop.eskaera_shop")
        self.assertIsNotNone(template)
        self.assertEqual(template.type, "qweb")

    def test_eskaera_checkout_template_exists(self):
        """Test that eskaera_checkout template compiles without errors."""
        template = self.env.ref("website_sale_aplicoop.eskaera_checkout")
        self.assertIsNotNone(template)
        self.assertEqual(template.type, "qweb")

    def test_day_names_context_is_provided(self):
        """Test that day_names context is provided by the controller method."""
        # Simulate what the controller does, passing env for test context
        from ..controllers.website_sale import AplicoopWebsiteSale

        controller = AplicoopWebsiteSale()
        day_names = controller._get_day_names(env=self.env)

        # Verify we have exactly 7 days
        self.assertEqual(len(day_names), 7)

        # Verify all are strings and not None
        for i, day_name in enumerate(day_names):
            self.assertIsNotNone(day_name, f"Day at index {i} is None")
            self.assertIsInstance(day_name, str, f"Day at index {i} is not a string")
            self.assertGreater(len(day_name), 0, f"Day at index {i} is empty string")

    def test_day_names_not_using_inline_underscore(self):
        """Test that day_names are defined in Python, not in t-value attributes.

        This test ensures the fix has been applied:
        - day_names MUST be passed from controller context
        - day_names MUST NOT be defined with _() inside t-value attributes
        - Templates use day_names[index] from context, not t-set with _()
        """
        template = self.env.ref("website_sale_aplicoop.eskaera_page")
        # Read the template source to verify it doesn't have inline _() in t-value
        self.assertIn(
            "day_names",
            template.arch_db,
            "Template must reference day_names from context",
        )
        # The fix ensures no <t t-set="day_names" t-value="[_(...)]"/> exists
        # which was causing the NoneType error

    def test_eskaera_checkout_summary_template_exists(self):
        """Test that eskaera_checkout_summary sub-template exists."""
        template = self.env.ref("website_sale_aplicoop.eskaera_checkout_summary")
        self.assertIsNotNone(template)
        self.assertEqual(template.type, "qweb")
        # Verify it has the expected structure
        self.assertIn(
            "checkout-summary-table",
            template.arch_db,
            "Template must have checkout-summary-table id",
        )
        self.assertIn(
            "Product",
            template.arch_db,
            "Template must have Product label for translation",
        )
        self.assertIn(
            "Quantity",
            template.arch_db,
            "Template must have Quantity label for translation",
        )
        self.assertIn(
            "Price", template.arch_db, "Template must have Price label for translation"
        )
        self.assertIn(
            "Subtotal",
            template.arch_db,
            "Template must have Subtotal label for translation",
        )

    def test_eskaera_checkout_summary_renders(self):
        """Test that eskaera_checkout_summary renders without errors."""
        template = self.env.ref("website_sale_aplicoop.eskaera_checkout_summary")
        # Render the template with empty context
        html = template._render_template(template.xml_id, {})
        # Should contain the basic table structure
        self.assertIn("<table", html)
        self.assertIn("checkout-summary-table", html)
        self.assertIn("Product", html)
        self.assertIn("Quantity", html)
        self.assertIn("This order's cart is empty", html)
