<?php

namespace App\Services;

use App\Models\Media;
use Illuminate\Database\Eloquent\Collection;

class MediaService
{
    public function getAll(): Collection
    {
        return Media::query()
            ->with(['user'])
            ->orderBy("created_at", "desc")
            ->get();
    }

    public function findById(int $id): ?Media
    {
        return Media::with(['user'])->find($id);
    }

    public function create(array $data): Media
    {
        return Media::create($data);
    }

    public function update(int $id, array $data): ?Media
    {
        $model = $this->findById($id);

        if (!$model) {
            return null;
        }

        $model->update($data);
        return $model->fresh(['user']);
    }

    public function delete(int $id): bool
    {
        $model = $this->findById($id);

        if (!$model) {
            return false;
        }

        return $model->delete();
    }
}
