"""
Fixtures for library API tests.
Provides test data and helpers for testing library/collection/RAG endpoints.
"""

import hashlib
import uuid

import pytest

from src.local_deep_research.database.models.library import (
    DocumentStatus,
)


@pytest.fixture
def sample_source_type_data():
    """Sample source type data for test documents."""
    return {
        "id": str(uuid.uuid4()),
        "name": "test_upload",
        "display_name": "Test Upload",
        "description": "Test documents for API tests",
        "icon": "fas fa-file",
    }


@pytest.fixture
def sample_collection_data():
    """Sample collection data for tests."""
    return {
        "name": f"Test Collection {uuid.uuid4().hex[:8]}",
        "description": "A test collection for API tests",
        "type": "user_uploads",
    }


@pytest.fixture
def sample_document_data():
    """Sample document data for tests."""
    content = "This is sample text content for testing API endpoints."
    return {
        "id": str(uuid.uuid4()),
        "document_hash": hashlib.sha256(content.encode()).hexdigest(),
        "file_size": len(content),
        "file_type": "text",
        "text_content": content,
        "title": "Test Document for API",
        "status": DocumentStatus.COMPLETED,
    }


def create_test_collection(client, name=None, description=None):
    """
    Helper to create a collection via API.

    Args:
        client: Flask test client (authenticated)
        name: Collection name (optional, auto-generated if not provided)
        description: Collection description (optional)

    Returns:
        dict: Created collection data or None if failed
    """
    payload = {
        "name": name or f"Test Collection {uuid.uuid4().hex[:8]}",
        "description": description or "Test collection",
        "type": "user_uploads",
    }

    response = client.post(
        "/library/api/collections",
        json=payload,
        content_type="application/json",
    )

    if response.status_code == 200:
        data = response.get_json()
        if data.get("success"):
            return data.get("collection")

    return None


def delete_test_collection(client, collection_id):
    """
    Helper to delete a collection via API.

    Args:
        client: Flask test client (authenticated)
        collection_id: ID of collection to delete

    Returns:
        bool: True if deleted successfully
    """
    response = client.delete(f"/library/api/collections/{collection_id}")
    return response.status_code == 200


@pytest.fixture
def create_collection_helper(authenticated_client):
    """Fixture that returns a helper function to create collections."""
    created_collections = []

    def _create(name=None, description=None):
        collection = create_test_collection(
            authenticated_client, name, description
        )
        if collection:
            created_collections.append(collection["id"])
        return collection

    yield _create

    # Cleanup: delete all created collections
    for coll_id in created_collections:
        try:
            delete_test_collection(authenticated_client, coll_id)
        except Exception:
            pass
