<?php
/*=========================================================================================
    App Name  : PPID PTUN Kupang
    File Name : BaseService
    --------------------------------------------------------------------------------------
    Author    : Fuad Auliya Rohman
    Email     : fuad.dev16@gmail.com
==========================================================================================*/

namespace App\Services;

use App\Contracts\CrudServiceContract;

class BaseService implements CrudServiceContract
{
    /**
     * Find an item by ID
     *
     * @param int|mixed $id
     * @return Model|null
     */
    public function findById($id)
    {
        return $this->mainRepository->findById($id);
    }

    /**
     * find an item by parameter
     *
     * @param string $parameter
     * @param mixed $value
     * @return Model|null
     */
    public function findBy(string $parameter, $value)
    {
        return $this->mainRepository->findBy($parameter, $value);
    }

    /**
     * If eloquent can't find data redirect to back
     *
     * @param mixed $id
     * @return mixed
     */
    public function findOr($id)
    {
        $data = $this->mainRepository->findById($id);

        if(!$data) {
            return redirect()->back()->with("failed", __('messages.data_not_found'));
        }

        return $data;
    }

    /**
     * Return all items
     *
     * @return Collection
     */
    public function all()
    {
        return $this->mainRepository->all();
    }

    /**
     * Create an item
     *
     * @param array|mixed $data
     * @return Model|mixed
     */
    public function create($data)
    {
        return $this->mainRepository->create($data);
    }

    /**
     * Update a model
     *
     * @param int|mixed $id
     * @param array $data
     * @return Bool|mixed
     */
    public function update($id, array $data)
    {
        return $this->mainRepository->update($id, $data);
    }

    /**
     * Delete a model
     *
     * @param int|mixed $id
     * @return Bool|mixed
     */
    public function delete($id)
    {
        return $this->mainRepository->delete($id);
    }

    /**
     * Delete multiple items
     *
     * @param array $id
     * @return Bool|mixed
     */
    public function destroy(array $id)
    {
        return $this->mainRepository->destroy($id);
    }
}
