"""Tests for core functionality."""

import pytest
import asyncio
from semantic_comparer.core import (
    split_paragraphs,
    calculate_containment_score,
    AlignmentPair,
    SemanticAligner,
)


def test_split_paragraphs():
    """Test paragraph splitting functionality."""
    text = "Paragraph 1.\n\nParagraph 2.\n\n\nParagraph 3."
    paragraphs = split_paragraphs(text)
    assert len(paragraphs) == 3
    assert paragraphs[0] == "Paragraph 1."
    assert paragraphs[1] == "Paragraph 2."
    assert paragraphs[2] == "Paragraph 3."


def test_split_paragraphs_empty():
    """Test paragraph splitting with empty text."""
    text = ""
    paragraphs = split_paragraphs(text)
    assert len(paragraphs) == 0


def test_calculate_containment_score():
    """Test containment score calculation."""
    paragraphs_a = ["Text A1", "Text A2", "Text A3"]
    
    # Perfect matches
    pairs = [
        AlignmentPair(type="match", text_a="Text A1", text_b="Text B1", score=0.9),
        AlignmentPair(type="match", text_a="Text A2", text_b="Text B2", score=0.8),
        AlignmentPair(type="match", text_a="Text A3", text_b="Text B3", score=0.7),
    ]
    
    score = calculate_containment_score(pairs, paragraphs_a)
    assert 0.7 <= score <= 0.9  # Weighted average should be in this range


def test_calculate_containment_score_no_matches():
    """Test containment score with no matches."""
    paragraphs_a = ["Text A1", "Text A2"]
    pairs = [
        AlignmentPair(type="gap_b", text_a="Text A1"),
        AlignmentPair(type="gap_b", text_a="Text A2"),
    ]
    
    score = calculate_containment_score(pairs, paragraphs_a)
    assert score == 0.0


@pytest.mark.asyncio
async def test_semantic_aligner_initialization():
    """Test SemanticAligner initialization."""
    aligner = SemanticAligner(
        model_name="all-MiniLM-L6-v2",
        gap_penalty=0.3,
        similarity_threshold=0.5
    )
    
    assert aligner.model_name == "all-MiniLM-L6-v2"
    assert aligner.gap_penalty == 0.3
    assert aligner.similarity_threshold == 0.5
    assert aligner._model is None


@pytest.mark.asyncio
async def test_semantic_aligner_simple_alignment():
    """Test simple text alignment."""
    aligner = SemanticAligner()
    
    text_a = "This is the first paragraph.\n\nThis is the second paragraph."
    text_b = "This is the first paragraph.\n\nThis is a different second paragraph."
    
    result = await aligner.align_texts(text_a, text_b)
    
    assert result.total_paragraphs_a == 2
    assert result.total_paragraphs_b == 2
    assert result.containment_score >= 0.0
    assert result.containment_score <= 1.0
    assert len(result.pairs) > 0 