<?php

namespace Modules\Media\Services;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Modules\Media\Repositories\Interfaces\MediaRepositoryInterface;

class MediaService
{
    public function __construct(
        private MediaRepositoryInterface $mediaRepository
    ) {}

    public function getAll(): \Illuminate\Database\Eloquent\Collection
    {
        return $this->mediaRepository->all();
    }

    public function findById(int $id): ?\Modules\Media\Models\Media
    {
        return $this->mediaRepository->find($id);
    }

    public function upload(UploadedFile $file, string $type, Model $mediable): \Modules\Media\Models\Media
    {
        return $this->mediaRepository->upload($file, $type, $mediable);
    }

    public function uploadWithoutMediable(UploadedFile $file, string $type): \Modules\Media\Models\Media
    {
        return $this->mediaRepository->uploadWithoutMediable($file, $type);
    }

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

    public function getByMediable(string $mediableType, int $mediableId): \Illuminate\Database\Eloquent\Collection
    {
        return $this->mediaRepository->getByMediable($mediableType, $mediableId);
    }

    public function getUrl(\Modules\Media\Models\Media $media): string
    {
        return Storage::disk('public')->url($media->path);
    }
}
