<?php

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

use App\Models\Ecommerce\Product\Product\Product;
use App\Nova\Fields\FieldHelpers;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Brand\Brand;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
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 ProductResource extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<Product>
     */
    public static $model = Product::class;

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

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

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

    /**
     * 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
    {
        return $query->where('parent_id', null);
    }

    /**
     * Fields
     */
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),
            Images::make(__('Image'), 'uploads')->conversionOnIndexView('thumb200x200'),
            Text::make(__('Name'), fn () => $this->novaDetailPage())
                ->displayUsing(FieldHelpers::asLink(
                    link: $this->novaDetailPage(),
                    title: $this->name
                ))
                ->asHtml(),

            BelongsTo::make(__('Brand'), 'brand', Brand::class)->sortable(),
            Number::make(__('Price'), 'cost_amount')->sortable(),
            Text::make(__('SKU'), 'sku')->sortable(),
            Text::make(__('Total count'), FieldHelpers::formatQuantity())->asHtml(),
            Text::make(__('Giriş çykyş boýunça'), FieldHelpers::formatQuantity(
                intval(DB::table('inventory_history_items')->where('product_id', $this->id)->sum('quantity'))
                -
                intval(DB::table('inventory_history_removed_items')->where('product_id', $this->id)->sum('quantity'))
            ))->asHtml(),

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

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