<?php

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

use App\Models\Ecommerce\Product\Product\Product as ProductModel;
use App\Models\User;
use App\Nova\Actions\Ecommerce\Product\ProductImportAction;
use App\Nova\Filters\ResourceLimitFilter;
use App\Nova\Filters\VisableFilter;
use App\Nova\Lenses\MostSoldProducts;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\HandlesProductResourceEvents;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\ProductFieldsForCreate;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\ProductFieldsForDetail;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\ProductFieldsForIndex;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\ProductFieldsForUpdate;
use App\Nova\Resources\Ecommerce\Product\Product\Filters\ProductEntrepreneurFilter;
use App\Repositories\CMS\Icon\IconRepository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use ShuvroRoy\NovaTabs\Traits\HasTabs;

class Product extends Resource
{
    /**
     * Handles product resource events
     */
    use HandlesProductResourceEvents;

    /**
     * Supports tabs
     */
    use HasTabs;

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

    /**
     * The number of resources to show per page via relationships.
     *
     * @var int
     */
    public static $perPageViaRelationship = 5;

    /**
     * The pagination per-page options configured for this resource.
     */
    public static function perPageOptions(): array
    {
        return [50, 100, 150];
    }

    /**
     * Get the value that should be displayed to represent the resource.
     */
    public function title(): string
    {
        if (request()->filled('search') && is_null(request('filters'))) {
            return sprintf('%s (%s)', $this->name, $this->barcode);
        }

        return $this->name;
    }

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

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

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

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

    /**
     * Build an "index" query for the given resource.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     */
    public static function indexQuery(NovaRequest $request, $query): Builder
    {
        if ($request->filled('search') && is_null($request->filters)) {

        } else {
            $query->with(['brand', 'media']);
        }

        if ($request->viaResource() != Product::class) {
            $query->where('parent_id', null);
            // ->whereNot('name', 'inlineCreated');
        }

        $user = $request->user();

        if ($user->hasRole('vendor')) {
            $vendorProducts = DB::table('channel_product')
                ->where('channel_id', $user->channel()->id)
                ->pluck('product_id');

            $query->whereIntegerInRaw('id', $vendorProducts);
        }

        return $query;
    }

    /**
     * Get the fields for index.
     */
    public function fieldsForIndex(NovaRequest $request): array
    {
        return ProductFieldsForIndex::make($request, $this);
    }

    /**
     * Get the fields for create.
     */
    public function fieldsForCreate(NovaRequest $request): array
    {
        return ProductFieldsForCreate::make($this, $request);
    }

    /**
     * Get the fields displayed by the resource on detail page.
     */
    public function fieldsForDetail(NovaRequest $request): array
    {
        return ProductFieldsForDetail::make($this, $request);
    }

    /**
     * Get the fields displayed by the resource on detail page.
     */
    public function fieldsForUpdate(NovaRequest $request): array
    {
        return ProductFieldsForUpdate::make($this, $request);
    }

    /**
     * Get the fields.
     */
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),
            Text::make(__('Ady'), 'name'),
            HasMany::make(__('Variations'), 'variations', ProductVariant::class),
        ];
    }

    /**
     * Get the filters available for the resource.
     */
    public function filters(NovaRequest $request): array
    {
        return [
            ProductEntrepreneurFilter::make(),
            VisableFilter::make(),
            // ResourceLimitFilter::make($this),
        ];
    }

    /**
     * Get the lenses available for the resource.
     */
    public function lenses(NovaRequest $request): array
    {
        return [
            MostSoldProducts::make()
                ->canSeeWhen('isAdmin', User::class),
        ];
    }

    /**
     * Get the actions available for the resource.
     */
    public function actions(NovaRequest $request): array
    {
        return [
            ProductImportAction::make()
                ->standalone()
                ->onlyOnIndex()
                ->icon(IconRepository::make()->upload()),
        ];
    }
}
