<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\Course;
use App\Models\Admin\Staff;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class CoursesController extends Controller
{
    public function addCourse(): View
    {
        $staffs = Staff::all();

        return view('admin.add-course', [
            'staffs'=>$staffs,
        ]);
    }

    public function storeCourse(Request $request): RedirectResponse
    {

//        dd($request);

        $course = new Course();

        $request->validate([
            'title' => 'required',
            'tutor' => 'required',
            'caption' => 'required',
            'description' => 'required',
            'icon' => 'required',
            'path' => 'required',
            'order' => 'required',
        ]);

        // Determine the values of active and featured based on the checkbox status
        $active = $request->has('active') ? 1 : 0;


        $course->title = $request->title;
        $course->tutor = $request->tutor;
        $course->caption = $request->caption;
        $course->description = $request->description;
        $course->icon = $request->icon;
        $course->active = $active;
        $course->image = $request->path;
        $course->order = $request->order;
        $course->slug = strtolower(str_replace(' ', '-', $request->title));

        $course->save();

        return redirect()->route('list-courses')->with('success', 'Course created successfully');

    }


    public function listCourses(): View
    {
        $courses = Course::all();



        return view('admin.list-courses', [
            'courses' => $courses,
        ]);
    }


    public function editCourse($id): View
    {
        $staffs = Staff::all();
        $course = Course::findOrFail($id);


        return view('admin.edit-course', [
            'course' => $course,
            'staffs'=>$staffs,
        ]);
    }


    public function updateCourse(Request $request): RedirectResponse
    {

//        dd($request);

        $request->validate([
            'title' => 'required',
            'tutor' => 'required',
            'caption' => 'required',
            'description' => 'required',
            'icon' => 'required',
            'path' => 'required',
            'order' => 'required',
        ]);

        $id = $request->id;

        // Determine the values of active and featured based on the checkbox status
        $active = $request->has('active') ? 1 : 0;

        Course::findOrFail($id)->update([
            'title' => $request->title,
            'tutor' => $request->tutor,
            'caption' => $request->caption,
            'description' => $request->description,
            'icon' => $request->icon,
            'active' => $active,
            'image' => $request->path,
            'order' => $request->order,
            'slug' => strtolower(str_replace(' ', '-', $request->title)),
        ]);

        return redirect()->route('list-courses')->with('success', 'Course updated successfully');

    }

    public function deleteCourse($id): RedirectResponse
    {
        Course::findOrFail($id)->delete();
        return redirect()->route('list-courses')->with('success', 'Course deleted successfully');

    }

}
