from __future__ import annotations

import unittest

from pydantic import ValidationError

from app.schemas import MAX_SUPPORT_STIFFNESS, ProjectDocument, SolveRequest, Support
from app.solver import solve_structure


class SupportConstraintSchemaTests(unittest.TestCase):
    def test_support_accepts_fixed_and_elastic_dofs(self) -> None:
        support = Support.model_validate({
            "node_id": 7,
            "ux": {"mode": "fixed"},
            "uz": {"mode": "elastic", "stiffness": 2_500_000.0},
        })

        self.assertEqual(support.ux.mode, "fixed")
        self.assertEqual(support.uy.mode, "free")
        self.assertEqual(support.uz.mode, "elastic")
        self.assertEqual(support.uz.stiffness, 2_500_000.0)

    def test_elastic_dof_requires_positive_stiffness(self) -> None:
        for stiffness in (None, 0.0, -1.0):
            with self.subTest(stiffness=stiffness), self.assertRaises(ValidationError):
                Support.model_validate({
                    "node_id": 1,
                    "ux": {"mode": "elastic", "stiffness": stiffness},
                })

        with self.assertRaises(ValidationError):
            Support.model_validate({
                "node_id": 1,
                "ux": {
                    "mode": "elastic",
                    "stiffness": MAX_SUPPORT_STIFFNESS * 10,
                },
            })

    def test_non_elastic_dof_rejects_stiffness_and_empty_support(self) -> None:
        with self.assertRaises(ValidationError):
            Support.model_validate({
                "node_id": 1,
                "ux": {"mode": "fixed", "stiffness": 1_000.0},
            })
        with self.assertRaises(ValidationError):
            Support.model_validate({"node_id": 1})

    def test_legacy_support_and_project_format_are_rejected(self) -> None:
        with self.assertRaises(ValidationError):
            Support.model_validate({"node_id": 1, "fix_x": True})
        with self.assertRaises(ValidationError):
            ProjectDocument.model_validate({
                "format": "fse.project",
                "format_version": 1,
                "metadata": {"name": "Projeto antigo"},
                "model": {},
            })

    def test_rotational_support_is_rejected_on_pure_truss_node(self) -> None:
        request = SolveRequest.model_validate({
            "nodes": [
                {"id": 1, "x": 0.0, "y": 0.0, "z": 0.0},
                {"id": 2, "x": 1.0, "y": 0.0, "z": 0.0},
            ],
            "materials": [{"id": 1, "name": "Aco", "e": 200_000_000_000.0}],
            "elements": [{
                "id": 1,
                "start_node": 1,
                "end_node": 2,
                "material_id": 1,
                "area": 0.001,
                "start_release_ry": True,
                "start_release_rz": True,
                "end_release_ry": True,
                "end_release_rz": True,
            }],
            "supports": [{
                "node_id": 1,
                "ux": {"mode": "fixed"},
                "uy": {"mode": "fixed"},
                "uz": {"mode": "fixed"},
                "rx": {"mode": "fixed"},
            }],
            "loads": [{"node_id": 2, "fx": 1_000.0}],
            "solver_engine": "openseespy",
        })

        with self.assertRaisesRegex(ValueError, "Vinculacao rotacional nao se aplica"):
            solve_structure(request)


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