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

"""
Test suite for Phase 3 refactoring of confirm_eskaera().

Tests the 3 helper methods created in Phase 3:
- _validate_confirm_json(): Validates JSON request data
- _process_cart_items(): Processes cart items into sale.order lines
- _build_confirmation_message(): Builds localized confirmation messages

Includes tests for:
- Request validation with various error conditions
- Cart item processing with product context
- Multi-language message building (ES, EU, CA, GL, PT, FR, IT)
- Pickup vs delivery date handling
- Edge cases and error handling
"""

import json
from datetime import date
from datetime import timedelta
from unittest.mock import Mock
from unittest.mock import patch

from odoo import http
from odoo.tests.common import TransactionCase


class TestValidateConfirmJson(TransactionCase):
    """Test _validate_confirm_json() helper method."""

    def setUp(self):
        super().setUp()
        self.controller = http.request.env["website.sale"].browse([])
        self.user = self.env.ref("base.user_admin")
        self.partner = self.env.ref("base.partner_admin")

        # Create test group order
        self.group_order = self.env["group.order"].create(
            {
                "name": "Test Order Phase 3",
                "state": "open",
                "collection_date": date.today() + timedelta(days=3),
                "cutoff_day": "3",  # Thursday
                "pickup_day": "5",  # Saturday
            }
        )

    @patch("odoo.http.request")
    def test_validate_confirm_json_success(self, mock_request):
        """Test successful validation of confirm JSON data."""
        mock_request.env = self.env.with_user(self.user)

        data = {
            "order_id": self.group_order.id,
            "items": [{"product_id": 1, "quantity": 2, "product_price": 10.0}],
            "is_delivery": False,
        }

        order_id, group_order, current_user, items, is_delivery = (
            self.controller._validate_confirm_json(data)
        )

        self.assertEqual(order_id, self.group_order.id)
        self.assertEqual(group_order.id, self.group_order.id)
        self.assertEqual(current_user.id, self.user.id)
        self.assertEqual(len(items), 1)
        self.assertFalse(is_delivery)

    @patch("odoo.http.request")
    def test_validate_confirm_json_missing_order_id(self, mock_request):
        """Test validation fails without order_id."""
        mock_request.env = self.env.with_user(self.user)

        data = {"items": [{"product_id": 1, "quantity": 2}]}

        with self.assertRaises(ValueError) as context:
            self.controller._validate_confirm_json(data)

        self.assertIn("Missing order_id", str(context.exception))

    @patch("odoo.http.request")
    def test_validate_confirm_json_order_not_exists(self, mock_request):
        """Test validation fails with non-existent order."""
        mock_request.env = self.env.with_user(self.user)

        data = {
            "order_id": 99999,  # Non-existent ID
            "items": [{"product_id": 1, "quantity": 2}],
        }

        with self.assertRaises(ValueError) as context:
            self.controller._validate_confirm_json(data)

        self.assertIn("Order", str(context.exception))

    @patch("odoo.http.request")
    def test_validate_confirm_json_no_items(self, mock_request):
        """Test validation fails without items in cart."""
        mock_request.env = self.env.with_user(self.user)

        data = {
            "order_id": self.group_order.id,
            "items": [],
        }

        with self.assertRaises(ValueError) as context:
            self.controller._validate_confirm_json(data)

        self.assertIn("No items in cart", str(context.exception))

    @patch("odoo.http.request")
    def test_validate_confirm_json_with_delivery_flag(self, mock_request):
        """Test validation correctly handles is_delivery flag."""
        mock_request.env = self.env.with_user(self.user)

        data = {
            "order_id": self.group_order.id,
            "items": [{"product_id": 1, "quantity": 1}],
            "is_delivery": True,
        }

        _, _, _, _, is_delivery = self.controller._validate_confirm_json(data)

        self.assertTrue(is_delivery)


