import unittest

from fastapi.testclient import TestClient

from app.main import create_app


class SubtractWithImageReferenceEndpointTest(unittest.TestCase):
    def setUp(self) -> None:
        self.client = TestClient(create_app())
        self.figure = {
            "id": "fish",
            "name": "Fish",
            "image_path": "/images/fish.png",
        }

    def test_creates_subtraction_problem(self) -> None:
        response = self.client.post(
            "/math/grade_1/subtract_with_image_reference",
            json={
                "object_name": "peces",
                "initial_quantity": 5,
                "removed_quantity": 2,
                "actor_name": "Diego",
                "figure": self.figure,
            },
        )

        self.assertEqual(response.status_code, 200)
        problem = response.json()

        self.assertEqual(problem["title"], "¿Cuántos quedan?")
        self.assertEqual(
            problem["question"],
            "Hay 5 peces. Diego sacó 2 peces. ¿Cuántos peces quedan?",
        )
        self.assertEqual(problem["object_name"], "peces")
        self.assertEqual(problem["actor_name"], "Diego")
        self.assertEqual(problem["figure"], self.figure)
        self.assertEqual(
            problem["remaining_group"],
            {"label": "quedan", "quantity": 3, "picture": self.figure},
        )
        self.assertEqual(
            problem["subtracted_group"],
            {"label": "sacó", "quantity": 2, "picture": self.figure},
        )
        self.assertEqual(
            problem["equation"],
            {
                "initial_quantity": 5,
                "removed_quantity": 2,
                "remaining_quantity": 3,
                "symbol": "-",
                "equals_symbol": "=",
            },
        )

    def test_allows_zero_remaining_figures(self) -> None:
        response = self.client.post(
            "/math/grade_1/subtract_with_image_reference",
            json={
                "object_name": "peces",
                "initial_quantity": 2,
                "removed_quantity": 2,
                "figure": self.figure,
            },
        )

        self.assertEqual(response.status_code, 200)
        problem = response.json()
        self.assertEqual(problem["equation"]["remaining_quantity"], 0)
        self.assertEqual(problem["remaining_group"]["quantity"], 0)
        self.assertEqual(problem["subtracted_group"]["quantity"], 2)

    def test_returns_bad_request_when_removed_quantity_is_too_large(self) -> None:
        response = self.client.post(
            "/math/grade_1/subtract_with_image_reference",
            json={
                "object_name": "peces",
                "initial_quantity": 2,
                "removed_quantity": 5,
                "figure": self.figure,
            },
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.json(),
            {"detail": "removed_quantity must be less than or equal to initial_quantity"},
        )


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