from .models import *
from .serializers import *

from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes
from rest_framework.decorators import permission_classes
import json


@api_view(['GET'])
def all_workouts(request):
    users = Workout.objects.all()
    serializer = WorkoutSerializer(users, many=True)
    return Response(data=serializer.data, status=status.HTTP_200_OK)


@api_view(['GET'])
def workout_details(request, workout_id):
    users = Workout.objects.get(pk=workout_id)
    serializer = WorkoutDetailSerializer(users, many=False)
    return Response(data=serializer.data, status=status.HTTP_200_OK)

@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def complete_workout(request):
    registered_user = RegisteredUser.objects.get(user=request.user)    
    serializer = CompleteWorkoutSerializer(data=request.data, context = {"registered_user":registered_user.pk})

    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

@api_view(['GET'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def workouts_completed_by_logged_in_user(request):
    registered_user = RegisteredUser.objects.get(user=request.user)    
    workouts = CompletedWorkout.objects.filter(registered_user=registered_user)
    serializer = GetCompleteWorkoutSerializer(workouts, many=True)
    return Response(serializer.data, status=status.HTTP_201_CREATED)

@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def add_workout(request):
    registered_user = RegisteredUser.objects.get(user=request.user)   
    if registered_user is None:
        return Response(status=status.HTTP_400_BAD_REQUEST)

    # exercise_data = dict(request.POST)["exercise_data"]
    exercise_data = request.data["exercise_data"]
    print(exercise_data)

    if exercise_data is None:
        return Response({"exercise_data": [ "missing" ] }, status=status.HTTP_400_BAD_REQUEST)
    if len(exercise_data) < 1:
        return Response({"exercise_data": [ "empty" ] }, status=status.HTTP_400_BAD_REQUEST)

    serializer = POSTCompleteWorkoutSerializer(data=request.data)

    if serializer.is_valid():
        new_obj = serializer.save(registered_user=registered_user)
        new_obj.save()

        for data in exercise_data:
            print(data)
            id = data["id"]
            if id is None:
                continue
      
            exercise = Exercise.objects.get(pk=id)
            completed_workout = WorkoutExercise.objects.create(
                workout=new_obj, 
                exercise=exercise
            )
            if "weight" in data:
                completed_workout.weight = data["weight"]
            if "reps" in data:
                completed_workout.reps = data["reps"]
            completed_workout.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)