import textwrap

from r2pb.parser import parse_msg_content
from r2pb.generator import ProtoGenerator


def test_generate_proto_with_dependencies():
    """Test generating a .proto file with dependencies."""
    msg_content = textwrap.dedent("""
        # Test message
        string name
        std_msgs/Header header
        another_pkg/AnotherMessage data
        """)
    parsed_msg = parse_msg_content(msg_content)
    generator = ProtoGenerator()

    proto_content, dependencies = generator.generate_proto(
        parsed_msg,
        package_name="my_package",
        msg_name="Person"
    )

    expected_proto = textwrap.dedent("""
        // Generated by r2pb - from my_package/Person.msg
        syntax = "proto3";

        package my_package;

        import "another_pkg/AnotherMessage.proto";
        import "std_msgs/Header.proto";

        message Person {
          string name = 1;
          std_msgs.Header header = 2;
          another_pkg.AnotherMessage data = 3;
        }
        """).strip()

    actual_lines = [line.strip() for line in proto_content.strip().splitlines() if line.strip()]
    expected_lines = [line.strip() for line in expected_proto.strip().splitlines() if line.strip()]

    assert actual_lines == expected_lines
    assert sorted(dependencies) == ["another_pkg/AnotherMessage", "std_msgs/Header"]


def test_generate_proto_no_dependencies():
    """Test generating a .proto file with no dependencies."""
    msg_content = textwrap.dedent("""
        # Simple message
        string first_name
        int32 age
        """)
    parsed_msg = parse_msg_content(msg_content)
    generator = ProtoGenerator()

    proto_content, dependencies = generator.generate_proto(
        parsed_msg,
        package_name="my_package",
        msg_name="User"
    )

    expected_proto = textwrap.dedent("""
        // Generated by r2pb - from my_package/User.msg
        syntax = "proto3";

        package my_package;

        message User {
          string first_name = 1;
          int32 age = 2;
        }
        """).strip()

    actual_lines = [line.strip() for line in proto_content.strip().splitlines() if line.strip()]
    expected_lines = [line.strip() for line in expected_proto.strip().splitlines() if line.strip()]

    assert actual_lines == expected_lines
    assert dependencies == []