"""
Tests for Automated Export functionality.

Tests the ability to export Godot projects from the command line.
Note: Full export requires export templates to be installed, so some tests
may verify command construction rather than actual export success.
"""

from pathlib import Path


from godot_mcp.server import project_export, project_list_export_presets


class TestExportProject:
    """Tests for the project_export tool."""

    def test_project_export_missing_project(self) -> None:
        """Test that exporting with invalid project path fails gracefully."""
        result = project_export(
            project_path="/nonexistent/project",
            preset_name="Windows Desktop",
            output_path="game.exe",
        )

        assert (
            "error" in result.lower() or "not" in result.lower() or "invalid" in result.lower()
        ), f"Expected error for invalid project, got: {result}"

    def test_project_export_missing_parameters(self) -> None:
        """Test that missing required parameters are handled."""
        result = project_export(
            project_path="",
            preset_name="Windows Desktop",
            output_path="game.exe",
        )
        assert "required" in result.lower() or "invalid" in result.lower(), (
            f"Expected error for empty project_path, got: {result}"
        )

    def test_project_export_no_preset(self, temp_godot_project: Path) -> None:
        """
        Test that exporting without a configured preset fails gracefully.

        This tests the case where the project doesn't have export_presets.cfg.
        """
        result = project_export(
            project_path=str(temp_godot_project),
            preset_name="Windows Desktop",
            output_path=str(temp_godot_project / "builds" / "game.exe"),
        )

        # Should fail because there's no export_presets.cfg
        # The error message should mention preset or configuration
        # Note: This is expected to fail because the temp project has no presets
        assert (
            "preset" in result.lower() or "error" in result.lower() or "failed" in result.lower()
        ), f"Expected error about missing preset, got: {result}"

    def test_project_export_with_preset_file(self, temp_godot_project: Path) -> None:
        """
        Test export with a configured preset.

        Creates a minimal export_presets.cfg and attempts export.
        Note: May still fail if export templates are not installed.
        """
        # Create a minimal export_presets.cfg
        export_presets_content = """[preset.0]

name="Test Export"
platform="Windows Desktop"
runnable=true
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path=""
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false

[preset.0.options]

custom_template/debug=""
custom_template/release=""
"""
        presets_file = temp_godot_project / "export_presets.cfg"
        presets_file.write_text(export_presets_content, encoding="utf-8")

        # Try to export - this will likely fail due to missing export templates
        # but the tool invocation should work correctly
        result = project_export(
            project_path=str(temp_godot_project),
            preset_name="Test Export",
            output_path=str(temp_godot_project / "builds" / "game.exe"),
        )

        # The result should at least attempt the export
        # It's okay if it fails due to missing templates, we're testing the tool works
        assert isinstance(result, str), "Expected a string result"

    def test_export_pack_file(self, temp_godot_project: Path) -> None:
        """
        Test exporting as a PCK file.

        PCK export may work without full export templates.
        """
        # Create export presets
        export_presets_content = """[preset.0]

name="PCK Export"
platform="Windows Desktop"
runnable=true
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path=""
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false

[preset.0.options]

custom_template/debug=""
custom_template/release=""
"""
        presets_file = temp_godot_project / "export_presets.cfg"
        presets_file.write_text(export_presets_content, encoding="utf-8")

        # Create builds directory
        builds_dir = temp_godot_project / "builds"
        builds_dir.mkdir(exist_ok=True)

        # Try to export as PCK
        result = project_export(
            project_path=str(temp_godot_project),
            preset_name="PCK Export",
            output_path=str(builds_dir / "game.pck"),
            export_pack=True,
        )

        # The result should at least attempt the export
        assert isinstance(result, str), "Expected a string result"

    def test_project_export_debug_mode(self, temp_godot_project: Path) -> None:
        """Test export with debug mode enabled."""
        # Create export presets
        export_presets_content = """[preset.0]

name="Debug Export"
platform="Windows Desktop"
runnable=true

[preset.0.options]

custom_template/debug=""
custom_template/release=""
"""
        presets_file = temp_godot_project / "export_presets.cfg"
        presets_file.write_text(export_presets_content, encoding="utf-8")

        result = project_export(
            project_path=str(temp_godot_project),
            preset_name="Debug Export",
            output_path=str(temp_godot_project / "game.exe"),
            debug=True,
        )

        # The result should at least attempt the export
        assert isinstance(result, str), "Expected a string result"


class TestListExportPresets:
    """Tests for the project_list_export_presets tool."""

    def test_list_presets_no_config(self, temp_godot_project: Path) -> None:
        """Test listing presets when no export_presets.cfg exists."""
        result = project_list_export_presets(
            project_path=str(temp_godot_project),
        )

        # Should indicate no presets found
        assert "no" in result.lower() or "[]" in result or "empty" in result.lower(), (
            f"Expected no presets, got: {result}"
        )

    def test_list_presets_with_config(self, temp_godot_project: Path) -> None:
        """Test listing presets when export_presets.cfg exists."""
        # Create export presets
        export_presets_content = """[preset.0]

name="Windows"
platform="Windows Desktop"

[preset.1]

name="Linux"
platform="Linux"
"""
        presets_file = temp_godot_project / "export_presets.cfg"
        presets_file.write_text(export_presets_content, encoding="utf-8")

        result = project_list_export_presets(
            project_path=str(temp_godot_project),
        )

        # Should list the presets
        assert "Windows" in result or "windows" in result.lower(), (
            f"Expected to find 'Windows' preset, got: {result}"
        )

    def test_list_presets_invalid_project(self) -> None:
        """Test listing presets for invalid project path."""
        result = project_list_export_presets(
            project_path="/nonexistent/project",
        )

        assert (
            "error" in result.lower() or "not" in result.lower() or "invalid" in result.lower()
        ), f"Expected error for invalid project, got: {result}"
