from django.conf import settings as s
from ninja import Router
from ninja_extra.pagination import paginate
from uuid import UUID

from epdb.models import Compound
from ..pagination import EnhancedPageNumberPagination
from ..schemas import CompoundOutSchema, ReviewStatusFilter
from ..dal import get_user_entities_for_read, get_package_entities_for_read

router = Router()


@router.get("/compounds/", response=EnhancedPageNumberPagination.Output[CompoundOutSchema])
@paginate(
    EnhancedPageNumberPagination,
    page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE,
    filter_schema=ReviewStatusFilter,
)
def list_all_compounds(request):
    """
    List all compounds from reviewed packages.
    """
    return get_user_entities_for_read(Compound, request.user).order_by("name").all()


@router.get(
    "/package/{uuid:package_uuid}/compound/",
    response=EnhancedPageNumberPagination.Output[CompoundOutSchema],
)
@paginate(
    EnhancedPageNumberPagination,
    page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE,
    filter_schema=ReviewStatusFilter,
)
def list_package_compounds(request, package_uuid: UUID):
    """
    List all compounds for a specific package.
    """
    user = request.user
    return get_package_entities_for_read(Compound, package_uuid, user).order_by("name").all()
