"""Tests for scripts/sync_version.py version synchronization."""

import textwrap
from pathlib import Path

from scripts.sync_version import read_version, update_plugin_cfg, update_server_gd, sync_all


class TestSyncVersion:
    """Verify sync_version reads pyproject.toml and patches target files."""

    def test_reads_version_from_pyproject_toml(self, tmp_path: Path):
        """sync_version extracts the correct version string from pyproject.toml."""
        pyproject = tmp_path / "pyproject.toml"
        pyproject.write_text(textwrap.dedent("""\
            [build-system]
            requires = ["hatchling"]

            [project]
            name = "godotiq"
            version = "0.1.4"
            description = "test"
        """))
        assert read_version(pyproject) == "0.1.4"

    def test_updates_plugin_cfg_version_line(self, tmp_path: Path):
        """sync_version replaces the version= line in plugin.cfg."""
        cfg = tmp_path / "plugin.cfg"
        cfg.write_text(textwrap.dedent("""\
            [plugin]

            name="GodotIQ"
            description="AI-assisted Godot development via MCP bridge"
            author="Nash Farruggio"
            version="0.1.0"
            script="godotiq_plugin.gd"
        """))
        assert update_plugin_cfg(cfg, "0.1.4") is True
        content = cfg.read_text()
        assert 'version="0.1.4"' in content
        # Other lines untouched
        assert 'name="GodotIQ"' in content
        assert 'script="godotiq_plugin.gd"' in content

    def test_updates_godotiq_server_gd_addon_version(self, tmp_path: Path):
        """sync_version replaces the ADDON_VERSION const in godotiq_server.gd."""
        gd = tmp_path / "godotiq_server.gd"
        gd.write_text(textwrap.dedent("""\
            @tool
            extends Node

            const DEFAULT_PORT := 6007
            const ADDON_VERSION := "0.1.0"
            const SCREENSHOT_TIMEOUT_MS := 30000
        """))
        assert update_server_gd(gd, "0.1.4") is True
        content = gd.read_text()
        assert 'const ADDON_VERSION := "0.1.4"' in content
        # Other lines untouched
        assert "const DEFAULT_PORT := 6007" in content
        assert "const SCREENSHOT_TIMEOUT_MS := 30000" in content

    def test_full_sync_with_version_0_1_4(self, tmp_path: Path):
        """End-to-end: all three files agree on '0.1.4' after sync."""
        # Create pyproject.toml
        (tmp_path / "pyproject.toml").write_text(textwrap.dedent("""\
            [project]
            name = "godotiq"
            version = "0.1.4"
        """))
        # Create plugin.cfg in expected relative path
        addon_dir = tmp_path / "godot-addon" / "addons" / "godotiq"
        addon_dir.mkdir(parents=True)
        (addon_dir / "plugin.cfg").write_text(textwrap.dedent("""\
            [plugin]
            name="GodotIQ"
            version="0.1.0"
            script="godotiq_plugin.gd"
        """))
        (addon_dir / "godotiq_server.gd").write_text(textwrap.dedent("""\
            @tool
            extends Node
            const ADDON_VERSION := "0.1.0"
        """))

        sync_all(tmp_path)

        assert 'version="0.1.4"' in (addon_dir / "plugin.cfg").read_text()
        assert 'const ADDON_VERSION := "0.1.4"' in (addon_dir / "godotiq_server.gd").read_text()

    def test_no_change_when_already_synced(self, tmp_path: Path):
        """update functions return False when version is already correct."""
        cfg = tmp_path / "plugin.cfg"
        cfg.write_text('version="0.1.4"\n')
        assert update_plugin_cfg(cfg, "0.1.4") is False

        gd = tmp_path / "godotiq_server.gd"
        gd.write_text('const ADDON_VERSION := "0.1.4"\n')
        assert update_server_gd(gd, "0.1.4") is False
