<?php

namespace App\Services;

use Exception;
use App\Repositories\Interfaces\RoleRepositoryInterface;

class RoleService
{
    /**
     * @var RoleRepositoryInterface
     */
    protected $roleRepository;

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

    public function __construct(RoleRepositoryInterface $roleRepository)
    {
        $this->roleRepository = $roleRepository;
    }

    public function all()
    {
        try {
            $roles = $this->roleRepository->all();
            $this->response['message'] = "Data retrieved";
            $this->response['data'] = $roles;
            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->roleRepository->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 {
            $role = $this->roleRepository->find($id);
            $this->response['message'] = "Data retrieved";
            $this->response['data'] = $role;
            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 {
            $role = $this->roleRepository->fill($payload, $id);
            if ($role->isDirty()) {
                $this->roleRepository->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->roleRepository->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;
        }
    }
}