class TestProcessCartItems(TransactionCase):
    """Test _process_cart_items() helper method."""

    def setUp(self):
        super().setUp()
        self.controller = http.request.env["website.sale"].browse([])

        # Create test products
        self.product1 = self.env["product.product"].create(
            {
                "name": "Test Product 1",
                "list_price": 15.0,
                "type": "consu",
            }
        )
        self.product2 = self.env["product.product"].create(
            {
                "name": "Test Product 2",
                "list_price": 25.0,
                "type": "consu",
            }
        )

        # Create test group order
        self.group_order = self.env["group.order"].create(
            {
                "name": "Test Order for Cart",
                "state": "open",
            }
        )

    @patch("odoo.http.request")
    def test_process_cart_items_success(self, mock_request):
        """Test successful cart item processing."""
        mock_request.env = self.env
        mock_request.env.lang = "es_ES"

        items = [
            {
                "product_id": self.product1.id,
                "quantity": 2,
                "product_price": 15.0,
            },
            {
                "product_id": self.product2.id,
                "quantity": 1,
                "product_price": 25.0,
            },
        ]

        result = self.controller._process_cart_items(items, self.group_order)

        self.assertEqual(len(result), 2)
        self.assertEqual(result[0][0], 0)  # Command (0, 0, vals)
        self.assertEqual(result[0][1], 0)
        self.assertIn("product_id", result[0][2])
        self.assertEqual(result[0][2]["product_uom_qty"], 2)
        self.assertEqual(result[0][2]["price_unit"], 15.0)

    @patch("odoo.http.request")
    def test_process_cart_items_uses_list_price_fallback(self, mock_request):
        """Test cart processing uses list_price when product_price is 0."""
        mock_request.env = self.env
        mock_request.env.lang = "es_ES"

        items = [
            {
                "product_id": self.product1.id,
                "quantity": 1,
                "product_price": 0,  # Should fallback to list_price
            }
        ]

        result = self.controller._process_cart_items(items, self.group_order)

        self.assertEqual(len(result), 1)
        # Should use product.list_price as fallback
        self.assertEqual(result[0][2]["price_unit"], self.product1.list_price)

    @patch("odoo.http.request")
    def test_process_cart_items_skips_invalid_product(self, mock_request):
        """Test cart processing skips non-existent products."""
        mock_request.env = self.env
        mock_request.env.lang = "es_ES"

        items = [
            {
                "product_id": 99999,  # Non-existent
                "quantity": 1,
                "product_price": 10.0,
            },
            {
                "product_id": self.product1.id,
                "quantity": 2,
                "product_price": 15.0,
            },
        ]

        result = self.controller._process_cart_items(items, self.group_order)

        # Should only process the valid product
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0][2]["product_id"], self.product1.id)

    @patch("odoo.http.request")
    def test_process_cart_items_empty_after_filtering(self, mock_request):
        """Test cart processing raises error when no valid items remain."""
        mock_request.env = self.env
        mock_request.env.lang = "es_ES"

        items = [{"product_id": 99999, "quantity": 1, "product_price": 10.0}]

        with self.assertRaises(ValueError) as context:
            self.controller._process_cart_items(items, self.group_order)

        self.assertIn("No valid items", str(context.exception))

    @patch("odoo.http.request")
    def test_process_cart_items_translates_product_name(self, mock_request):
        """Test cart processing uses translated product names."""
        mock_request.env = self.env
        mock_request.env.lang = "eu_ES"  # Basque

        # Add translation for product name
        self.env["ir.translation"].create(
            {
                "type": "model",
                "name": "product.product,name",
                "module": "website_sale_aplicoop",
                "lang": "eu_ES",
                "res_id": self.product1.id,
                "src": "Test Product 1",
                "value": "Proba Produktua 1",
                "state": "translated",
            }
        )

        items = [
            {
                "product_id": self.product1.id,
                "quantity": 1,
                "product_price": 15.0,
            }
        ]

        result = self.controller._process_cart_items(items, self.group_order)

        # Product name should be in Basque context
        product_name = result[0][2]["name"]
        self.assertIsNotNone(product_name)
        # In real test, would be "Proba Produktua 1" but translation may not work in test


