<?php

namespace App\Jobs;

use App\Models\Album;
use App\Models\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Laravel\Facades\Image as InterventionImage;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class ImportMediaJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(private TemporaryUploadedFile $file, private Album $album)
    {
    }

    public function handle(): void
    {
        if($this->isImage()) {
            $image = Image::create([
                'album' => $this->album,
                'isCover' => false
            ]);
            $encoded = InterventionImage::read($this->file)->toAvif(config('gallery.original.quality', 80));
            Storage::disk('images')->put("{$$this->album->id}/original/{$image->id}.avif");
        }
    }

    
}
