<?php

declare(strict_types=1);

namespace App\Filament\Actions\Storage\Wiki\Video\Script;

use App\Actions\Storage\Wiki\Video\Script\MoveScriptAction as MoveScript;
use App\Constants\Config\VideoConstants;
use App\Filament\Actions\Storage\Base\MoveAction;
use App\Models\Wiki\Video\VideoScript;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;

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

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

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

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

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

        return new MoveScript($script, $path);
    }

    public function disk(): string
    {
        return Config::get(VideoConstants::SCRIPT_DISK_QUALIFIED);
    }

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

        return $script instanceof VideoScript
            ? $script->path
            : null;
    }

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