<?php

declare(strict_types=1);

namespace App\Filament\Actions\Storage\Wiki\Audio;

use App\Actions\Storage\Wiki\Audio\MoveAudioAction as MoveAudio;
use App\Constants\Config\AudioConstants;
use App\Filament\Actions\Storage\Base\MoveAction;
use App\Models\Wiki\Audio;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;

class MoveAudioAction extends MoveAction
{
    public static function getDefaultName(): ?string
    {
        return 'move-audio';
    }

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

        $this->label(__('filament.actions.audio.move.name'));

        $this->visible(Gate::allows('create', Audio::class));
    }

    /**
     * Get the underlying storage action.
     *
     * @param  Audio  $audio
     * @param  array<string, mixed>  $data
     */
    protected function storageAction(?Model $audio, array $data): MoveAudio
    {
        /** @var string $path */
        $path = Arr::get($data, 'path');

        return new MoveAudio($audio, $path);
    }

    public function disk(): string
    {
        return Config::get(AudioConstants::DEFAULT_DISK_QUALIFIED);
    }

    /**
     * Resolve the default value for the path field.
     */
    protected function defaultPath(): ?string
    {
        $audio = $this->getRecord();

        return $audio instanceof Audio
            ? $audio->path
            : null;
    }

    /**
     * The file extension that the path must end with.
     */
    protected function allowedFileExtension(): string
    {
        return '.ogg';
    }
}
