<?php

namespace App\Services;

use App\Repositories\Interfaces\ProfessionRepositoryInterface;
use Exception;

class ProfessionService
{
    /**
     * @var ProfessionRepositoryInterface
     */
    protected $professionRepository;

    private $response = [
        'success' => true,
        'message' => null
    ];

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

    public function all()
    {
        try {
            $professions = $this->professionRepository->getParentOnly();
            $this->response['message'] = "Data retrieved";
            $this->response['data'] = $professions;
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Error retrieving data";
            return $this->response;
        }
    }

    /**
     * @param array $payload
     *
     * @return array
     */
    public function store(array $payload): array
    {
        try {
            $this->professionRepository->create($payload);
            $this->response['message'] = "Data created";
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Bad Request";
            return $this->response;
        }
    }

    /**
     * @param int $id
     *
     * @return array
     */
    public function get(int $id): array
    {
        try {
            $profession = $this->professionRepository->find($id);
            $this->response['message'] = "Data retrieved";
            $this->response['data'] = $profession;
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Error retrieving data";
            return $this->response;
        }
    }

    /**
     * @param array $payload
     * @param int $id
     *
     * @return array
     */
    public function update(array $payload, int $id): array
    {
        try {
            $profession = $this->professionRepository->fill($payload, $id);
            if ($profession->isDirty()) {
                $this->professionRepository->update($payload, $id);
                $this->response['message'] = "Data updated";
            } else {
                $this->response['message'] = "No changes detected";
            }
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Bad Request";
            return $this->response;
        }
    }

    /**
     * @param int $id
     *
     * @return array
     */
    public function delete(int $id): array
    {
        try {
            $this->professionRepository->delete($id);
            $this->response['message'] = "Data deleted";
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Bad Request";
            return $this->response;
        }
    }

    /**
     * @param int $professionId
     * 
     * @return array
     */
    public function getSpecialties(int $professionId): array
    {
        try {
            $specialties = $this->professionRepository->getMultipleValues('parent_id', $professionId);
            $this->response['message'] = "Data retrieved";
            $this->response['data'] = $specialties;
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Error retrieving data";
            return $this->response;
        }
    }
}
