<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Commands;

use Illuminate\Support\Str;

final class MakeResourceCommand extends MakeBuilderFileCommand
{
    protected $signature = 'builder:make:resource
        {name : The class name of the resource (e.g. PostResource)}
        {--model= : The model class name (defaults to class name with Resource stripped)}
        {--translatable : Generate a resource with translatable fields}
        {--force : Overwrite the resource if it already exists}';

    protected $description = 'Create a new Builder resource class';

    protected function stubName(): string
    {
        return $this->option('translatable') ? 'resource-translatable' : 'resource';
    }

    protected function outputSubdirectory(): string
    {
        return 'Resources';
    }

    protected function packageStubsPath(): string
    {
        return __DIR__.'/../../../stubs';
    }

    /** @return array<string, string> */
    protected function buildReplacements(string $className): array
    {
        $base = Str::replaceLast('Resource', '', $className);

        /** @var string|null $modelOption */
        $modelOption = $this->option('model');
        $model = $modelOption ?? $base;

        $singularLabel = Str::headline($base);
        $label = Str::plural($singularLabel);
        $slug = Str::plural(Str::kebab($base));

        return [
            'namespace' => 'App\\Builder\\Resources',
            'class' => $className,
            'slug' => $slug,
            'label' => $label,
            'singularLabel' => $singularLabel,
            'model' => $model,
        ];
    }
}
