<?php

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

use App\Models\Ecommerce\Product\Inventory\InventoryHistoryRemovedItem;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Product\Product;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class InventoryHistoryRemovedItemResource extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<\App\Models\Ecommerce\Product\Inventory\InventoryHistory>
     */
    public static $model = InventoryHistoryRemovedItem::class;

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

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

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

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

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

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

        return $query;
    }

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

            Number::make(__('Quantity'), 'quantity')
                ->rules('required', 'min:1', 'numeric'),

            Text::make(__('Cost amount'), 'cost_amount'),
            Text::make(__('Total'), 'total'),
        ];
    }

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

            BelongsTo::make(__('Inventory history'), 'inventoryHistoryRemoved', InventoryHistoryRemovedResource::class),
            BelongsTo::make(__('Product'), 'product', Product::class),

            Number::make(__('Quantity'), 'quantity')
                ->rules('required', 'min:1', 'numeric'),

            Number::make(__('Cost amount'), 'cost_amount'),
            Text::make(__('Total'), 'total'),
        ];
    }

    /**
     * Return the location to redirect the user after creation.
     *
     * @param  \Laravel\Nova\Resource  $resource
     * @return \Laravel\Nova\URL|string
     */
    public static function redirectAfterUpdate(NovaRequest $request, $resource): string
    {
        return sprintf('/resources/inventory-history-resources/%s', $resource->inventory_history_id);
    }
}
