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

from ..auth import OptionalBearerTokenAuth
from ..dal import get_user_packages_for_read
from ..pagination import EnhancedPageNumberPagination
from ..schemas import PackageOutSchema, SelfReviewStatusFilter

router = Router()
logger = logging.getLogger(__name__)


@router.get(
    "/packages/",
    response=EnhancedPageNumberPagination.Output[PackageOutSchema],
    auth=OptionalBearerTokenAuth(),
)
@paginate(
    EnhancedPageNumberPagination,
    page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE,
    filter_schema=SelfReviewStatusFilter,
)
def list_all_packages(request):
    """
    List packages accessible to the user.

    """
    user = request.user
    qs = get_user_packages_for_read(user)
    return qs.order_by("name").all()
