from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import Student, StudentProgress
from schedule.models import Lesson, LessonEnrollment
from .forms import StudentProfileForm

@login_required
def student_dashboard(request):
    student = get_object_or_404(Student, profile__user=request.user)
    enrollments = LessonEnrollment.objects.filter(student=student).select_related('lesson')
    return render(request, 'student/dashboard.html', {
        'student': student,
        'enrollments': enrollments
    })

@login_required
def student_schedule(request):
    student = get_object_or_404(Student, profile__user=request.user)
    enrollments = LessonEnrollment.objects.filter(student=student).select_related('lesson')
    return render(request, 'student/schedule.html', {
        'student': student,
        'enrollments': enrollments
    })

@login_required
def student_progress(request):
    student = get_object_or_404(Student, profile__user=request.user)
    progress = StudentProgress.objects.get_or_create(student=student)[0]
    return render(request, 'student/progress.html', {
        'student': student,
        'progress': progress
    })

@login_required
def student_profile(request):
    student = get_object_or_404(Student, profile__user=request.user)
    return render(request, 'student/profile.html', {'student': student})

@login_required
def edit_student_profile(request):
    student = get_object_or_404(Student, profile__user=request.user)
    if request.method == 'POST':
        form = StudentProfileForm(request.POST, request.FILES, instance=student)
        if form.is_valid():
            form.save()
            messages.success(request, 'Профиль успешно обновлен!')
            return redirect('student_profile')
    else:
        form = StudentProfileForm(instance=student)
    return render(request, 'student/edit_profile.html', {'form': form})

@login_required
def lesson_list(request):
    student = get_object_or_404(Student, profile__user=request.user)
    enrollments = LessonEnrollment.objects.filter(student=student).select_related('lesson')
    return render(request, 'student/lesson_list.html', {
        'student': student,
        'enrollments': enrollments
    })

@login_required
def lesson_detail(request, pk):
    student = get_object_or_404(Student, profile__user=request.user)
    lesson = get_object_or_404(Lesson, pk=pk)
    enrollment = get_object_or_404(LessonEnrollment, student=student, lesson=lesson)
    return render(request, 'student/lesson_detail.html', {
        'student': student,
        'lesson': lesson,
        'enrollment': enrollment
    })

def student_list(request):
    students = Student.objects.all().order_by('user__last_name', 'user__first_name')
    return render(request, 'student/student_list.html', {'students': students})

@login_required
def student_detail(request, pk):
    student = get_object_or_404(Student, pk=pk)
    enrolled_lessons = Lesson.objects.filter(enrolled_students=student)
    return render(request, 'student/student_detail.html', {
        'student': student,
        'enrolled_lessons': enrolled_lessons
    })

@login_required
def student_schedule(request, pk):
    student = get_object_or_404(Student, pk=pk)
    enrolled_lessons = Lesson.objects.filter(enrolled_students=student).order_by('date', 'start_time')
    return render(request, 'student/student_schedule.html', {
        'student': student,
        'enrolled_lessons': enrolled_lessons
    }) 