import logging
from typing import Annotated, List, Type, TypeVar

from fastapi import Depends, Query, Request
from pydantic.types import Json, constr
from sqlalchemy import Select, desc, func, or_
from sqlalchemy.exc import ProgrammingError
from sqlalchemy_filters import apply_pagination

from src.database.schema import CommonParams

from .core import DbSession

log = logging.getLogger(__name__)

# allows only printable characters
QueryStr = constr(pattern=r"^[ -~]+$", min_length=1)


def common_parameters(
    db_session: DbSession,  # type: ignore
    params: Annotated[CommonParams, Query()] 
    # role: QueryStr = Depends(get_current_role),
):    
    return {
        "db_session": db_session,
        "page": params.page,
        "items_per_page": params.items_per_page,
        "query_str": params.query_str,
        "filter_spec": params.filter_spec,
        "sort_by": params.sort_by,
        "descending": params.descending,
        "current_user": params.current_user,
        "all": params.is_all,
        # "role": role,
    }


CommonParameters = Annotated[
    dict[str, int | str | DbSession | QueryStr | Json | List[str] | List[bool]] | bool,
    Depends(common_parameters),
]

T = TypeVar("T", bound=CommonParams)

def get_params_factory(model_type: Type[T]):
    async def wrapper(
        db_session: DbSession,
        params: Annotated[model_type, Query()] # type: ignore
    ):
        res = params.model_dump()
        return {
            "db_session": db_session,
            "all": params.is_all,
            **res
        }
    return wrapper


def search(*, query_str: str, query: Query, model, sort=False):
    """Perform a search based on the query."""
    search_model = model

    if not query_str.strip():
        return query

    search = []
    if hasattr(search_model, "search_vector"):
        vector = search_model.search_vector
        search.append(vector.op("@@")(func.tsq_parse(query_str)))

    if hasattr(search_model, "name"):
        search.append(
            search_model.name.ilike(f"%{query_str}%"),
        )
        search.append(search_model.name == query_str)

    if not search:
        raise Exception(f"Search not supported for model: {model}")

    query = query.filter(or_(*search))

    if sort:
        query = query.order_by(desc(func.ts_rank_cd(vector, func.tsq_parse(query_str))))

    return query.params(term=query_str)


async def search_filter_sort_paginate(
    db_session: DbSession,
    model,
    query_str: str = None,
    filter_spec: str | dict | None = None,
    page: int = 1,
    items_per_page: int = 5,
    sort_by: List[str] = None,
    descending: List[bool] = None,
    current_user: str = None,
    exclude: List[str] = None,
    all: bool = False,
    **extra_params,
):
    """Common functionality for searching, filtering, sorting, and pagination."""
    # try:
    # Check if model is Select
    if not isinstance(model, Select):
        query = Select(model)
    else:
        query = model

    if query_str:
        sort = False if sort_by else True
        query = search(query_str=query_str, query=query, model=model, sort=sort)

    # Get total count
    count_query = Select(func.count()).select_from(query.subquery())
    total = await db_session.scalar(count_query)

    if all:
        result = await db_session.execute(query)
        items = result.scalars().all()

        return {
            "items": items,
            "itemsPerPage": total,
            "page": 1,
            "total": total,
            "totalPages": 1,
        }

    query = query.offset((page - 1) * items_per_page).limit(items_per_page)

    result = await db_session.execute(query)
    items = result.scalars().all()

    return {
        "items": items,
        "itemsPerPage": items_per_page,
        "page": page,
        "total": total,
        "totalPages": (total + items_per_page - 1) // items_per_page,
    }
