"""Tests for the init command."""

import yaml
import pytest
from pathlib import Path

from supermemory.init import init_project


@pytest.fixture
def tmp_project(tmp_path):
    """Create a temporary project directory."""
    return tmp_path


def test_init_creates_directory_structure(tmp_project):
    root = init_project(project="test-project", me="alice", base=tmp_project)

    assert root == tmp_project / ".memory"
    assert root.is_dir()

    for d in ["systems", "decisions", "gotchas", "research", "people", "sessions", "scratch"]:
        assert (root / d).is_dir(), f"{d}/ not created"


def test_init_creates_config(tmp_project):
    root = init_project(project="my-app", me="bob", base=tmp_project)

    config_path = root / "config.yaml"
    assert config_path.exists()

    config = yaml.safe_load(config_path.read_text())
    assert config["project"]["name"] == "my-app"
    assert any(m["id"] == "bob" for m in config["team"]["members"])


def test_init_creates_agent_protocol(tmp_project):
    root = init_project(project="test", base=tmp_project)

    protocol = root / "AGENT_PROTOCOL.md"
    assert protocol.exists()
    assert "Agent Protocol" in protocol.read_text()


def test_init_creates_templates(tmp_project):
    root = init_project(project="test", base=tmp_project)

    for d in ["systems", "decisions", "gotchas", "research", "people", "sessions"]:
        template = root / d / "_template.md"
        assert template.exists(), f"{d}/_template.md not created"


def test_init_creates_me_file(tmp_project):
    root = init_project(project="test", me="charlie", base=tmp_project)

    me_file = root / ".me"
    assert me_file.exists()
    assert me_file.read_text().strip() == "charlie"


def test_init_creates_person_file(tmp_project):
    root = init_project(project="test", me="dave", base=tmp_project)

    person = root / "people" / "dave.md"
    assert person.exists()
    assert "dave" in person.read_text()


def test_init_creates_gitignore(tmp_project):
    root = init_project(project="test", base=tmp_project)

    gitignore = root / ".gitignore"
    assert gitignore.exists()
    content = gitignore.read_text()
    assert "sessions/" in content
    assert "scratch/" in content
    assert ".me" in content
    assert ".obsidian/" in content


def test_init_creates_summary(tmp_project):
    root = init_project(project="test-proj", base=tmp_project)

    summary = root / "SUMMARY.md"
    assert summary.exists()
    assert "test-proj" in summary.read_text()


def test_init_no_me(tmp_project):
    """Init without specifying me should still work."""
    root = init_project(project="test", base=tmp_project)

    assert not (root / ".me").exists()
    # Only _template.md should exist (no person file without me)
    people_files = [f for f in (root / "people").glob("*.md") if not f.name.startswith("_")]
    assert not people_files
    assert (root / "people" / "_template.md").exists()


def test_init_claude_code_platform(tmp_project):
    init_project(project="test", platform="claude-code", base=tmp_project)

    claude_md = tmp_project / "CLAUDE.md"
    assert claude_md.exists()
    assert "Project Memory" in claude_md.read_text()


def test_init_cursor_platform(tmp_project):
    init_project(project="test", platform="cursor", base=tmp_project)

    cursorrules = tmp_project / ".cursorrules"
    assert cursorrules.exists()
    assert "Agent Protocol" in cursorrules.read_text()


def test_init_aider_platform(tmp_project):
    init_project(project="test", platform="aider", base=tmp_project)

    conf = tmp_project / ".aider.conf.yml"
    assert conf.exists()
    assert "AGENT_PROTOCOL.md" in conf.read_text()


def test_init_obsidian(tmp_project):
    root = init_project(project="test", obsidian=True, base=tmp_project)

    obsidian_dir = root / ".obsidian"
    assert obsidian_dir.is_dir()
    assert (obsidian_dir / "app.json").exists()


def test_init_tracker_beads(tmp_project):
    root = init_project(project="test", tracker="beads", base=tmp_project)

    config = yaml.safe_load((root / "config.yaml").read_text())
    assert config["integrations"]["beads"] is True


def test_init_idempotent(tmp_project):
    """Running init twice should not break anything."""
    init_project(project="test", me="eve", base=tmp_project)
    root = init_project(project="test", me="eve", base=tmp_project)

    # Should still work fine
    assert root.is_dir()
    gitignore = (root / ".gitignore").read_text()
    # Entries should not be duplicated
    assert gitignore.count("sessions/") == 1
