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

from ..pagination import EnhancedPageNumberPagination
from ..schemas import CompoundStructureOutSchema, StructureReviewStatusFilter
from ..dal import (
    get_user_structure_for_read,
    get_package_compound_structure_for_read,
)

router = Router()


@router.get(
    "/structures/", response=EnhancedPageNumberPagination.Output[CompoundStructureOutSchema]
)
@paginate(
    EnhancedPageNumberPagination,
    page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE,
    filter_schema=StructureReviewStatusFilter,
)
def list_all_structures(request):
    """
    List all structures from all packages.
    """
    user = request.user
    return get_user_structure_for_read(user).order_by("name").all()


@router.get(
    "/package/{uuid:package_uuid}/compound/{uuid:compound_uuid}/structure/",
    response=EnhancedPageNumberPagination.Output[CompoundStructureOutSchema],
)
@paginate(
    EnhancedPageNumberPagination,
    page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE,
    filter_schema=StructureReviewStatusFilter,
)
def list_package_structures(request, package_uuid: UUID, compound_uuid: UUID):
    """
    List all structures for a specific package and compound.
    """
    user = request.user
    return (
        get_package_compound_structure_for_read(package_uuid, compound_uuid, user)
        .order_by("name")
        .all()
    )
