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 Rule
from ..pagination import EnhancedPageNumberPagination
from ..schemas import ReviewStatusFilter, RuleOutSchema
from ..dal import get_user_entities_for_read, get_package_entities_for_read

router = Router()


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


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