"""Tests for plantuml functionality."""

from ctao_datamodel import (
    PlantUMLDiagram,
    generate_plantuml_diagrams,
    model_to_plantuml_class,
    model_to_plantuml_relations,
)


def test_plantuml_class(example_model):
    puml = model_to_plantuml_class(example_model)
    assert puml.startswith("@startuml")
    assert puml.endswith("@enduml")
    assert "+ optional_option [opt]:" in puml


def test_plantuml_relations(example_model):
    puml = model_to_plantuml_relations(example_model)
    assert puml.startswith("@startuml")
    assert puml.endswith("@enduml")
    assert 'ExampleModel "+optional_option" *-- "0..1" OptionEnum' in puml


def test_plantuml_relations_inhertance(example_model):
    puml = model_to_plantuml_relations(example_model, show_inheritance=True)
    assert puml.startswith("@startuml")
    assert puml.endswith("@enduml")
    assert 'ExampleModel "+optional_option" *-- "0..1" OptionEnum' in puml
    assert "ParentModel <|-- ChildModel" in puml


def test_plantuml_relations_imports(example_model):
    puml = model_to_plantuml_relations(
        example_model, import_definitions=True, show_inheritance=True
    )
    assert puml.startswith("@startuml")
    assert puml.endswith("@enduml")
    assert 'ExampleModel "+optional_option" *-- "0..1" OptionEnum' in puml

    assert "!include ./definitions/OptionEnum.plantuml" in puml
    assert "!include ./definitions/Test.Namespace.Pointing.plantuml" in puml
    assert "!include ./definitions/ChildModel.plantuml" in puml
    assert "!include ./definitions/ExampleModel.plantuml" in puml
    assert "!include ./definitions/ParentModel.plantuml" in puml


def test_plantuml_generate(example_model, tmp_path):
    generate_plantuml_diagrams([example_model], tmp_path / "plantuml")

    assert (tmp_path / "plantuml" / "full_ExampleModel.plantuml").exists()
    assert (tmp_path / "plantuml" / "definitions" / "OptionEnum.plantuml").exists()
    assert (
        tmp_path / "plantuml" / "definitions" / "Test.Namespace.Pointing.plantuml"
    ).exists()


def test_plantuml_diagram(example_model, tmp_path):
    """Check that the diagram helper works."""

    # from model
    dia = PlantUMLDiagram(example_model)
    assert len(dia.plantuml_text) > 0
    assert "Error" not in dia._repr_html_()

    # from string
    dia = PlantUMLDiagram("@startuml\nA --> B\n@enduml")
    assert len(dia.plantuml_text) > 0
    assert "Error" not in dia._repr_html_()

    # from file
    generate_plantuml_diagrams([example_model], tmp_path)

    dia = PlantUMLDiagram.from_path(tmp_path / "full_ExampleModel.plantuml")
    assert len(dia.plantuml_text) > 0
    assert "Error" not in dia._repr_html_()
