import unittest

from fastapi.testclient import TestClient

from app.main import create_app


class ComposeAndDecomposeNumbersEndpointTest(unittest.TestCase):
    def setUp(self) -> None:
        self.client = TestClient(create_app())
        self.picture = {
            "id": "cube",
            "name": "Cube",
            "image_path": "/images/cube.png",
        }

    def test_creates_ordered_problem(self) -> None:
        response = self.client.post(
            "/math/grade_1/compose_and_decompose_numbers",
            json={"picture": self.picture},
        )

        self.assertEqual(response.status_code, 200)
        problem = response.json()
        rows = problem["rows"]

        self.assertEqual(problem["instructions"], "Compón y descompón el número.")
        self.assertEqual(problem["whole"], 10)
        self.assertEqual(problem["picture"], self.picture)
        self.assertEqual(len(rows), 9)
        self.assertEqual(
            [(row["first_part"], row["second_part"]) for row in rows],
            [(9, 1), (8, 2), (7, 3), (6, 4), (5, 5), (4, 6), (3, 7), (2, 8), (1, 9)],
        )

        for row in rows:
            self.assertEqual(row["whole"], 10)
            self.assertEqual(row["first_part"] + row["second_part"], 10)
            self.assertEqual(row["picture"], self.picture)

    def test_can_randomize_rows(self) -> None:
        response = self.client.post(
            "/math/grade_1/compose_and_decompose_numbers",
            json={"picture": self.picture, "randomize_rows": True, "seed": 1},
        )

        self.assertEqual(response.status_code, 200)
        rows = response.json()["rows"]
        ordered_pairs = [(9, 1), (8, 2), (7, 3), (6, 4), (5, 5), (4, 6), (3, 7), (2, 8), (1, 9)]
        randomized_pairs = [(row["first_part"], row["second_part"]) for row in rows]

        self.assertNotEqual(randomized_pairs, ordered_pairs)
        self.assertCountEqual(randomized_pairs, ordered_pairs)

    def test_returns_bad_request_for_whole_less_than_two(self) -> None:
        response = self.client.post(
            "/math/grade_1/compose_and_decompose_numbers",
            json={"picture": self.picture, "whole": 1},
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), {"detail": "whole must be at least 2"})


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