# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Custom routers for the server API."""

from typing import override

from rest_framework.routers import DefaultRouter
from rest_framework.viewsets import ViewSetMixin


class APIRouter(DefaultRouter):
    """A router that can handle looking up objects by multiple fields."""

    @override
    def get_lookup_regex(
        self, viewset: type[ViewSetMixin], lookup_prefix: str = ""
    ) -> str:
        if hasattr(viewset, "get_lookup_regex"):
            assert getattr(self, "_use_regex")
            lookup_regex = viewset.get_lookup_regex(lookup_prefix=lookup_prefix)
            assert isinstance(lookup_regex, str)
            return lookup_regex
        else:
            return super().get_lookup_regex(
                viewset, lookup_prefix=lookup_prefix
            )
