<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Chapter;
use App\Models\Course;
use Illuminate\Http\Request;

class ChapterController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $courseId = $request->course_id ?? '';
        if ($request->ajax()) {
            if (!empty($courseId)) {
                $query = Chapter::where('course_id', $courseId)->with('course')->get();
            } else {
                $query = Chapter::with('course')->get();
            }
            return datatables($query)->toJson();
        }
        return view('admin.chapter.index');
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $courseId = request('course_id');
        if (!empty($courseId)) {
            $course = Course::with('category')->find($courseId);
            return view('admin.chapter.create', compact('course'));
        } else {
            $categories = Category::get();
            return view('admin.chapter.create', compact('categories'));
        }
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // return $request->all();
        $attributes = $request->validate([
            'title' => 'required|string|max:255',
            'category_id' => 'required|exists:categories,id',
            'course_id' => 'required|exists:courses,id',
        ]);

        Chapter::create($attributes);
        $courseId = $request->course_id ?? '';

        return to_route('chapters.index', ['course_id' => $courseId])->with('success', __('trans.alert.success.done_create', ['name' => 'Chapter']));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Chapter $chapter)
    {
        // $categories = Category::get();
        // return view('admin.chapter.edit', compact('categories', 'chapter'));
        $course = Course::with('category:id,name')->find($chapter->course_id);
        return view('admin.chapter.edit', compact('course', 'chapter'));

    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Chapter $chapter)
    {
        $attributes = $request->validate([
            'title' => 'required|string|max:255',
            'category_id' => 'required|exists:categories,id',
            'course_id' => 'required|exists:courses,id',
        ]);

        $chapter->update($attributes);
        $courseId = $request->course_id ?? '';

        return to_route('chapters.index', ['course_id' => $courseId])->with('success', __('trans.alert.success.done_update', ['name' => 'Chapter']));
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Chapter $chapter)
    {
        $courseId = $chapter->course_id ?? '';
        $chapter->delete();
        return to_route('chapters.index', ['course_id' => $courseId])->with('success', __('trans.alert.success.done_delete', ['name' => 'Chapter']));
    }


    public function ajaxChapters(Request $request)
    {
        $chapters = Chapter::where('course_id', $request->course_id)
            ->select('id', 'title')
            ->get();

        return response()->json(['data' => $chapters]);
    }
}
