from django.urls import path, include
from rest_framework.routers import DefaultRouter

# Authentication views
from .views import (
    MasterAdminLoginView,
    MyTokenObtainPairView,
    RefreshTokenView,
    LogoutView,
    CurrentUserDetailView,
)

# School management views
from .school_views import (
    SchoolViewSet,
    SchoolTypeViewSet,
    SchoolBoardViewSet,
    SchoolModuleViewSet,
    LanguageViewSet,
    SchoolCreateView,
    SchoolDetailView,
)
from .views import (
    EmailTemplateViewSet,
    BulkEmailLogViewSet,
    SendBulkEmailView,
)
from .legal_views import LegalDocumentAdminView
from .user_management_views import CreateUserView, UpdateUserView
from .credential_views import ApiCredentialListView, ApiCredentialUpdateView

# Location management views
from .location_views import (
    CountryViewSet,
    StateViewSet,
    CityViewSet,
    AreaViewSet,
    PincodeViewSet,
    CountryListView,
    StateListView,
    CityListView,
    AreaListView,
    PincodeListView,
    CountryStatesView,
    StateCitiesView,
    CityAreasView,
    AreaPincodesView,
)

# ==========================================
# CREATE ALL ROUTERS FIRST
# ==========================================

# Router for school management
school_router = DefaultRouter()
school_router.register(r"schools", SchoolViewSet, basename="school")
school_router.register(r"school-types", SchoolTypeViewSet, basename="school-type")
school_router.register(r"school-boards", SchoolBoardViewSet, basename="school-board")
school_router.register(r"school-modules", SchoolModuleViewSet, basename="school-module")
school_router.register(r"languages", LanguageViewSet, basename="language")

# Router for location CRUD operations
location_crud_router = DefaultRouter()
location_crud_router.register(r"countries", CountryViewSet, basename="country-crud")
location_crud_router.register(r"states", StateViewSet, basename="state-crud")
location_crud_router.register(r"cities", CityViewSet, basename="city-crud")
location_crud_router.register(r"areas", AreaViewSet, basename="area-crud")
location_crud_router.register(r"pincodes", PincodeViewSet, basename="pincode-crud")
# Register ViewSets
router = DefaultRouter()
router.register(r"templates", EmailTemplateViewSet, basename="email-template")
router.register(r"logs", BulkEmailLogViewSet, basename="email-log")
# ==========================================
# URL PATTERNS
# ==========================================

urlpatterns = [
    # ========================
    # AUTHENTICATION ENDPOINTS
    # ========================
    path("login/", MasterAdminLoginView.as_view(), name="token_obtain_pair"),
    path("token/", MyTokenObtainPairView.as_view(), name="token_obtain_pair_cookie"),
    path("token/refresh/", RefreshTokenView.as_view(), name="token_refresh_cookie"),
    path("logout/", LogoutView.as_view(), name="logout"),
    # ========================
    # SCHOOL MANAGEMENT ENDPOINTS
    # ========================
    path("schools/new/", SchoolCreateView.as_view(), name="school-create"),
    path("schools/<int:pk>/detail/", SchoolDetailView.as_view(), name="school-detail"),
    # School Actions (Add these endpoints)
    path(
        "schools/<int:pk>/verify/",
        SchoolViewSet.as_view({"post": "verify"}),
        name="school-verify",
    ),
    path(
        "schools/<int:pk>/activate/",
        SchoolViewSet.as_view({"post": "activate"}),
        name="school-activate",
    ),
    path(
        "schools/<int:pk>/deactivate/",
        SchoolViewSet.as_view({"post": "deactivate"}),
        name="school-deactivate",
    ),
    path(
        "schools/<int:pk>/extend-trial/",
        SchoolViewSet.as_view({"post": "extend_trial"}),
        name="school-extend-trial",
    ),
    path(
        "schools/<int:pk>/modules/",
        SchoolViewSet.as_view({"get": "modules", "post": "modules"}),
        name="school-modules",
    ),
    path(
        "schools/<int:pk>/delete/",
        SchoolViewSet.as_view({"delete": "destroy"}),
        name="school-delete",
    ),
    # ========================
    # LOCATION ENDPOINTS
    # ========================
    # OPTION 1: SIMPLE CRUD ENDPOINTS
    path("locations/", include(location_crud_router.urls)),
    # OPTION 2: READ-ONLY LIST ENDPOINTS
    path("locations/lists/countries/", CountryListView.as_view(), name="country-list"),
    path("locations/lists/states/", StateListView.as_view(), name="state-list"),
    path("locations/lists/cities/", CityListView.as_view(), name="city-list"),
    path("locations/lists/areas/", AreaListView.as_view(), name="area-list"),
    path("locations/lists/pincodes/", PincodeListView.as_view(), name="pincode-list"),
    # OPTION 3: NESTED HIERARCHICAL ENDPOINTS
    path(
        "locations/countries/<str:country_code>/states/",
        CountryStatesView.as_view(),
        name="country-states",
    ),
    path(
        "locations/states/<int:state_id>/cities/",
        StateCitiesView.as_view(),
        name="state-cities",
    ),
    path(
        "locations/cities/<int:city_id>/areas/",
        CityAreasView.as_view(),
        name="city-areas",
    ),
    path(
        "locations/areas/<int:area_id>/pincodes/",
        AreaPincodesView.as_view(),
        name="area-pincodes",
    ),
    # ========================
    # INCLUDE SCHOOL ROUTER (MUST BE LAST)
    # ========================
    path("legal-documents/", LegalDocumentAdminView.as_view(), name="legal-documents-admin"),
    path("me/", CurrentUserDetailView.as_view(), name="current-user-detail"),
    path("create-user/", CreateUserView.as_view(), name="create-user"),
    path("update-user/<int:pk>/", UpdateUserView.as_view(), name="update-user"),
    path("credentials/", ApiCredentialListView.as_view(), name="credentials-list"),
    path("credentials/<int:pk>/", ApiCredentialUpdateView.as_view(), name="credentials-update"),
    path("", include(school_router.urls)),
    path("bulk-email/send/", SendBulkEmailView.as_view(), name="bulk-email-send"),
path("bulk-email/", include(router.urls)),
]
