from unittest.mock import MagicMock, patch

import pytest

from synth_pdb.main import main


def test_main_structure_inference_fail():
    """Cover sys.exit(1) for structure inference failure (line 521)."""
    # Providing a structure that doesn't define any residue range
    with patch("sys.argv", ["synth-pdb", "--structure", "invalid:alpha"]):
        with pytest.raises(SystemExit) as excinfo:
            main()
        assert excinfo.value.code == 1


def test_main_docking_missing_input():
    """Cover sys.exit(1) for docking without input-pdb."""
    with patch("sys.argv", ["synth-pdb", "--mode", "docking"]):
        with pytest.raises(SystemExit) as excinfo:
            main()
        assert excinfo.value.code == 1


def test_main_pymol_missing_args():
    """Cover sys.exit(1) for pymol without inputs."""
    with patch("sys.argv", ["synth-pdb", "--mode", "pymol"]):
        with pytest.raises(SystemExit) as excinfo:
            main()
        assert excinfo.value.code == 1


def test_main_relax_logic():
    """Cover relaxation logic paths."""
    with patch(
        "synth_pdb.main.generate_pdb_content",
        return_value="ATOM      1  N   ALA A   1       0.000   0.000   0.000  1.00 20.00           N\nATOM      2  H   ALA A   1       0.000   0.000   1.000  1.00 20.00           H\n",
    ), patch("synth_pdb.relaxation.calculate_relaxation_rates", return_value={}), patch(
        "synth_pdb.nef_io.write_nef_relaxation"
    ), patch(
        "builtins.open", MagicMock()
    ):

        with patch(
            "sys.argv", ["synth-pdb", "--sequence", "A", "--gen-relax", "--tumbling-time", "8.0"]
        ):
            try:
                main()
            except SystemExit:
                pass


def test_main_visualize_highlight_structure_failure():
    """Cover line 745: could not parse structure for highlighting."""
    with patch("synth_pdb.main.generate_pdb_content", return_value="ATOM..."), patch(
        "builtins.open", MagicMock()
    ):
        # Invalid structure format (triggers exception in parsing)
        with patch("sys.argv", ["synth-pdb", "--sequence", "A", "--structure", "None:None:None"]):
            try:
                main()
            except SystemExit:
                pass
