<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Course;

class CourseController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $courses = Course::where('status', 'published')
            ->with('teacher')
            ->paginate(12);

        return view('courses.index', compact('courses'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(Course $course)
    {
        if ($course->status !== 'published') {
            abort(404);
        }

        $course->load(['teacher', 'sessions' => function($query) {
            $query->where('is_published', true)->orderBy('session_number');
        }]);

        $isEnrolled = false;
        $enrollment = null;

        if (auth()->check()) {
            $enrollment = $course->enrollments()
                ->where('user_id', auth()->id())
                ->first();
            $isEnrolled = $enrollment !== null;
        }

        return view('courses.show', compact('course', 'isEnrolled', 'enrollment'));
    }

    /**
     * Show a specific session of a course
     */
    public function showSession(Course $course, $sessionNumber)
    {
        // Check if user is logged in
        if (!auth()->check()) {
            return redirect()->route('login')->with('error', 'Please login to access course sessions.');
        }

        // Find the session by session number
        $session = $course->sessions()->where('session_number', $sessionNumber)->firstOrFail();

        // Check if user is enrolled
        $isEnrolled = $course->enrollments()
            ->where('user_id', auth()->id())
            ->where('status', 'enrolled')
            ->exists();

        // Require enrollment to access sessions
        if (!$isEnrolled) {
            return redirect()->route('courses.show', $course->slug)->with('error', 'Please enroll in this course to access sessions.');
        }

        // Get previous and next sessions
        $previousSession = $course->sessions()
            ->where('session_number', '<', $sessionNumber)
            ->orderBy('session_number', 'desc')
            ->first();

        $nextSession = $course->sessions()
            ->where('session_number', '>', $sessionNumber)
            ->orderBy('session_number', 'asc')
            ->first();

        return view('courses.session', compact('course', 'session', 'previousSession', 'nextSession', 'isEnrolled'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
