"""Tests for CLI interface."""

import pytest
from click.testing import CliRunner
from pathlib import Path
from tempfile import TemporaryDirectory
import frontmatter
from tagitmd.cli import main


def test_cli_basic():
    """Test basic CLI invocation."""
    runner = CliRunner()
    
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        filepath.write_text("Content\n")
        
        result = runner.invoke(main, ["testtag", "--path", tmpdir])
        
        assert result.exit_code == 0
        assert "ADDED" in result.output
        assert "test.md" in result.output
        
        content = filepath.read_text()
        post = frontmatter.loads(content)
        assert "tags" in post.metadata
        assert "testtag" in post.metadata["tags"]
        assert post.content.strip() == "Content"


def test_cli_multiple_tags():
    """Test CLI with multiple tags."""
    runner = CliRunner()
    
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        filepath.write_text("Content\n")
        
        result = runner.invoke(main, ["tag1", "tag2", "tag3", "--path", tmpdir])
        
        assert result.exit_code == 0
        assert "tag1" in result.output
        assert "tag2" in result.output
        assert "tag3" in result.output
        
        content = filepath.read_text()
        post = frontmatter.loads(content)
        assert "tags" in post.metadata
        assert set(post.metadata["tags"]) == {"tag1", "tag2", "tag3"}
        assert post.content.strip() == "Content"


def test_cli_only_untagged():
    """Test --only-untagged flag."""
    runner = CliRunner()
    
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        filepath.write_text("---\ntags: [existing]\n---\nContent\n")
        
        result = runner.invoke(main, ["newtag", "--path", tmpdir, "--only-untagged"])
        
        assert result.exit_code == 0
        assert "SKIP" in result.output
        assert "already has tags" in result.output
        
        content = filepath.read_text()
        post = frontmatter.loads(content)
        assert post.metadata["tags"] == ["existing"]
        assert post.content.strip() == "Content"


def test_cli_dry_run():
    """Test --dry-run mode doesn't modify files."""
    runner = CliRunner()
    
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        original_content = "Content\n"
        filepath.write_text(original_content)
        
        result = runner.invoke(main, ["testtag", "--path", tmpdir, "--dry-run"])
        
        assert result.exit_code == 0
        assert "DRY RUN MODE" in result.output
        assert "WOULD ADD" in result.output
        assert filepath.read_text() == original_content


def test_cli_no_tags_error():
    """Test CLI error when no tags provided."""
    runner = CliRunner()
    
    result = runner.invoke(main, [])
    
    assert result.exit_code != 0


def test_cli_summary():
    """Test CLI summary output."""
    runner = CliRunner()
    
    with TemporaryDirectory() as tmpdir:
        file1 = Path(tmpdir) / "file1.md"
        file2 = Path(tmpdir) / "file2.md"
        file1.write_text("Content 1\n")
        file2.write_text("Content 2\n")
        
        result = runner.invoke(main, ["testtag", "--path", tmpdir])
        
        assert result.exit_code == 0
        assert "Summary" in result.output
        assert "Modified: 2" in result.output
        
        for filepath in [file1, file2]:
            content = filepath.read_text()
            post = frontmatter.loads(content)
            assert "tags" in post.metadata
            assert "testtag" in post.metadata["tags"]
