<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProfessionRequest;
use App\Http\Traits\Desxpert;
use App\Services\ProfessionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class ProfessionController extends Controller
{
    use Desxpert;

    protected $professionService;

    public function __construct(ProfessionService $professionService)
    {
        $this->professionService = $professionService;
        $this->route = 'professions';
        $this->view = 'professions';
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $professions = $this->professionService->getAllProfessions();
        $loggedUser = $this->loggedUser();

        return view($this->view.'.index', compact('professions', 'loggedUser'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $parentProfessions = $this->professionService->getAllParent();
        $loggedUser = $this->loggedUser();

        return view($this->view.'.create', compact('parentProfessions', 'loggedUser'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProfessionRequest $request)
    {
        $response = $this->professionService->storeProfession($request);

        return redirect($this->route)->with($response['status'], $response['message']);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Profession  $profession
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $profession = $this->professionService->getProfession($id);
        $parentProfessions = $this->professionService->getAllParent();
        $loggedUser = $this->loggedUser();

        return view($this->view.'.edit', compact('profession', 'parentProfessions', 'loggedUser'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\ProfessionRequest  $request
     * @param  \App\Models\Profession  $profession
     * @return \Illuminate\Http\Response
     */
    public function update(ProfessionRequest $request, $id)
    {
        $response = $this->professionService->updateProfession($request, $id);

        return redirect($this->route)->with($response['status'], $response['message']);
    }

    /**
     * Display the specified resource.
     *
     * @param  $profession id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $profession = $this->professionService->getProfession($id);

        return Response::json($profession);
    }

    /**
     * Checking of professional name if still available
     *
     * @param  \App\Models\Profession  $profession
     * @return \Illuminate\Http\Response
     */
    public function checkName(Request $request)
    {
        $response = $this->professionService->checkIfNameIsAvailable($request);

        return Response::json($response);
    }

    /**
     * delete data
     *
     * @param  $profession id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->professionService->removeProfession($id);

        return redirect($this->route)->with('success', 'Success! The profession has been deleted.');
    }
}
