<?php

namespace App\Modules\GlobalOrder\Nova\Resources;

use App\Models\Ecommerce\Product\Order\Payment\OrderPayment;
use App\Models\Ecommerce\Product\Order\Status\OrderStatus;
use App\Modules\GlobalOrder\Models\GlobalOrder;
use App\Nova\Resource;
use App\Nova\Resources\User\UserSearchResource;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Badge;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Nurmuhammet\NovaInputmask\NovaInputmask;

class GlobalOrderResource extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<GlobalOrder>
     */
    public static $model = GlobalOrder::class;

    /**
     * The relationships that should be eager loaded on index queries.
     *
     * @var array<int, string>
     */
    public static $with = ['user'];

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

    /**
     * The columns that should be searched.
     *
     * @var array<int, string>
     */
    public static $search = [
        'customer_name', 'customer_phone',
    ];

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

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

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

            Images::make(__('Image'), 'uploads')
                ->conversionOnIndexView('thumb200x200'),

            Text::make(__('Name'), 'customer_name')
                ->rules(['required', 'string', 'max:255']),

            Text::make(__('Address'), 'customer_address')
                ->rules(['required', 'string', 'max:255']),

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

            Text::make(__('Order website'), 'order_website')
                ->rules(['required', 'string', 'max:255']),

            Text::make(__('Notes'), 'notes')
                ->rules(['required', 'string', 'max:255']),

            Select::make(__('App'), 'payment_type')
                ->displayUsingLabels()
                ->options(OrderPayment::oldButGoodTypes())
                ->sortable(),

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

            DateTime::make(__('Created at'), 'created_at')
                ->displayUsing(fn ($value) => $value->format('H:i, d.m.Y'))
                ->exceptOnForms()
                ->sortable(),

            BelongsTo::make(__('User'), 'user', UserSearchResource::class)
                ->searchable()
                ->withSubtitles(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @return array
     */
    public function lenses(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [];
    }

    /**
     * Determine if the resource should be available for the given request.
     *
     * @return bool
     */
    public static function authorizedToViewAny(Request $request)
    {
        return auth()->user() && auth()->user()->isAdmin();
    }

    /**
     * Determine if the current user can view the given resource.
     *
     * @return bool
     */
    public function authorizedToView(Request $request)
    {

        return auth()->user() && auth()->user()->isAdmin();
    }

    /**
     * Determine if the current user can create new resources.
     *
     * @return bool
     */
    public static function authorizedToCreate(Request $request)
    {
        return auth()->user() && auth()->user()->isAdmin();
    }

    /**
     * Determine if the current user can update the given resource.
     */
    public function authorizedToUpdate(Request $request): bool
    {
        return auth()->user() && auth()->user()->isAdmin();
    }

    /**
     * Determine if the current user can delete the given resource.
     */
    public function authorizedToDelete(Request $request): bool
    {
        if (auth()->user()->isMe()) {
            return true;
        }

        return false;
    }
}
