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 Instructor, InstructorSchedule
from schedule.models import Lesson, LessonEnrollment
from .forms import InstructorScheduleForm, InstructorProfileForm, InstructorForm
from student.models import Student

@login_required
def instructor_dashboard(request):
    instructor = get_object_or_404(Instructor, profile__user=request.user)
    lessons = Lesson.objects.filter(instructor=instructor).order_by('start_time')
    return render(request, 'instructor/dashboard.html', {
        'instructor': instructor,
        'lessons': lessons
    })

@login_required
def instructor_schedule(request, pk):
    instructor = get_object_or_404(Instructor, pk=pk)
    lessons = instructor.lessons.all().order_by('date', 'start_time')
    return render(request, 'instructor/instructor_schedule.html', {'instructor': instructor, 'lessons': lessons})

@login_required
def edit_schedule(request):
    instructor = get_object_or_404(Instructor, profile__user=request.user)
    if request.method == 'POST':
        form = InstructorScheduleForm(request.POST)
        if form.is_valid():
            schedule = form.save(commit=False)
            schedule.instructor = instructor
            schedule.save()
            messages.success(request, 'Расписание успешно обновлено!')
            return redirect('instructor_schedule')
    else:
        form = InstructorScheduleForm()
    return render(request, 'instructor/edit_schedule.html', {'form': form})

@login_required
def student_list(request):
    instructor = get_object_or_404(Instructor, profile__user=request.user)
    enrollments = LessonEnrollment.objects.filter(lesson__instructor=instructor).select_related('student')
    students = {enrollment.student for enrollment in enrollments}
    return render(request, 'instructor/student_list.html', {
        'instructor': instructor,
        'students': students
    })

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

@login_required
def instructor_profile(request):
    if not hasattr(request.user, 'instructor'):
        messages.error(request, 'У вас нет прав для просмотра профиля инструктора.')
        return redirect('home')
    
    if request.method == 'POST':
        form = InstructorForm(request.POST, instance=request.user.instructor)
        if form.is_valid():
            form.save()
            messages.success(request, 'Профиль успешно обновлен.')
            return redirect('instructor:profile')
    else:
        form = InstructorForm(instance=request.user.instructor)
    
    return render(request, 'instructor/instructor_profile.html', {'form': form})

def instructor_list(request):
    instructors = Instructor.objects.all()
    return render(request, 'instructor/instructor_list.html', {'instructors': instructors})

@login_required
def instructor_detail(request, pk):
    instructor = get_object_or_404(Instructor, pk=pk)
    return render(request, 'instructor/instructor_detail.html', {'instructor': instructor}) 