<?php

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

use App\Models\Ecommerce\Product\Order\Status\OrderStatus;
use App\Models\System\Settings\OS;
use App\Nova\Resources\Ecommerce\Product\Order\OrderItem;
use App\Nova\Resources\System\Payments\PaymentType;
use App\Nova\User;
use Laravel\Nova\Fields\Badge;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Nurmuhammet\NovaInputmask\NovaInputmask;

class OrderFieldsForDetail
{
    /**
     * Order fields for detail
     */
    public static function make($resource, $request): array
    {
        // nova has a wierd bug, can load detail fields while on Index
        if (! $request->isResourceDetailRequest()) {
            if ($request->viaResource && $request->viaRelationship && $request->relationshipType) {

            } else {
                return [];
            }
        }

        return [
            ID::make()->sortable(),

            Text::make(__('Customer name'), 'customer_name'),

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

            Text::make(
                name: __('Address'),
                attribute: fn ($model) => view('vendor.nova.resources.order.create-fields.address', [
                    'model' => $model,
                ])->render()
            )
                ->asHtml(),

            BelongsTo::make(__('Payment type'), 'paymentType', PaymentType::class),

            Text::make(
                name: __('Shipping'),
                attribute: fn ($model) => view('vendor.nova.resources.order.create-fields.shipping', [
                    'model' => $model,
                ])->render()
            )
                ->asHtml(),

            Text::make(
                name: __('Shipping time'),
                attribute: fn ($model) => view('vendor.nova.resources.order.create-fields.shipping-time', [
                    'model' => $model,
                ])->render()
            )
                ->asHtml(),

            Select::make(__('App'), 'source_app')
                ->displayUsingLabels()
                ->options(OS::apps())
                ->sortable(),

            Badge::make('Status')->map(OrderStatus::classes())->addTypes([
                'primary' => 'dark:bg-gray-900 bg-gray-600 text-white',
            ])->labels(OrderStatus::values())->sortable(),

            Text::make(__('Notes'), 'notes'),

            Text::make(__('Additional tax'), fn () => $resource->additional_tax.' TMT')
                ->canSeeWhen('systemUser', $resource),

            Text::make(__('Products total'), fn () => $resource->total().' TMT')
                ->canSeeWhen('systemUser', $resource),

            Text::make(__('Total'), fn () => $resource->fullPriceWithShipping().' TMT')
                ->canSeeWhen('systemUser', $resource),

            DateTime::make(__('Created at'), 'created_at')
                ->turkmenDate(),

            BelongsTo::make(__('Filled by:'), 'user', User::class),

            HasMany::make(__('Products'), 'items', OrderItem::class),
        ];
    }
}
