<?php

namespace App\Http\Controllers\Parametres;

use App\Http\Controllers\Parametres\Base\ParametreBaseController;
use App\Http\Requests\Parametres\StoreRequest;
use App\Http\Requests\Parametres\UpdateRequest;
use App\Models\Action;
use App\Models\Thematique;
use APPLICATION;

class ActionController extends ParametreBaseController
{
    public function __construct()
    {
        parent::__construct('actions', 'Thématique');
    }

    /**
     * Display a listing of the resource.
     *
     * @param int $thematique_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function index(int $thematique_id)
    {
        $thematique = Thematique::find($thematique_id);
        if (isset($thematique)) {
            if ($this->checkAccesStructure($thematique->structure_id)) {
                return view('parametres.actions.index', [
                    'parametres' => Action::where('thematique_id', $thematique_id)->paginate(APPLICATION::ITEMS_PER_PAGE),
                    'parent' => $thematique,
                    'parametres_url' => array($thematique_id),
                    'route' => $this->_route,
                    'has_enfant' => false
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param int $thematique_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function create(int $thematique_id)
    {
        $thematique = Thematique::find($thematique_id);
        if (isset($thematique)) {
            if ($this->checkAccesStructure($thematique->structure_id)) {
                return view('parametres.actions.create', [
                    'parent' => $thematique,
                    'type_parent' => $this->_type_parent,
                    'route' => $this->_route,
                    'store_parametre' => $thematique_id
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param StoreRequest $request
     * @param int $thematique_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(StoreRequest $request, int $thematique_id)
    {
        $validated = $request->validated();

        $thematique = Thematique::find($thematique_id);
        if (isset($thematique)) {
            if ($this->checkAccesStructure($thematique->structure_id)) {
                Action::firstOrCreate(array_merge(
                    $validated,
                    ['thematique_id' => $thematique_id]
                ));
                return redirect()->route('actions.index', $thematique_id)->with('success', 'Action ajoutée avec succès.');
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $thematique_id
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function edit(int $thematique_id, int $id)
    {
        $thematique = Thematique::find($thematique_id);
        $action = Action::find($id);
        if (isset($thematique) && isset($action) && $action->thematique_id == $thematique_id) {
            if ($this->checkAccesStructure($thematique->structure_id)) {
                return view('parametres.actions.edit', [
                    'parametre' => $action,
                    'parent' => $thematique,
                    'type_parent' => $this->_type_parent,
                    'route' => $this->_route,
                    'update_parametre' => array($thematique_id)
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateRequest $request
     * @param int $thematique_id
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(UpdateRequest $request, int $thematique_id, int $id)
    {
        $thematique = Thematique::find($thematique_id);
        $action = Action::find($id);
        if (isset($thematique) && isset($action) && $action->thematique_id == $thematique_id) {
            if ($this->checkAccesStructure($thematique->structure_id)) {
                $validated = $request->validated();
                $action->update($validated);
                return redirect()->route('actions.index', $thematique_id)->with('success', 'Action mise à jour avec succès.');
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $thematique_id
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(int $thematique_id, int $id)
    {
        $thematique = Thematique::find($thematique_id);
        $action = Action::find($id);
        if (isset($thematique) && isset($action) && $action->thematique_id == $thematique_id) {
            if ($this->checkAccesStructure($thematique->structure_id)) {
                if ($action->actions_projet->count() > 0) {
                    return redirect()->back()->withErrors('Impossible de supprimer l\'action car elle est utilisée dans des projets.');
                }
                $action->delete();
                return redirect()->route('actions.index', $thematique_id)->with('success', 'Action supprimée avec succès.');
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }
}
