"""DMS File search provider."""

from __future__ import annotations

import logging
import uuid
from typing import Any

from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession

logger = logging.getLogger(__name__)


class FileSearchProvider:
    """Search provider for DMS File entities."""

    entity_type = "file"

    async def search_fts(
        self,
        db: AsyncSession,
        tsquery: str,
        tenant_id: uuid.UUID,
        limit: int,
    ) -> list[dict[str, Any]]:
        """Full-text search on files.content_tsv."""
        sql = text(
            """
            SELECT f.*, ts_rank(f.content_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
            FROM files f
            WHERE f.tenant_id = :tid
              AND f.deleted_at IS NULL
              AND f.content_tsv @@ to_tsquery('pg_catalog.german', :q)
            ORDER BY rank DESC
            LIMIT :lim
            """
        )
        result = await db.execute(
            sql,
            {"q": tsquery, "tid": tenant_id, "lim": limit},
        )
        rows = result.mappings().all()
        return [dict(r) for r in rows]

    async def search_vector(
        self,
        db: AsyncSession,
        embedding: list[float],
        tenant_id: uuid.UUID,
        limit: int,
    ) -> list[dict[str, Any]]:
        """Semantic search on files.embedding."""
        sql = text(
            """
            SELECT f.*, 1 - (f.embedding <=> cast(:emb AS vector)) AS score
            FROM files f
            WHERE f.tenant_id = :tid
              AND f.deleted_at IS NULL
              AND f.embedding IS NOT NULL
            ORDER BY f.embedding <=> cast(:emb AS vector)
            LIMIT :lim
            """
        )
        result = await db.execute(
            sql,
            {"emb": str(embedding), "tid": tenant_id, "lim": limit},
        )
        rows = result.mappings().all()
        return [dict(r) for r in rows]

    async def get_embedding_text(
        self,
        db: AsyncSession,
        entity_id: uuid.UUID,
        tenant_id: uuid.UUID,
    ) -> str:
        """Get text for embedding generation."""
        sql = text(
            """
            SELECT name, content_text
            FROM files
            WHERE id = :eid AND tenant_id = :tid
            """
        )
        result = await db.execute(sql, {"eid": entity_id, "tid": tenant_id})
        row = result.mappings().first()
        if not row:
            return ""
        name = row.get("name", "") or ""
        content = row.get("content_text", "") or ""
        return f"{name} {content[:5000]}"

    def to_search_result(self, entity: object) -> dict[str, Any]:
        """Convert file to search result dict."""
        if isinstance(entity, dict):
            name = entity.get("name", "")
            content = entity.get("content_text", "") or ""
            entity_id = str(entity.get("id", ""))
        else:
            name = getattr(entity, "name", "")
            content = getattr(entity, "content_text", "") or ""
            entity_id = str(getattr(entity, "id", ""))
        return {
            "entity_type": self.entity_type,
            "entity_id": entity_id,
            "title": name,
            "snippet": content[:200],
            "score": 0.0,
            "data": {},
        }
