import logging

from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
# from django.core.checks import messages
from django.http import JsonResponse
import json

from accounts.models import Customers
from django.views.decorators.csrf import csrf_exempt


logger = logging.getLogger("account_logger")

@csrf_exempt
def change_customer_password(request):
    print("in change password for customer api change cust password")
    if request.method != 'POST':
        logger.warning(f"Invalid request method: {request.method}")
        # messages.error(request, "Invalid request method.", extra_tags="danger")
        return JsonResponse({'error': 'Invalid request method.'}, status=405)

    data = request.body
    data = json.loads(data)
    print(data)
    user_id = data.get('user_id')
    new_password = data.get('new_password')
    confirm_password = data.get('confirm_password')
    current_password = data.get('current_password')

    if new_password == confirm_password:
        print("user_id", user_id)
        print("new_password", new_password)
        if not user_id or not new_password:
            logger.warning("Missing user_id or new_password in the request.")
            # messages.error(request, "Missing user_id or new_password.", extra_tags="danger")
            return JsonResponse({'error': 'Missing user_id or new_password.'}, status=400)

        try:
            user = Customers.objects.get(id=user_id)
            print(user.email)
            print(user.check_password(current_password),"user.check_password(current_password)")
            # if authenticate(request, email=user.email, password=current_password):
            if user and user.check_password(current_password):
                user.set_password(new_password)
                user.save()
                logger.info(f"Password for user {user.first_name} changed successfully")
            else:
                # messages.error(request, "Current password do not match with database", extra_tags="danger")
                return JsonResponse({'error': 'Incorrect Current password, Please try again'}, status=404)

        except Customers.DoesNotExist:
            logger.error(f"User with id {user_id} does not exist.")
            # messages.error(request, "User does not exist.", extra_tags="danger")
            return JsonResponse({'error': 'User does not exist.'}, status=404)
        except Exception as e:
            logger.error(f"Error changing password: {str(e)}")
            # messages.error(request, "Error changing password.", extra_tags="danger")
            return JsonResponse({'error': 'Error changing password.'}, status=500)

        # messages.error(request, "Password changed successfully.", extra_tags="success")
        return JsonResponse({'success': 'Password changed successfully.'})
    else:
        # messages.error(request, "Passwords do not match.", extra_tags="danger")
        return JsonResponse({'Failed': 'Passwords do not match.'})
