import unittest

from fastapi.testclient import TestClient

from app.main import create_app


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

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

        self.assertEqual(response.status_code, 200)
        problem = response.json()
        self.assertEqual(
            problem["instructions"],
            "Cuenta las imágenes y une cada grupo con el número correcto.",
        )
        self.assertEqual(len(problem["left_containers"]), 5)
        self.assertEqual(len(problem["number_cards"]), 5)
        self.assertEqual(len(problem["right_containers"]), 5)

    def test_returns_bad_request_for_too_few_pictures(self) -> None:
        response = self.client.post(
            "/math/grade_1/join_pictures_with_quantity",
            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 10 pictures"},
        )


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