<?php

namespace App\Http\Controllers;

use App\Models\Enrollment;
use App\Models\Student;
use App\Models\TrainingClass;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class EnrollmentController extends Controller
{
    public function index(): View
    {
        $enrollments = Enrollment::with(['student', 'trainingClass.course'])->latest()->get();
        return view('enrollments.index', compact('enrollments'));
    }

    public function create(): View
    {
        $classes = TrainingClass::with('course')->latest()->get();
        $students = Student::latest()->get();
        return view('enrollments.create', compact('classes', 'students'));
    }

    public function store(Request $request): RedirectResponse
    {
        $request->validate([
            'training_class_id' => 'required|exists:training_classes,id',
            'student_id' => 'required|exists:students,id',
            'register_date' => 'nullable|date',
            'status' => 'required|max:100',
        ]);

        $exists = Enrollment::where('training_class_id', $request->training_class_id)
            ->where('student_id', $request->student_id)
            ->exists();

        if ($exists) {
            return back()->withInput()->withErrors(['student_id' => 'Học viên này đã được đăng ký vào lớp học đã chọn.']);
        }

        Enrollment::create($request->only(['training_class_id', 'student_id', 'register_date', 'status', 'note']));
        return redirect()->route('enrollments.index')->with('success', 'Đã đăng ký học viên vào lớp thành công.');
    }

    public function show(Enrollment $enrollment): View
    {
        $enrollment->load(['student', 'trainingClass.course']);
        return view('enrollments.show', compact('enrollment'));
    }

    public function edit(Enrollment $enrollment): View
    {
        $classes = TrainingClass::with('course')->latest()->get();
        $students = Student::latest()->get();
        return view('enrollments.edit', compact('enrollment', 'classes', 'students'));
    }

    public function update(Request $request, Enrollment $enrollment): RedirectResponse
    {
        $request->validate([
            'training_class_id' => 'required|exists:training_classes,id',
            'student_id' => 'required|exists:students,id',
            'register_date' => 'nullable|date',
            'status' => 'required|max:100',
        ]);

        $exists = Enrollment::where('training_class_id', $request->training_class_id)
            ->where('student_id', $request->student_id)
            ->where('id', '<>', $enrollment->id)
            ->exists();

        if ($exists) {
            return back()->withInput()->withErrors(['student_id' => 'Học viên này đã được đăng ký vào lớp học đã chọn.']);
        }

        $enrollment->update($request->only(['training_class_id', 'student_id', 'register_date', 'status', 'note']));
        return redirect()->route('enrollments.index')->with('success', 'Đã cập nhật đăng ký học thành công.');
    }

    public function destroy(Enrollment $enrollment): RedirectResponse
    {
        $enrollment->delete();
        return redirect()->route('enrollments.index')->with('success', 'Đã xóa đăng ký học thành công.');
    }
}
