from pathlib import Path

import pytest

from GramAddict.core.darwin_engine import DarwinEngine

FIXTURES_DIR = Path(__file__).parent.parent / "fixtures"


def read_fixture(filename: str) -> str:
    path = FIXTURES_DIR / filename
    if not path.exists():
        pytest.skip(f"Fixture {filename} not found")
    with open(path, "r", encoding="utf-8") as f:
        return f.read()


@pytest.fixture
def darwin():
    engine = DarwinEngine("test_user")
    return engine


def test_has_comments_true_reel(darwin):
    # This reel has "Comment number is181. View comments"
    xml = read_fixture("explore_feed_reel.xml")
    assert darwin._has_comments(xml) is True


def test_has_comments_true_organic(darwin):
    # This organic post has "Photo 1 of 13 by Fiona Dawson, Liked by ..., 23 comments"
    xml = read_fixture("organic_post.xml")
    assert darwin._has_comments(xml) is True


def test_has_comments_zero_reel(darwin):
    # This reel has "Comment number is1247. View comments" so it DOES have comments
    xml = read_fixture("reels_feed_dump.xml")
    assert darwin._has_comments(xml) is True


def test_has_comments_regex_cases(darwin):
    # Specific edge cases string tests
    assert darwin._has_comments('<node text="View all 12 comments" />') is True
    assert darwin._has_comments('<node text="View 1 comment" />') is True
    assert darwin._has_comments('<node content-desc="Photo by Alice, 0 comments" />') is False
    assert darwin._has_comments('<node content-desc="Liked by john and others, 1,234 comments" />') is True
    # Just the comment button shouldn't trigger as having comments
    assert darwin._has_comments('<node content-desc="Comment" />') is False
