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

"""
Test suite for Phase 2 refactoring of eskaera_shop() method.

Tests for refactored eskaera_shop using extracted helpers:
- Usage of _resolve_pricelist() instead of inline 3-tier fallback
- Extracted category filtering logic
- Price calculation with pricelist
- Search and category filter functionality
"""

from datetime import datetime
from datetime import timedelta

from odoo.tests.common import TransactionCase


class TestEskaeraShopobjInit(TransactionCase):
    """Test eskaera_shop() initial validation and setup."""

    def setUp(self):
        super().setUp()
        self.pricelist = self.env["product.pricelist"].create(
            {
                "name": "Test Pricelist",
                "currency_id": self.env.company.currency_id.id,
            }
        )

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

        self.member = self.env["res.partner"].create(
            {
                "name": "Group Member",
                "email": "member@test.com",
            }
        )
        self.group.member_ids = [(4, self.member.id)]

        self.user = self.env["res.users"].create(
            {
                "name": "Test User",
                "login": "testuser@test.com",
                "email": "testuser@test.com",
                "partner_id": self.member.id,
            }
        )

        self.category = self.env["product.category"].create(
            {
                "name": "Test Category",
            }
        )

        self.product = self.env["product.product"].create(
            {
                "name": "Test Product",
                "type": "product",
                "list_price": 100.0,
                "categ_id": self.category.id,
            }
        )

        self.group_order = self.env["group.order"].create(
            {
                "name": "Test Order",
                "group_ids": [(4, self.group.id)],
                "start_date": datetime.now().date(),
                "end_date": datetime.now().date() + timedelta(days=7),
                "pickup_day": "3",
                "cutoff_day": "0",
                "state": "open",
                "category_ids": [(4, self.category.id)],
            }
        )

    def test_eskaera_shop_order_not_found(self):
        """Test that eskaera_shop redirects when order doesn't exist."""
        # Nonexistent order_id should redirect to /eskaera
        # Placeholder: will be tested via HttpCase with request.Client

    def test_eskaera_shop_order_not_open(self):
        """Test that eskaera_shop redirects when order is not open."""
        self.group_order.state = "confirmed"
        # Should redirect to /eskaera
        # Placeholder: will be tested via HttpCase with request.Client

    def test_eskaera_shop_uses_resolve_pricelist(self):
        """Test that eskaera_shop uses _resolve_pricelist() helper."""
        # Configure Aplicoop pricelist
        self.env["ir.config_parameter"].sudo().set_param(
            "website_sale_aplicoop.pricelist_id", str(self.pricelist.id)
        )

        # When eskaera_shop is called, should use _resolve_pricelist()
        # Placeholder: will verify via mock or direct method call


class TestEskaeraShopcategoryHierarchy(TransactionCase):
    """Test eskaera_shop category hierarchy building."""

    def setUp(self):
        super().setUp()
        self.parent_category = self.env["product.category"].create(
            {
                "name": "Parent Category",
            }
        )

        self.child_category = self.env["product.category"].create(
            {
                "name": "Child Category",
                "parent_id": self.parent_category.id,
            }
        )

        self.product1 = self.env["product.product"].create(
            {
                "name": "Product in Parent",
                "type": "product",
                "list_price": 100.0,
                "categ_id": self.parent_category.id,
            }
        )

        self.product2 = self.env["product.product"].create(
            {
                "name": "Product in Child",
                "type": "product",
                "list_price": 200.0,
                "categ_id": self.child_category.id,
            }
        )

    def test_category_hierarchy_includes_parents(self):
        """Test that available_categories includes parent categories."""
        # When products have categories, category hierarchy should include parents
        # Placeholder: verify category tree structure

    def test_category_filter_includes_descendants(self):
        """Test that category filter includes child categories."""
        # When filtering by parent category, should include products from children
        # Placeholder: verify filtered products


class TestEskaeraShopriceCalculation(TransactionCase):
    """Test eskaera_shop price calculation with pricelist."""

    def setUp(self):
        super().setUp()
        self.pricelist = self.env["product.pricelist"].create(
            {
                "name": "Test Pricelist",
                "currency_id": self.env.company.currency_id.id,
            }
        )

        self.category = self.env["product.category"].create(
            {
                "name": "Test Category",
            }
        )

        self.product_no_tax = self.env["product.product"].create(
            {
                "name": "Product No Tax",
                "type": "product",
                "list_price": 100.0,
                "categ_id": self.category.id,
                "taxes_id": False,
            }
        )

        # Create tax
        self.tax = self.env["account.tax"].create(
            {
                "name": "Test Tax",
                "type_tax_use": "sale",
                "amount": 21.0,
                "amount_type": "percent",
            }
        )

        self.product_with_tax = self.env["product.product"].create(
            {
                "name": "Product With Tax",
                "type": "product",
                "list_price": 100.0,
                "categ_id": self.category.id,
                "taxes_id": [(4, self.tax.id)],
            }
        )

    def test_price_calculation_uses_pricelist(self):
        """Test that product prices are calculated using configured pricelist."""
        # Configure Aplicoop pricelist
        self.env["ir.config_parameter"].sudo().set_param(
            "website_sale_aplicoop.pricelist_id", str(self.pricelist.id)
        )

        # When eskaera_shop renders, should calculate prices via pricelist
        # Placeholder: verify price_info dict populated

    def test_price_info_structure(self):
        """Test that product_price_info has correct structure."""
        # product_price_info should have: price, list_price, has_discounted_price, discount, tax_included
        # Placeholder: verify dict structure


class TestEskaeraShoosearch(TransactionCase):
    """Test eskaera_shop search functionality."""

    def setUp(self):
        super().setUp()
        self.category = self.env["product.category"].create(
            {
                "name": "Test Category",
            }
        )

        self.product1 = self.env["product.product"].create(
            {
                "name": "Apple Juice",
                "type": "product",
                "list_price": 10.0,
                "categ_id": self.category.id,
            }
        )

        self.product2 = self.env["product.product"].create(
            {
                "name": "Orange Juice",
                "type": "product",
                "list_price": 12.0,
                "categ_id": self.category.id,
                "description": "Fresh orange juice from Spain",
            }
        )

        self.product3 = self.env["product.product"].create(
            {
                "name": "Water",
                "type": "product",
                "list_price": 2.0,
                "categ_id": self.category.id,
            }
        )

        self.group_order = self.env["group.order"].create(
            {
                "name": "Test Order",
                "start_date": datetime.now().date(),
                "end_date": datetime.now().date() + timedelta(days=7),
                "pickup_day": "3",
                "cutoff_day": "0",
                "state": "open",
                "category_ids": [(4, self.category.id)],
            }
        )

    def test_search_filters_by_name(self):
        """Test that search query filters products by name."""
        # When search='apple', should return only Apple Juice
        # Placeholder: verify filtered products

    def test_search_filters_by_description(self):
        """Test that search query filters products by description."""
        # When search='spain', should return Orange Juice (matches description)
        # Placeholder: verify filtered products

    def test_search_case_insensitive(self):
        """Test that search is case insensitive."""
        # search='APPLE' should match 'Apple Juice'
        # Placeholder: verify filtered products

    def test_search_empty_returns_all(self):
        """Test that empty search returns all products."""
        # When search='', should return all products
        # Placeholder: verify all products returned
