from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login as auth_login
from .models import Course, Module, Lesson, UserProgress
from .forms import UserUpdateForm, ProfileUpdateForm, CourseForm, ModuleForm, LessonForm

def index(request):
    courses = Course.objects.all()
    return render(request, 'core/index.html', {'courses': courses})

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth_login(request, user)
            messages.success(request, 'Registration successful!')
            return redirect('index')
    else:
        form = UserCreationForm()
    return render(request, 'registration/signup.html', {'form': form})

def course_detail(request, pk):
    course = get_object_or_404(Course, pk=pk)
    return render(request, 'core/course_detail.html', {'course': course})

def lesson_detail(request, pk):
    lesson = get_object_or_404(Lesson, pk=pk)
    course = lesson.module.course
    
    if request.user.is_authenticated:
        UserProgress.objects.get_or_create(user=request.user, lesson=lesson)
    
    return render(request, 'core/lesson_detail.html', {'lesson': lesson, 'course': course})

@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    user_progress = UserProgress.objects.filter(user=request.user)
    completed_lessons = [progress.lesson for progress in user_progress]

    context = {
        'u_form': u_form,
        'p_form': p_form,
        'completed_lessons': completed_lessons
    }

    return render(request, 'core/profile.html', context)

@login_required
def dashboard(request):
    authored_courses = Course.objects.filter(author=request.user)
    return render(request, 'core/dashboard.html', {'courses': authored_courses})

@login_required
def course_create(request):
    if request.method == 'POST':
        form = CourseForm(request.POST, request.FILES)
        if form.is_valid():
            course = form.save(commit=False)
            course.author = request.user
            course.save()
            messages.success(request, 'Course created successfully!')
            return redirect('dashboard')
    else:
        form = CourseForm()
    return render(request, 'core/course_form.html', {'form': form, 'title': 'Create Course'})

@login_required
def course_edit(request, pk):
    course = get_object_or_404(Course, pk=pk, author=request.user)
    if request.method == 'POST':
        form = CourseForm(request.POST, request.FILES, instance=course)
        if form.is_valid():
            form.save()
            messages.success(request, 'Course updated successfully!')
            return redirect('dashboard')
    else:
        form = CourseForm(instance=course)
    return render(request, 'core/course_form.html', {'form': form, 'title': 'Edit Course'})

@login_required
def course_delete(request, pk):
    course = get_object_or_404(Course, pk=pk, author=request.user)
    if request.method == 'POST':
        course.delete()
        messages.success(request, 'Course deleted successfully!')
        return redirect('dashboard')
    return render(request, 'core/course_confirm_delete.html', {'object': course})

@login_required
def module_create(request, course_pk):
    course = get_object_or_404(Course, pk=course_pk, author=request.user)
    if request.method == 'POST':
        form = ModuleForm(request.POST)
        if form.is_valid():
            module = form.save(commit=False)
            module.course = course
            module.save()
            messages.success(request, 'Module added successfully!')
            return redirect('course_edit', pk=course.pk)
    else:
        form = ModuleForm()
    return render(request, 'core/module_form.html', {'form': form, 'course': course})

@login_required
def module_edit(request, pk):
    module = get_object_or_404(Module, pk=pk, course__author=request.user)
    if request.method == 'POST':
        form = ModuleForm(request.POST, instance=module)
        if form.is_valid():
            form.save()
            messages.success(request, 'Module updated successfully!')
            return redirect('course_edit', pk=module.course.pk)
    else:
        form = ModuleForm(instance=module)
    return render(request, 'core/module_form.html', {'form': form, 'course': module.course})

@login_required
def lesson_create(request, module_pk):
    module = get_object_or_404(Module, pk=module_pk, course__author=request.user)
    if request.method == 'POST':
        form = LessonForm(request.POST)
        if form.is_valid():
            lesson = form.save(commit=False)
            lesson.module = module
            lesson.save()
            messages.success(request, 'Lesson added successfully!')
            return redirect('course_edit', pk=module.course.pk)
    else:
        form = LessonForm()
    return render(request, 'core/lesson_form.html', {'form': form, 'module': module})

@login_required
def lesson_edit(request, pk):
    lesson = get_object_or_404(Lesson, pk=pk, module__course__author=request.user)
    if request.method == 'POST':
        form = LessonForm(request.POST, instance=lesson)
        if form.is_valid():
            form.save()
            messages.success(request, 'Lesson updated successfully!')
            return redirect('course_edit', pk=lesson.module.course.pk)
    else:
        form = LessonForm(instance=lesson)
    return render(request, 'core/lesson_form.html', {'form': form, 'module': lesson.module})