<?php

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

use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;

class OrderItemFieldsForUpdate
{
    /**
     * Order item fields for update
     */
    public static function make($resource, $request): array
    {
        return [
            ID::make(),

            Hidden::make('order_id'),

            Text::make(__('Product name'), 'product_name')
                ->readonly(),

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

            Text::make(__('Price'), 'unit_price_amount')
                ->rules('required'),

            Number::make(__('Total'), 'total')
                ->dependsOn(['quantity', 'unit_price_amount'], function ($field, $request, $formData) {
                    if (! $formData->unit_price_amount || ! $formData->quantity) {
                        return;
                    }

                    $field->setValue($formData->unit_price_amount * $formData->quantity);
                })->readonly(),
        ];
    }
}
