<?php

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

use App\Models\Ecommerce\Product\Order\Payment\OrderPayment;
use App\Models\Ecommerce\Product\Order\Shipping\OrderShipping;
use App\Models\Ecommerce\Product\Order\Status\OrderStatus;
use App\Models\System\Settings\Location\Province;
use App\Models\System\Settings\Location\Region;
use App\Models\System\Settings\OS;
use App\Repositories\Ecommerce\Order\NovaOrderRepository;
use Illuminate\Support\Str;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Nurmuhammet\NovaInputmask\NovaInputmask;

class OrderFieldsForCreate
{
    /**
     * Order fields for create
     */
    public static function make($resource, $request): array
    {
        return [
            Hidden::make('number')->default(Str::random(30)),
            Hidden::make('user_id')->default($request->user()->id),
            Hidden::make('source_app')->default(OS::ADMIN),

            ID::make(),

            Text::make(__('Customer name'), 'customer_name')
                ->rules('nullable', 'string', 'max:255')
                ->canSeeWhen('systemUser', $resource)
                ->default('Walk in customer'),

            NovaInputmask::make(__('Customer phone'), 'customer_phone')
                ->mask(phoneMaskFormat())
                ->storeRawValue()
                ->canSeeWhen('systemUser', $resource),

            Select::make(__('Region'), 'region')
                ->displayUsingLabels()
                ->searchable()
                ->options(Region::values())
                ->default(Region::default())
                ->canSeeWhen('systemUser', $resource),

            Select::make(__('Province'), 'province_id')
                ->displayUsingLabels()
                ->searchable()
                ->dependsOn(['region'], NovaOrderRepository::dependsOnWhere('region', Province::class)),

            Text::make(__('Customer address'), 'customer_address')
                ->rules('nullable', 'string', 'max:255'),

            Select::make(__('Payment type'), 'payment_type_id')
                ->displayUsingLabels()
                ->searchable()
                ->options(OrderPayment::values())
                ->default(OrderPayment::default()),

            Select::make(__('Shipping method'), 'shipping_method')
                ->displayUsingLabels()
                ->searchable()
                ->options(OrderShipping::values())
                ->default(OrderShipping::default())
                ->sortable(),

            Text::make(__('Shipping price'), 'shipping_price')
                ->rules('required', 'numeric')
                ->dependsOn('shipping_method', function ($field, $request, $formData) {
                    if ($formData->shipping_method) {
                        $field->setValue(OrderShipping::priceFor($formData->shipping_method));
                    }
                }),

            Date::make(__('Delivery at'), 'delivery_at')
                ->default(date('Y-m-d'))
                ->rules('required'),

            Select::make(__('Delivery time'), 'delivery_time')
                ->displayUsingLabels()
                ->searchable()
                ->options(OrderShipping::times())
                ->default(OrderShipping::defaultTime()),

            Text::make(__('Additional tax'), 'additional_tax')
                ->rules('nullable', 'numeric'),

            Select::make(__('Status'), 'status')
                ->displayUsingLabels()
                ->searchable()
                ->options(OrderStatus::values())
                ->default(OrderStatus::default()),

            Text::make(__('Notes'), 'notes')
                ->rules('nullable', 'string', 'max:255'),
        ];
    }
}
