"""Tests for core functionality."""

import pytest
from pathlib import Path
from tempfile import TemporaryDirectory
from tagitmd.core import (
    has_valid_frontmatter,
    has_tags_in_frontmatter,
    get_frontmatter_and_content,
    add_tags_to_file,
    find_markdown_files,
)


def test_has_valid_frontmatter():
    """Test frontmatter validation."""
    assert has_valid_frontmatter("---\ntitle: Test\n---\nContent")
    assert has_valid_frontmatter("---\n---\nContent")
    assert not has_valid_frontmatter("No frontmatter")
    assert not has_valid_frontmatter("---\nNo closing")
    assert not has_valid_frontmatter("")


def test_has_tags_in_frontmatter():
    """Test tag detection in frontmatter."""
    assert has_tags_in_frontmatter("---\ntags:\n  - test\n---\nContent")
    assert has_tags_in_frontmatter("---\ntags: [test, demo]\n---\nContent")
    assert not has_tags_in_frontmatter("---\ntitle: Test\n---\nContent")
    assert not has_tags_in_frontmatter("No frontmatter")


def test_get_frontmatter_and_content():
    """Test frontmatter extraction."""
    content = "---\ntitle: Test\ntags: [a, b]\n---\nBody content"
    metadata, body = get_frontmatter_and_content(content)
    assert metadata["title"] == "Test"
    assert metadata["tags"] == ["a", "b"]
    assert body.strip() == "Body content"


def test_add_tags_to_file_new_frontmatter():
    """Test adding tags to file without frontmatter."""
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        filepath.write_text("Some content\n")
        
        modified, msg = add_tags_to_file(filepath, ["test"], False, dry_run=False)
        
        assert modified
        assert "ADDED" in msg
        content = filepath.read_text()
        assert "tags:" in content
        assert "test" in content


def test_add_tags_to_file_existing_frontmatter():
    """Test adding tags to file with existing frontmatter."""
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        filepath.write_text("---\ntitle: Test\n---\nContent\n")
        
        modified, msg = add_tags_to_file(filepath, ["newtag"], False, dry_run=False)
        
        assert modified
        assert "ADDED" in msg
        content = filepath.read_text()
        assert "tags:" in content
        assert "newtag" in content


def test_add_tags_to_file_only_untagged():
    """Test --only-untagged flag."""
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        filepath.write_text("---\ntags: [existing]\n---\nContent\n")
        
        modified, msg = add_tags_to_file(filepath, ["newtag"], True, dry_run=False)
        
        assert not modified
        assert "SKIP" in msg
        assert "already has tags" in msg


def test_add_tags_to_file_dry_run():
    """Test dry run mode."""
    with TemporaryDirectory() as tmpdir:
        filepath = Path(tmpdir) / "test.md"
        original_content = "Some content\n"
        filepath.write_text(original_content)
        
        modified, msg = add_tags_to_file(filepath, ["test"], False, dry_run=True)
        
        assert modified
        assert "WOULD ADD" in msg
        assert filepath.read_text() == original_content


def test_find_markdown_files():
    """Test finding markdown files."""
    with TemporaryDirectory() as tmpdir:
        tmpdir_path = Path(tmpdir)
        
        (tmpdir_path / "file1.md").write_text("content")
        (tmpdir_path / "file2.txt").write_text("content")
        (tmpdir_path / "subdir").mkdir()
        (tmpdir_path / "subdir" / "file3.md").write_text("content")
        (tmpdir_path / ".hidden").mkdir()
        (tmpdir_path / ".hidden" / "file4.md").write_text("content")
        
        files = find_markdown_files(tmpdir_path, recursive=True)
        file_names = [f.name for f in files]
        
        assert "file1.md" in file_names
        assert "file3.md" in file_names
        assert "file2.txt" not in file_names
        assert "file4.md" not in file_names
        
        files_nonrecursive = find_markdown_files(tmpdir_path, recursive=False)
        assert len(files_nonrecursive) == 1
        assert files_nonrecursive[0].name == "file1.md"
