"""Test fixtures for knowledge module tests."""

from uuid import uuid4

import pytest
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker

from src.db.models import Base
from src.extraction.models import ExtractedRecord


@pytest.fixture
def db_session():
    """Create a mock database session for unit tests.

    Returns a MagicMock that simulates async session behavior.
    For real database tests, use integration tests with PostgreSQL.
    """
    from unittest.mock import AsyncMock, MagicMock

    mock_session = MagicMock()
    mock_session.begin = AsyncMock(return_value=AsyncMock(
        __aenter__=AsyncMock(),
        __aexit__=AsyncMock(),
    ))
    mock_session.execute = AsyncMock()
    mock_session.commit = AsyncMock()
    mock_session.rollback = AsyncMock()
    mock_session.close = AsyncMock()

    return mock_session


@pytest.fixture
def test_document_id():
    """Generate a test document ID."""
    return uuid4()


@pytest.fixture
def sample_node_id():
    """Generate a stable UUID (as string) representing a hierarchy node."""
    return str(uuid4())


@pytest.fixture
def sample_extracted_records(test_document_id):
    """Create sample ExtractedRecord objects for testing.

    Returns:
        List of 10 sample records with realistic content.
    """
    records = []

    for i in range(10):
        record = ExtractedRecord(
            content={
                "Item Code": f"2024-4_{i:04d}",
                "Description": f"Test Item {i}",
                "Quantity": str(100 + i),
                "Screen": "○" if i % 2 == 0 else "×",
            },
            headers=["Item Code", "Description", "Quantity", "Screen"],
            _source={
                "document_id": str(test_document_id),
                "filename": "test_document.xlsx",
                "sheet": "Sheet1",
                "table_id": 1,
                "row": 10 + i,
                "col_range": "A:D",
            },
        )
        records.append(record)

    return records


@pytest.fixture
def sample_extracted_record_with_resolved(test_document_id):
    """Create a single ExtractedRecord with resolved content.

    Used for testing symbol resolution flow.
    """
    record = ExtractedRecord(
        content={
            "Item Code": "2024-4_0001",
            "Description": "Test Item",
            "Status": "○",
        },
        headers=["Item Code", "Description", "Status"],
        _source={
            "document_id": str(test_document_id),
            "filename": "test_document.xlsx",
            "sheet": "Sheet1",
            "table_id": 1,
            "row": 10,
            "col_range": "A:C",
        },
    )

    # Simulate resolved content (added by symbol resolver)
    record.resolved_content = {
        "Item Code": "2024-4_0001",
        "Description": "Test Item",
        "Status": "Available",  # Symbol resolved: ○ → Available
    }

    return record
