<?php

namespace App\Services;

use App\Libs\Consts\ProfessionConst;
use App\Libs\Consts\StatusConst;
use App\Repositories\Interfaces\ProfessionRepositoryInterface;

class ProfessionService
{

    protected $professionRepository;

    public function __construct(ProfessionRepositoryInterface $professionRepository)
    {
        $this->professionRepository = $professionRepository;
    }

    public function getAllProfessions()
    {
        $professions = $this->professionRepository->all();

        foreach ($professions as $key => $profession) {
            $parentName = $this->checkIfHasParent($profession);

            $professions[$key]->parent_name = $parentName;
        }

        return $professions;
    }

    public function storeProfession($request)
    {

        $payload = $request->all();
        $payload['parent_id'] = $this->checkRequestParent($request);

        $this->professionRepository->create($payload);

        $response = [
            'status' => 'success',
            'message' => 'Success! The new profession has been created.'
        ];

        return $response;
    }

    public function getProfession($id)
    {
        $profession = $this->professionRepository->find($id);
        $profession->parent_name = $this->checkIfHasParent($profession);

        return $profession;
    }

    public function updateProfession($request, $professionId)
    {

        $payload = $request->all();
        $payload['parent_id'] = $this->checkRequestParent($request);

        $this->professionRepository->update($payload, $professionId);

        $response = [
            'status' => 'success',
            'message' => 'Success! The profession has been updated.'
        ];

        return $response;;
    }

    public function removeProfession($id)
    {
        $this->professionRepository->removeAsParent($id);
        return $this->professionRepository->delete($id);
    }

    public function listProfession()
    {
        return $this->professionRepository->getList();
    }

    public function getAllParent()
    {
        return $this->professionRepository->getParentProfession();
    }

    public function checkIfHasParent($profession)
    {
        $parentName = "-";

        if ($profession->parent_id > 0) {
            $parent = $this->professionRepository->find($profession->parent_id);
            $parentName = $parent->name;
        }

        return $parentName;
    }

    public function checkRequestParent($request)
    {
        return $request->filled('parent_id') ? $request->get('parent_id') : ProfessionConst::SET_AS_PARENT;
    }

    public function checkIfNameIsAvailable($request)
    {
        $inputName = $request->get('name');
        $professionId = $request->get('id');

        $profession = $this->professionRepository->getByColumn('name', $inputName);

        if (!empty($profession)) {
            if ($profession->id != $professionId) {
                return ['status' => StatusConst::BAD_REQUEST, 'message' => 'Profession Name is already existing'];
            }
        }

        return ['status' => StatusConst::SUCCESS, 'message' => ''];
    }
}
