"""Public (careers portal) URL group — mounted at /api/v1/public/."""

from django.urls import path
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView

from core.responses import success_response
from apps.jobs.public_views import PublicJobListView, PublicJobDetailView
from apps.pipeline.apply_views import CandidateApplyView, MyApplicationsView


class PublicSkillsView(APIView):
    """GET /api/v1/public/skills/ — active skill names for the careers portal."""
    permission_classes = [AllowAny]
    authentication_classes = []

    def get(self, request):
        from apps.master_skills.models import MasterSkill
        qs = MasterSkill.objects.all().order_by("name")
        if hasattr(MasterSkill, "is_active"):
            qs = qs.filter(is_active=True)
        return success_response([{"id": s.id, "name": s.name} for s in qs])


class PublicEducationsView(APIView):
    """GET /api/v1/public/educations/ — active qualifications for the portal."""
    permission_classes = [AllowAny]
    authentication_classes = []

    def get(self, request):
        from apps.master_data.models import Education
        qs = Education.objects.filter(is_active=True).order_by("sort_order", "name")
        return success_response([{"id": e.id, "name": e.name} for e in qs])


class PublicLanguagesView(APIView):
    """GET /api/v1/public/languages/ — active languages for the careers portal."""
    permission_classes = [AllowAny]
    authentication_classes = []

    def get(self, request):
        from apps.master_data.models import Language
        qs = Language.objects.filter(is_active=True).order_by("sort_order", "name")
        return success_response([{"id": l.id, "name": l.name} for l in qs])


class PublicDesignationsView(APIView):
    """GET /api/v1/public/designations/ — active designation names for the careers portal."""
    permission_classes = [AllowAny]
    authentication_classes = []

    def get(self, request):
        from apps.master_designations.models import MasterDesignation
        qs = MasterDesignation.objects.filter(status="ACTIVE").order_by("name")
        return success_response([{"id": d.id, "name": d.name} for d in qs])


class PublicNoticePeriodsView(APIView):
    """GET /api/v1/public/notice-periods/ — active notice periods for the portal / candidate forms."""
    permission_classes = [AllowAny]
    authentication_classes = []

    def get(self, request):
        from apps.master_data.models import NoticePeriod
        qs = NoticePeriod.objects.filter(is_active=True).order_by("sort_order", "value")
        return success_response([{"id": n.id, "value": n.value, "label": n.label} for n in qs])


class PublicAnnualCTCView(APIView):
    """GET /api/v1/public/annual-ctc/ — active annual CTC bands for the portal / candidate forms."""
    permission_classes = [AllowAny]
    authentication_classes = []

    def get(self, request):
        from apps.master_data.models import AnnualCTC
        qs = AnnualCTC.objects.filter(is_active=True).order_by("sort_order", "value")
        return success_response([{"id": c.id, "value": c.value, "label": c.label} for c in qs])


urlpatterns = [
    path("jobs/", PublicJobListView.as_view(), name="public-jobs"),
    path("jobs/<str:pk>/", PublicJobDetailView.as_view(), name="public-job-detail"),
    path("apply/", CandidateApplyView.as_view(), name="public-apply"),
    path("my-applications/", MyApplicationsView.as_view(), name="public-my-applications"),
    path("skills/", PublicSkillsView.as_view(), name="public-skills"),
    path("educations/", PublicEducationsView.as_view(), name="public-educations"),
    path("languages/", PublicLanguagesView.as_view(), name="public-languages"),
    path("designations/", PublicDesignationsView.as_view(), name="public-designations"),
    path("notice-periods/", PublicNoticePeriodsView.as_view(), name="public-notice-periods"),
    path("annual-ctc/", PublicAnnualCTCView.as_view(), name="public-annual-ctc"),
]
