<?php

declare(strict_types=1);

namespace App\Filament\Actions\Models\Wiki\Image;

use App\Actions\Storage\Wiki\Image\UploadImageAction as ImageUploadImageAction;
use App\Enums\Models\Wiki\ImageFacet;
use App\Filament\Actions\BaseAction;
use App\Filament\Components\Fields\Select;
use App\Filament\Resources\Base\BaseListResources;
use App\Models\Wiki\Image;
use Filament\Forms\Components\FileUpload;
use Filament\Schemas\Schema;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;

class UploadImageAction extends BaseAction
{
    protected array $facets = [];

    public static function getDefaultName(): ?string
    {
        return 'upload-image';
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->label(__('filament.actions.models.wiki.upload_image.name'));

        $this->facets([
            ImageFacet::GRILL,
            ImageFacet::DOCUMENT,
        ]);

        $this->visible(function ($livewire): bool {
            if (Auth::user()->cannot('create', Image::class)) {
                return false;
            }

            return $livewire instanceof BaseListResources;
        });

        $this->action(fn (array $data) => $this->handle($data));
    }

    /**
     * Perform the action on the table.
     *
     * @param  array<string, mixed>  $data
     */
    public function handle(array $data): void
    {
        $image = Arr::get($data, Image::ATTRIBUTE_PATH);
        $facet = Arr::get($data, Image::ATTRIBUTE_FACET);

        ImageUploadImageAction::createImageFromFile($image, $facet);
    }

    public function getSchema(Schema $schema): Schema
    {
        $options = [];

        foreach ($this->facets as $facet) {
            $options[$facet->value] = $facet->localize();
        }

        return $schema
            ->components([
                Select::make(Image::ATTRIBUTE_FACET)
                    ->label(__('filament.fields.image.facet.name'))
                    ->helperText(__('filament.fields.image.facet.help'))
                    ->options($options)
                    ->required()
                    ->enum(ImageFacet::class),

                FileUpload::make(Image::ATTRIBUTE_PATH)
                    ->label(__('filament.fields.image.image.name'))
                    ->required()
                    ->image()
                    ->imageEditor()
                    ->imageEditorAspectRatioOptions([null, '2:3'])
                    ->storeFiles(false),
            ])
            ->columns(1);
    }

    /**
     * Get the facets available for the action.
     *
     * @param  ImageFacet[]  $facets
     */
    public function facets(array $facets): static
    {
        $this->facets = $facets;

        return $this;
    }
}
