<?php

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

use App\Models\Ecommerce\Product\Category\Category as CategoryModel;
use App\Nova\Filters\VisableFilter;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Category\Filters\Level;
use App\Nova\Resources\Ecommerce\Product\Category\Filters\RelatedToMarket;
use App\Nova\Resources\Ecommerce\Product\Product\Product;
use App\Repositories\Ecommerce\Product\Category\CategoryRepository;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Color;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphMany;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
use Outl1ne\NovaSortable\Traits\HasSortableRows;
use Trin4ik\NovaSwitcher\NovaSwitcher;

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

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

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

    /**
     * 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', 'slug',
    ];

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

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

    /**
     * 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(),

            Color::make(__('Color'), 'color')
                ->rules('required')
                ->hideFromIndex()
                ->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
                    $model->options->set([
                        'color' => $request->input($attribute),
                    ]);
                }),

            Select::make(__('Parent Category'), 'parent_id')
                ->searchable()
                ->options(fn () => (new CategoryRepository(request()))->getNamesForNova())
                ->onlyOnForms(),

            BelongsTo::make(__('Parent'), 'parent', self::class)
                ->exceptOnForms(),

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

            Textarea::make(__('Description'), 'description'),

            Text::make(__('Tax percentage'), 'tax_percentage')
                ->rules('required', 'numeric', 'max:100'),

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

            NovaSwitcher::make(__('Market related'), 'type')
                ->canSeeWhen('systemUser', $this)
                ->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'),

            HasMany::make(__('Children'), 'children', self::class),

            MorphMany::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.
     */
    public function filters(NovaRequest $request): array
    {
        return [
            RelatedToMarket::make(),
            Level::make(),
            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 [];
    }
}
