<?php

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

use App\Nova\Fields\FieldHelpers;
use App\Nova\Resources\Ecommerce\Product\Brand\Brand;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Trin4ik\NovaSwitcher\NovaSwitcher;

class ProductFieldsForIndex
{
    /**
     * Get the fields for index.
     */
    public static function make(NovaRequest $request, $resource): array
    {
        if ($request->filled('search') && is_null($request->filters)) {
            return [];
        }

        return [
            ID::make()->sortable(),
            Images::make(__('Image'), 'uploads')->conversionOnIndexView('thumb400x400'),
            Text::make(__('Name'), 'name')->sortable(),

            BelongsTo::make(__('Brand'), 'brand', Brand::class)
                ->sortable()
                ->searchable()
                ->filterable(),

            Number::make(__('Price'), 'cost_amount')
                ->sortable()
                ->filterable(),

            Text::make(__('SKU'), 'sku')->sortable(),

            Text::make(__('Count'), FieldHelpers::formatQuantity())->asHtml(),

            // For filter
            Number::make(__('Count'), 'stock')
                ->hideFromIndex()
                ->filterable(),

            Boolean::make(__('Active'), 'is_visible')
                ->sortable()
                ->canSeeWhen('isVendor', $resource),

            NovaSwitcher::make(__('Is visible'), 'is_visible')
                ->sortable()
                ->canSeeWhen('isAdmin', $resource),
        ];
    }
}
