"""Tests for TemplateManager."""

import tempfile
from pathlib import Path

from src.fwauto.template_manager import TemplateManager


def test_list_templates():
    """測試列出所有 templates"""
    manager = TemplateManager()
    templates = manager.list_available_templates()

    # 至少應該有 STM32 和 AM62x 兩個內建 templates
    assert len(templates) >= 2

    # 驗證 template 結構
    for template in templates:
        assert "sdk_type" in template
        assert "path" in template
        assert "metadata" in template
        assert "source" in template


def test_get_template():
    """測試根據 SDK type 取得 template"""
    manager = TemplateManager()

    # 測試存在的 template (使用 metadata 中的 sdk.id)
    stm32_template = manager.get_template("stm32f4")
    assert stm32_template is not None
    assert stm32_template.exists()

    # 測試不存在的 template
    nonexistent_template = manager.get_template("nonexistent")
    assert nonexistent_template is None


def test_copy_template():
    """測試複製 template 至專案"""
    manager = TemplateManager()

    with tempfile.TemporaryDirectory() as tmpdir:
        project_root = Path(tmpdir)

        # 複製 STM32 template (使用 metadata 中的 sdk.id)
        success = manager.copy_template_to_project("stm32f4", project_root)
        assert success

        # 驗證檔案結構
        fwauto_dir = project_root / ".fwauto"
        assert (fwauto_dir / "build" / "Makefile").exists()
        assert (fwauto_dir / "build" / "build.ps1").exists()
        assert (fwauto_dir / "deploy" / "Makefile").exists()
        assert (fwauto_dir / "deploy" / "deploy.ps1").exists()


def test_copy_template_overwrite():
    """測試 overwrite 模式"""
    manager = TemplateManager()

    with tempfile.TemporaryDirectory() as tmpdir:
        project_root = Path(tmpdir)

        # 第一次複製 (使用 metadata 中的 sdk.id)
        manager.copy_template_to_project("stm32f4", project_root)

        # 修改檔案
        makefile = project_root / ".fwauto" / "build" / "Makefile"
        original_content = makefile.read_text()
        makefile.write_text("modified content")

        # 不使用 overwrite，應該保留修改
        manager.copy_template_to_project("stm32f4", project_root, overwrite=False)
        assert makefile.read_text() == "modified content"

        # 使用 overwrite，應該恢復原始內容
        manager.copy_template_to_project("stm32f4", project_root, overwrite=True)
        assert makefile.read_text() == original_content


def test_copy_nonexistent_template():
    """測試複製不存在的 template"""
    manager = TemplateManager()

    with tempfile.TemporaryDirectory() as tmpdir:
        project_root = Path(tmpdir)
        success = manager.copy_template_to_project("nonexistent", project_root)
        assert not success


def test_metadata_loading():
    """測試 metadata 載入"""
    manager = TemplateManager()
    templates = manager.list_available_templates()

    for template in templates:
        metadata = template["metadata"]

        # 驗證必要欄位
        assert "template" in metadata
        assert "name" in metadata["template"]
        assert "version" in metadata["template"]
        assert "description" in metadata["template"]

        assert "sdk" in metadata
        assert "id" in metadata["sdk"]
        assert "type" in metadata["sdk"]

        assert "files" in metadata
