<?php

namespace App\Importers;

use App\Importers\Image\Jobs\FinishImageModification;
use App\ImportsMedia;
use Illuminate\Http\UploadedFile;
use App\Importers\Image\Jobs\{ImportImage, GenerateFullscreen, GenerateThumbnail};
use App\Models\Image as ImageModel;
use App\Models\Album;
use Illuminate\Support\Facades\Bus;

class Image implements ImportsMedia {
    
    public static function supports(UploadedFile $file): bool {
        $supportedMimes = config('gallery.image.suffixes', ['png', 'gif', 'bmp', 'svg', 'avi', 'jpg', 'jpeg']);
        return in_array($file->guessExtension(), $supportedMimes);
    }

    public function __construct(private UploadedFile $file, private Album $location)
    {
    }

    public function import(): void {
        $image = ImageModel::create([
            'album_id' => $this->location->id
        ]);
        $imageId = $image->id;

        Bus::chain([
            new ImportImage($this->file->getPathname(), $image),
            Bus::batch([
                new GenerateFullscreen($image),
                new GenerateThumbnail($image),
            ]),
            new FinishImageModification($image),
        ])->dispatch();
    }
}