<?php

namespace App\Livewire\Drawer\Album;

use App\Models\BatchMutation;
use App\Services\MediaImporter;
use App\Models\Album;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Bus;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\Features\SupportFileUploads\WithFileUploads;

class AddImage extends Component
{
    use WithFileUploads;

    #[Locked]
    public Album $album;

    #[Locked]
    public bool $processing = false;

    #[Validate(['media.*' => 'image|max:8192'])] // max:8MB
    public $media = [];

    public function save(MediaImporter $importer) : void {
        $this->validate();

        foreach ($this->media as $file) {
            $importer->import($file, $this->album);
        }
        $this->redirect(route('album.show', $this->album), navigate: true);
    }

    public function mount(Album $album) : void {
        $this->album = $album;
    }

    public function render() : View|Factory
    {
        return view('livewire.drawer.album.add-image');
    }
}
