<?php

namespace App\Http\Controllers\Admin;

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

class UnitController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $chapterId = $request->chapter_id ?? '';
        if ($request->ajax()) {
            if (!empty($chapterId)) {
                $query = Unit::where('chapter_id', $chapterId)->with(['chapter', 'course', 'category'])->get();
            } else {
                $query = Unit::with(['chapter', 'course', 'category'])->get();
            }

            return datatables($query)->toJson();
        }
        return view('admin.unit.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $chapterId = request('chapter_id');
        if (!empty($chapterId)) {
            $existedChapter = Chapter::with('course:id,title', 'category:id,name')->find($chapterId);
            // return $existedChapter;
            return view('admin.unit.create', compact('existedChapter'));
        } else {
            $categories = Category::get();
            return view('admin.unit.create', compact('categories'));
        }
        // $chapters = Chapter::get();
        // $courses = Course::get();
        // $categories = Category::get();
        // return view('admin.unit.create', compact('chapters', 'courses', 'categories'));
        // return view('admin.unit.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',
            'chapter_id' => 'required|exists:chapters,id',
            'course_id' => 'required|exists:courses,id',
            'category_id' => 'required|exists:categories,id',
        ]);

        Unit::create($attributes);
        $chapterId = $request->chapter_id;

        return to_route('units.index', ['chapter_id' => $chapterId])->with('success', __('trans.alert.success.done_create', ['name' => 'Unit']));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Unit $unit)
    {
        // // $chapters = Chapter::get();
        // // $courses = Course::get();
        // $categories = Category::get();
        // // return view('admin.unit.edit', compact('chapters', 'courses', 'categories', 'unit'));
        // return view('admin.unit.edit', compact('categories', 'unit'));


        // $course = Course::with('category:id,name')->find($chapter->course_id);
        $chapter = Chapter::with('course:id,title', 'category:id,name')->find($unit->chapter_id);
        // return $chapter;
        return view('admin.unit.edit', compact('chapter', 'unit'));
    }

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

        return to_route('units.index', ['chapter_id' => $chapterId])->with('success', __('trans.alert.success.done_update', ['name' => 'Unit']));
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Unit $unit)
    {
        $chapterId = $unit->chapter_id;
        $unit->delete();
        return to_route('units.index', ['chapter_id' => $chapterId])->with('success', __('trans.alert.success.done_delete', ['name' => 'Unit']));
    }

    public function ajaxUnits(Request $request)
    {
        $units = Unit::where('chapter_id', $request->chapter_id)
            ->select('id', 'title')
            ->get();

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