<?php

namespace App\Http\Controllers;

use App\Models\{{Model}};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Input;

class {{Model}}Controller extends Controller {

    private $model = {{Model}}::class;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index() {
        $this->authorize('browse', $this->model);

        $type = Input::get('t');
        $searchText = trim(Input::get('text'));
        if($type=='title') $type = 'title';

        ${{models}} = {{Model}}::withTrashed()->sortable()->whereLike($type,$searchText)
            ->paginate(\auth('admin')->user()->paginatePreference);

        return view('ctrl.{{model_snake}}.index')->with([
            '{{models}}' => ${{models}},
            'modelClass' => $this->model,
        ]);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create() {
        $this->authorize('add', $this->model);
        return view('ctrl.{{model_snake}}.create-edit')->with([
            'edit' => false,
        ]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(Request $request) {
        $this->authorize('add', $this->model);
        if($validated = {{Model}}::getValidatedParameters($request)){
            ${{model}} = {{Model}}::create($validated);
        }
        return redirect(route('{{resource_route}}.index'))
            ->with('success', 'successfully created {{Model_title}}: <b>' . ${{model}}->title . '</b>');
    }


    /**
     * Display the specified resource.
     *
     * @param string $fake_id
     * @return void
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function show(string $fake_id) {
        $this->authorize('read', $this->model);
        ${{model}} = {{Model}}::withTrashed()->find(decryptFakeId($fake_id));
        //complete with a view if needed
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param string $fake_id
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(string $fake_id) {
        $this->authorize('edit', $this->model);
        ${{model}} = {{Model}}::withTrashed()->find(decryptFakeId($fake_id));
        return view('ctrl.{{model_snake}}.create-edit')->with([
                'edit' => true,
                '{{model}}' => ${{model}}
            ]
        );
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  string $fakeId
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(Request $request, string $fakeId) {
        $this->authorize('edit', $this->model);

        ${{model}} = {{Model}}::withTrashed()->find(decryptFakeId($fakeId));

        if($validated = {{Model}}::getValidatedParameters($request, ${{model}})){
            ${{model}}->update($validated);
        }

        return redirect(route('{{resource_route}}.index'))
            ->with('success', 'successfully updated {{Model_title}}: <b>' . ${{model}}->title . '</b>');
    }


    /** Remove the specified resource from storage.
     *
     * @param string $fake_id
     * @return string
     */
    public function destroy(string $fake_id) {
        return Gate::denies('delete', $this->model) ? '403' : (
        ($model = {{Model}}::withTrashed()->find(decryptFakeId($fake_id))) ? $model->deleteSequence() : '404'
        );
    }


    /** Activate the specified resource.
     *
     * @param string $fake_id
     * @return string
     */
    public function activate(string $fake_id) {
        return Gate::denies('de/activate', $this->model) ? '403' : (
        ($model = {{Model}}::withTrashed()->find(decryptFakeId($fake_id))) ? $model->activateSequence() : '404'
        );
    }


    /** Deactivate the specified resource.
     *
     * @param string $fake_id
     * @return string
     */
    public function deactivate(string $fake_id) {
        return Gate::denies('de/activate', $this->model) ? '403' : (
        ($model = {{Model}}::find(decryptFakeId($fake_id))) ? $model->deactivateSequence() : '404'
        );
    }


    /** Children of the specified resource if any.
     *
     * @param string $fake_id
     * @return string
     */
    public function children(string $fake_id) {
        return !Gate::any(['delete','de/activate'], $this->model) ? '403' : (
        ($model = {{Model}}::withTrashed()->find(decryptFakeId($fake_id))) ? $model->children() : '404'
        );
    }

}