import unittest

from fastapi.testclient import TestClient

from app.main import create_app


class WhereAreMoreItemsEndpointTest(unittest.TestCase):
    def setUp(self) -> None:
        self.client = TestClient(create_app())

    def test_creates_problem(self) -> None:
        response = self.client.post(
            "/math/grade_1/where_are_more_items",
            json={
                "available_pictures": [
                    {
                        "id": f"picture-{index}",
                        "name": f"Picture {index}",
                        "image_path": f"/images/{index}.png",
                    }
                    for index in range(6)
                ],
                "seed": 1,
            },
        )

        self.assertEqual(response.status_code, 200)
        problem = response.json()
        self.assertEqual(problem["instructions"], "Marca dónde hay más.")
        self.assertEqual(len(problem["comparisons"]), 3)

        for comparison in problem["comparisons"]:
            left_group = comparison["left_group"]
            right_group = comparison["right_group"]

            self.assertEqual(left_group["side"], "left")
            self.assertEqual(right_group["side"], "right")
            self.assertNotEqual(left_group["quantity"], right_group["quantity"])
            self.assertIn("picture", left_group)
            self.assertIn("picture", right_group)

            expected_answer_side = (
                "left"
                if left_group["quantity"] > right_group["quantity"]
                else "right"
            )
            self.assertEqual(comparison["answer_side"], expected_answer_side)

    def test_returns_bad_request_for_too_few_figures(self) -> None:
        response = self.client.post(
            "/math/grade_1/where_are_more_items",
            json={
                "available_pictures": [
                    {
                        "id": "picture-1",
                        "name": "Picture 1",
                        "image_path": "/images/1.png",
                    }
                ]
            },
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.json(),
            {"detail": "available_pictures must contain at least 2 pictures"},
        )


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