<?php

namespace App\Http\Controllers;

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

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

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

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Response::detect("resource-extensions.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([
            "extension" => "unique|required|max:255",
            "description" => "required|max:255"
        ]);

        $event = new ResourceExtension($requestBody);

        $event->save();

        return Response::detect("events.store");
    }

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

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

    /**
     * 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();

        $extension = ResourceExtension::find($id);
        $extension->update($data);
        $extension->save();

        return Response::detect("resource-extensions.update", [ "extension" => $extension]);

    }

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