<?php

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

use App\Models\Ecommerce\Product\Brand\Brand as BrandModel;
use App\Nova\Filters\VisableFilter;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Product\Product;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Fields\Trix;
use Laravel\Nova\Http\Requests\NovaRequest;
use Outl1ne\NovaSortable\Traits\HasSortableRows;
use Trin4ik\NovaSwitcher\NovaSwitcher;

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

    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = BrandModel::class;

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

    /**
     * The relationships that should be eager loaded on index queries.
     *
     * @var array
     */
    public static $with = ['media'];

    /**
     * Indicates if the resource should be globally searchable.
     *
     * @var bool
     */
    public static $globallySearchable = true;

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

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

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

    /**
     * Build an "index" query for the given resource.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     */
    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query;
    }

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

            Images::make(__('Image'), 'uploads')
                ->conversionOnIndexView('thumb200x200')
                ->rules('required')
                ->required(),

            Text::make(__('Name'), 'name')
                ->rules(['required', 'max:150'])
                ->creationRules('unique:brands,name'),

            Text::make(__('Web site'), 'website')
                ->hideFromIndex(),

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

            Boolean::make(__('Market related'), 'type')
                ->sortable()
                ->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
                    $model->{$attribute} = $request->boolean('type') ? 'market' : '';
                }),

            Text::make(__('Seo title'), 'seo_title')
                ->hideFromIndex(),

            Textarea::make(__('Seo description'), 'seo_description'),

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

            HasMany::make('Products', 'products', Product::class),
        ];
    }

    /**
     * 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 [
            VisableFilter::make(),
        ];
    }

    /**
     * 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 [];
    }
}
