<?php

namespace App\Nova\Resources\Ecommerce\Product\Attribute;

use App\Models\Ecommerce\Product\Property\Attribute as AttributeModel;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Category\Category;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Trix;
use Laravel\Nova\Http\Requests\NovaRequest;
use NovaAttachMany\AttachMany;
use Outl1ne\MultiselectField\Multiselect;
use Outl1ne\NovaSortable\Traits\HasSortableRows;
use Trin4ik\NovaSwitcher\NovaSwitcher;

class Attribute extends Resource
{
    /**
     * Sortable behavior
     */
    use HasSortableRows;

    /**
     * The model the resource corresponds to.
     *
     * @var class-string<AttributeModel>
     */
    public static $model = AttributeModel::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'name';

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

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

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

    /**
     * Get the fields displayed by the resource.
     */
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),

            Text::make(__('Name'), 'name')
                ->rules('required', 'string')
                ->translatable(),

            Select::make(__('Type'), 'type')
                ->displayUsingLabels()
                ->searchable()
                ->default(AttributeModel::typesFields()['text'])
                ->options(AttributeModel::typesFields())
                ->rules('required'),

            Trix::make(__('Description'), 'description')
                ->translatable(),

            NovaSwitcher::make(__('Is enabled'), 'is_visible')
                ->default(true)
                ->canSeeWhen('systemUser', $this),

            NovaSwitcher::make(__('Is required'), 'is_required')
                ->default(true)
                ->canSeeWhen('systemUser', $this),

            NovaSwitcher::make(__('Is searchable'), 'is_searchable')
                ->default(false)
                ->canSeeWhen('systemUser', $this),

            NovaSwitcher::make(__('Is filterable'), 'is_filterable')
                ->default(false)
                ->canSeeWhen('systemUser', $this),

            Boolean::make(__('Is enabled'), 'is_visible')
                ->canSeeWhen('vendor', $this),

            Boolean::make(__('Is required'), 'is_required')
                ->canSeeWhen('vendor', $this),

            Boolean::make(__('Is searchable'), 'is_searchable')
                ->canSeeWhen('vendor', $this),

            Boolean::make(__('Is filterable'), 'is_filterable')
                ->canSeeWhen('vendor', $this),

            AttachMany::make(__('Categories'), 'categories', Category::class),
            Multiselect::make(__('Categories'), 'categories')->belongsToMany(Category::class)
                ->onlyOnDetail(),

            HasMany::make(__('Attribute values'), 'values', AttributeValue::class)
                ->hideFromDetail(function (NovaRequest $request, $resource) {
                    return $this->type !== 'select';
                }),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @return array
     */
    public function lenses(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [];
    }
}
