<?php

namespace Modules\Admin\Repositories;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Modules\Admin\Repositories\Interfaces\SaveTheFoodProductRepositoryInterface;
use Modules\SaveTheFood\Models\SaveTheFoodProduct;

class SaveTheFoodProductRepository implements SaveTheFoodProductRepositoryInterface
{
    public function __construct(
        private SaveTheFoodProduct $model
    ) {}

    public function all(): Collection
    {
        return $this->model->with('product')->get();
    }

    public function find(int $id): ?SaveTheFoodProduct
    {
        return $this->model->with('product')->find($id);
    }

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

    public function update(int $id, array $data): SaveTheFoodProduct
    {
        $saveTheFoodProduct = $this->find($id);
        $saveTheFoodProduct->update($data);
        return $saveTheFoodProduct->fresh();
    }

    public function delete(int $id): bool
    {
        return $this->model->destroy($id);
    }

    public function paginate(int $perPage = 10): LengthAwarePaginator
    {
        return $this->model->with('product')->orderBy('created_at', 'desc')->paginate($perPage);
    }

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