class TestBuildConfirmationMessage(TransactionCase):
    """Test _build_confirmation_message() helper method."""

    def setUp(self):
        super().setUp()
        self.controller = http.request.env["website.sale"].browse([])
        self.user = self.env.ref("base.user_admin")
        self.partner = self.env.ref("base.partner_admin")

        # Create test group order with dates
        pickup_date = date.today() + timedelta(days=5)
        delivery_date = pickup_date + timedelta(days=1)

        self.group_order = self.env["group.order"].create(
            {
                "name": "Test Order Messages",
                "state": "open",
                "pickup_day": "5",  # Saturday (0=Monday)
                "pickup_date": pickup_date,
                "delivery_date": delivery_date,
            }
        )

        # Create test sale order
        self.sale_order = self.env["sale.order"].create(
            {
                "partner_id": self.partner.id,
                "group_order_id": self.group_order.id,
            }
        )

    @patch("odoo.http.request")
    def test_build_confirmation_message_pickup(self, mock_request):
        """Test confirmation message for pickup (not delivery)."""
        mock_request.env = self.env.with_context(lang="es_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        self.assertIn("message", result)
        self.assertIn("pickup_day", result)
        self.assertIn("pickup_date", result)
        self.assertIn("pickup_day_index", result)

        # Should contain "Thank you" text (or translation)
        self.assertIn("Thank you", result["message"])

        # Should contain order reference
        self.assertIn(self.sale_order.name, result["message"])

        # Should have pickup day index
        self.assertEqual(result["pickup_day_index"], 5)

    @patch("odoo.http.request")
    def test_build_confirmation_message_delivery(self, mock_request):
        """Test confirmation message for home delivery."""
        mock_request.env = self.env.with_context(lang="es_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=True
        )

        self.assertIn("message", result)

        # Should contain "Delivery date" label (or translation)
        # and should use delivery_date, not pickup_date
        message = result["message"]
        self.assertIsNotNone(message)

        # Delivery day should be next day after pickup (Saturday -> Sunday)
        # pickup_day_index=5 (Saturday), delivery should be 6 (Sunday)
        # Note: _get_day_names would need to be mocked for exact day name

    @patch("odoo.http.request")
    def test_build_confirmation_message_no_dates(self, mock_request):
        """Test confirmation message when no pickup date is set."""
        mock_request.env = self.env.with_context(lang="es_ES")

        # Create order without dates
        group_order_no_dates = self.env["group.order"].create(
            {
                "name": "Order No Dates",
                "state": "open",
            }
        )

        sale_order_no_dates = self.env["sale.order"].create(
            {
                "partner_id": self.partner.id,
                "group_order_id": group_order_no_dates.id,
            }
        )

        result = self.controller._build_confirmation_message(
            sale_order_no_dates, group_order_no_dates, is_delivery=False
        )

        # Should still build message without dates
        self.assertIn("message", result)
        self.assertIn("Thank you", result["message"])

        # Date fields should be empty
        self.assertEqual(result["pickup_date"], "")

    @patch("odoo.http.request")
    def test_build_confirmation_message_formats_date(self, mock_request):
        """Test confirmation message formats dates correctly (DD/MM/YYYY)."""
        mock_request.env = self.env.with_context(lang="es_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        # Should have date in DD/MM/YYYY format
        pickup_date_str = result["pickup_date"]
        self.assertIsNotNone(pickup_date_str)

        # Verify format with regex

        date_pattern = r"\d{2}/\d{2}/\d{4}"
        self.assertRegex(pickup_date_str, date_pattern)

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_es(self, mock_request):
        """Test confirmation message in Spanish (es_ES)."""
        mock_request.env = self.env.with_context(lang="es_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        # Should contain translated strings (if translations loaded)
        self.assertIsNotNone(message)
        # In real scenario, would check for "¡Gracias!" or similar

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_eu(self, mock_request):
        """Test confirmation message in Basque (eu_ES)."""
        mock_request.env = self.env.with_context(lang="eu_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        self.assertIsNotNone(message)
        # In real scenario, would check for "Eskerrik asko!" or similar

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_ca(self, mock_request):
        """Test confirmation message in Catalan (ca_ES)."""
        mock_request.env = self.env.with_context(lang="ca_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        self.assertIsNotNone(message)
        # In real scenario, would check for "Gràcies!" or similar

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_gl(self, mock_request):
        """Test confirmation message in Galician (gl_ES)."""
        mock_request.env = self.env.with_context(lang="gl_ES")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        self.assertIsNotNone(message)
        # In real scenario, would check for "Grazas!" or similar

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_pt(self, mock_request):
        """Test confirmation message in Portuguese (pt_PT)."""
        mock_request.env = self.env.with_context(lang="pt_PT")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        self.assertIsNotNone(message)
        # In real scenario, would check for "Obrigado!" or similar

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_fr(self, mock_request):
        """Test confirmation message in French (fr_FR)."""
        mock_request.env = self.env.with_context(lang="fr_FR")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        self.assertIsNotNone(message)
        # In real scenario, would check for "Merci!" or similar

    @patch("odoo.http.request")
    def test_build_confirmation_message_multilang_it(self, mock_request):
        """Test confirmation message in Italian (it_IT)."""
        mock_request.env = self.env.with_context(lang="it_IT")

        result = self.controller._build_confirmation_message(
            self.sale_order, self.group_order, is_delivery=False
        )

        message = result["message"]
        self.assertIsNotNone(message)
        # In real scenario, would check for "Grazie!" or similar


class TestConfirmEskaera_Integration(TransactionCase):
    """Integration tests for confirm_eskaera() with all 3 helpers."""

    def setUp(self):
        super().setUp()
        self.controller = http.request.env["website.sale"].browse([])
        self.user = self.env.ref("base.user_admin")
        self.partner = self.env.ref("base.partner_admin")

        # Create test product
        self.product = self.env["product.product"].create(
            {
                "name": "Integration Test Product",
                "list_price": 20.0,
                "type": "consu",
            }
        )

        # Create test group order
        self.group_order = self.env["group.order"].create(
            {
                "name": "Integration Test Order",
                "state": "open",
                "pickup_day": "5",
                "pickup_date": date.today() + timedelta(days=5),
            }
        )

    @patch("odoo.http.request")
    def test_confirm_eskaera_full_flow_pickup(self, mock_request):
        """Test full confirm_eskaera flow for pickup order."""
        mock_request.env = self.env.with_user(self.user)
        mock_request.env.lang = "es_ES"
        mock_request.httprequest = Mock()

        # Prepare request data
        data = {
            "order_id": self.group_order.id,
            "items": [
                {
                    "product_id": self.product.id,
                    "quantity": 3,
                    "product_price": 20.0,
                }
            ],
            "is_delivery": False,
        }

        mock_request.httprequest.data = json.dumps(data).encode("utf-8")

        # Call confirm_eskaera
        response = self.controller.confirm_eskaera()

        # Verify response
        self.assertIsNotNone(response)
        response_data = json.loads(response.data.decode("utf-8"))

        self.assertTrue(response_data.get("success"))
        self.assertIn("message", response_data)
        self.assertIn("sale_order_id", response_data)

        # Verify sale.order was created
        sale_order_id = response_data["sale_order_id"]
        sale_order = self.env["sale.order"].browse(sale_order_id)

        self.assertTrue(sale_order.exists())
        self.assertEqual(sale_order.partner_id.id, self.partner.id)
        self.assertEqual(sale_order.group_order_id.id, self.group_order.id)
        self.assertEqual(len(sale_order.order_line), 1)
        self.assertEqual(sale_order.order_line[0].product_uom_qty, 3)

    @patch("odoo.http.request")
    def test_confirm_eskaera_full_flow_delivery(self, mock_request):
        """Test full confirm_eskaera flow for delivery order."""
        mock_request.env = self.env.with_user(self.user)
        mock_request.env.lang = "es_ES"
        mock_request.httprequest = Mock()

        # Add delivery_date to group order
        self.group_order.delivery_date = self.group_order.pickup_date + timedelta(
            days=1
        )

        # Prepare request data
        data = {
            "order_id": self.group_order.id,
            "items": [
                {
                    "product_id": self.product.id,
                    "quantity": 2,
                    "product_price": 20.0,
                }
            ],
            "is_delivery": True,
        }

        mock_request.httprequest.data = json.dumps(data).encode("utf-8")

        # Call confirm_eskaera
        response = self.controller.confirm_eskaera()

        # Verify response
        response_data = json.loads(response.data.decode("utf-8"))

        self.assertTrue(response_data.get("success"))

        # Verify sale.order has delivery flag
        sale_order_id = response_data["sale_order_id"]
        sale_order = self.env["sale.order"].browse(sale_order_id)

        self.assertTrue(sale_order.home_delivery)
        # commitment_date should be delivery_date
        self.assertEqual(
            sale_order.commitment_date.date(), self.group_order.delivery_date
        )

    @patch("odoo.http.request")
    def test_confirm_eskaera_updates_existing_draft(self, mock_request):
        """Test confirm_eskaera updates existing draft order instead of creating new."""
        mock_request.env = self.env.with_user(self.user)
        mock_request.env.lang = "es_ES"
        mock_request.httprequest = Mock()

        # Create existing draft order
        existing_order = self.env["sale.order"].create(
            {
                "partner_id": self.partner.id,
                "group_order_id": self.group_order.id,
                "state": "draft",
                "order_line": [
                    (
                        0,
                        0,
                        {
                            "product_id": self.product.id,
                            "product_uom_qty": 1,
                            "price_unit": 20.0,
                        },
                    )
                ],
            }
        )

        existing_order_id = existing_order.id

        # Prepare new request data
        data = {
            "order_id": self.group_order.id,
            "items": [
                {
                    "product_id": self.product.id,
                    "quantity": 5,  # Different quantity
                    "product_price": 20.0,
                }
            ],
            "is_delivery": False,
        }

        mock_request.httprequest.data = json.dumps(data).encode("utf-8")

        # Call confirm_eskaera
        response = self.controller.confirm_eskaera()

        response_data = json.loads(response.data.decode("utf-8"))

        # Should update existing order, not create new
        self.assertEqual(response_data["sale_order_id"], existing_order_id)

        # Verify order was updated
        existing_order.invalidate_recordset()
        self.assertEqual(len(existing_order.order_line), 1)
        self.assertEqual(existing_order.order_line[0].product_uom_qty, 5)
