<?php

namespace App\Http\Controllers;

use App\ResourceCategory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ResourceCategoryController extends Controller
{
    public function __construct()
    {
        $this->middleware([ "auth" ]);
        $this->middleware([ "lang" ]);

        $this->middleware([ "check.auth:resource.category.list" ])->only("index");
        $this->middleware([ "check.auth:resource.category.show" ])->only("show");
        $this->middleware([ "check.auth:resource.category.create" ])->only("create", "store");
        $this->middleware([ "check.auth:resource.category.edit" ])->only("edit", "update");
        $this->middleware([ "check.auth:resource.category.delete" ])->only("delete");
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $categories = ResourceCategory::query()->paginate($request->input("limit", 20));

        return Response::detect("resource-categories.index", ["category"=> $categories]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Response::detect("resource-categories.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $requestBody = $request->validate([
            "name" => "unique|required|max:255",
            "description" => "required|max:255",
            "slug" => "unique|required|max:255"
        ]);
        $category = new ResourceCategory($requestBody);
        $category->save();

        return Response::detect("resource-categories.store");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(ResourceCategory $id)
    {
        return Response::detect("resource-categories.show", ["category" => $id]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category = ResourceCategory::find($id);
        return Response::detect("resource-categories.edit", ["category" => $category]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();

        $category = ResourceCategory::find($id);
        $category->update($data);
        $category->save();

        return Response::detect("resource-categories.update", ["category" => $category]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = ResourceCategory::find($id);
        $category->delete();
        return redirect()->route("resource-categories.index");
    }
}
