<?php

namespace App\Repositories;

use Illuminate\Database\Eloquent\Model;

abstract class BaseRepository
{
    protected Model $model;

    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    public function create(array $data)
    {
        return $this->model->create($data);
    }

    public function update($id, array $data)
    {
        $record = $this->find($id);
        $record->update($data);
        return $record;
    }

    public function find($id)
    {
        return $this->model->findOrFail($id);
    }

    public function delete($id)
    {
        $record = $this->find($id);
        return $record->delete();
    }

    public function all()
    {
        return $this->model->all();
    }

    public function paginate($perPage = 15)
    {
        return $this->model->paginate($perPage);
    }

    public function findWhere($field, $value)
    {
        return $this->model->where($field, $value)->get();
    }

    public function findWhereFirst($field, $value)
    {
        return $this->model->where($field, $value)->first();
    }

    public function count()
    {
        return $this->model->count();
    }
}