<?php

declare(strict_types=1);

namespace App\Nova;

use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

final class ConfigResource extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<\App\Models\Config>
     */
    public static string $model = \App\Models\Config::class;

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = ['key'];

    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
    public static $group = 'Other';

    /**
     * Get the displayable label of the resource.
     */
    public static function label(): string
    {
        return 'Settings';
    }

    /**
     * Get the displayable singular label of the resource.
     */
    public static function singularLabel(): string
    {
        return 'Setting';
    }

    /**
     * Resolves the display title from the config class title or falls back to the raw key.
     */
    public function title(): string
    {
        return $this->getConfigClass()?->title ?? $this->resource->key;
    }

    /**
     * Get the fields displayed by the resource.
     *
     * @return array<int, \Laravel\Nova\Fields\Field|\Laravel\Nova\Panel|\Laravel\Nova\ResourceTool|\Illuminate\Http\Resources\MergeValue>
     */
    public function fields(NovaRequest $request): array
    {
        return array_merge(
            [
                Text::make('Name', 'key')
                    ->resolveUsing(fn ($value) => $this->getConfigClass()?->title ?? $value)
                    ->exceptOnForms(),

                Text::make('Description', 'key')
                    ->resolveUsing(fn () => $this->getConfigClass()?->description ?? '')
                    ->exceptOnForms(),
            ],
            array_map(
                fn ($field) => $field->hideFromIndex(),
                $this->getConfigClass()?->getNovaFields() ?? []
            ),
            [
                DateTime::make('Created At')->exceptOnForms()->hideFromIndex()->sortable()->filterable(),
                DateTime::make('Updated At')->exceptOnForms()->hideFromIndex()->sortable()->filterable(),
            ],
        );
    }

    /**
     * Get the cards available for the request.
     *
     * @return array<int, \Laravel\Nova\Card>
     */
    public function cards(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @return array<int, \Laravel\Nova\Filters\Filter>
     */
    public function filters(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @return array<int, \Laravel\Nova\Lenses\Lens>
     */
    public function lenses(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @return array<int, \Laravel\Nova\Actions\Action>
     */
    public function actions(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Resolves the config class instance for this resource's key via the Config service.
     */
    private function getConfigClass(): ?object
    {
        $key = $this->resource?->key;

        if ($key === null) {
            return null;
        }

        return app(\App\Services\Config::class)->getConfigClassForGroup($key);
    }
}
