"""
URL configuration for keinasystem project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from apps.fields.views import OfficialKyosaiFieldViewSet, OfficialChusankanFieldViewSet
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated


class ChangePasswordView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request):
        user = request.user
        current_password = request.data.get('current_password', '')
        new_password = request.data.get('new_password', '')
        if not current_password or not new_password:
            return Response({'error': '現在のパスワードと新しいパスワードを入力してください'}, status=status.HTTP_400_BAD_REQUEST)
        if not user.check_password(current_password):
            return Response({'error': '現在のパスワードが正しくありません'}, status=status.HTTP_400_BAD_REQUEST)
        if len(new_password) < 8:
            return Response({'error': 'パスワードは8文字以上にしてください'}, status=status.HTTP_400_BAD_REQUEST)
        user.set_password(new_password)
        user.save()
        return Response({'status': 'ok'})

master_router = DefaultRouter()
master_router.register(r'kyosai-fields', OfficialKyosaiFieldViewSet, basename='kyosai-field')
master_router.register(r'chusankan-fields', OfficialChusankanFieldViewSet, basename='chusankan-field')

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/fields/', include('apps.fields.urls')),
    path('api/', include(master_router.urls)),
    path('api/plans/', include('apps.plans.urls')),
    path('api/reports/', include('apps.reports.urls')),
    path('api/auth/jwt/create/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/auth/jwt/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('api/auth/change-password/', ChangePasswordView.as_view(), name='change-password'),
    path('api/mail/', include('apps.mail.urls')),
    path('api/weather/', include('apps.weather.urls')),
    path('api/fertilizer/', include('apps.fertilizer.urls')),
    path('api/materials/', include('apps.materials.urls')),
    path('api/workrecords/', include('apps.workrecords.urls')),
    path('api/levee-work/', include('apps.levee_work.urls')),
]
