<?php

namespace App\Nova;

use App\Models\User as UserModel;
use App\Nova\Resources\Ecommerce\Channel\Channel;
use App\Nova\Resources\Post\UserDocs\UserDocResource;
use App\Nova\Resources\System\Roles\Role;
use Illuminate\Validation\Rules;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\HasOne;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphMany;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Nurmuhammet\NovaInputmask\NovaInputmask;

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

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

    /**
     * Get the value that should be displayed to represent the resource.
     */
    public function title(): string
    {
        return $this->fullname;
    }

    /**
     * Indicates if the resource should be globally searchable.
     *
     * @var bool
     */
    public static $globallySearchable = true;

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'first_name', 'last_name', 'phone_number', 'email',
    ];

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

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

    /**
     * Get the fields displayed by the resource on index page.
     */
    public function fieldsForIndex(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),
            Text::make(__('Name'), 'fullname')->sortable(),
            Text::make(__('Phone'), 'phone_number')->sortable(),
            Text::make(__('Email'), 'email')->sortable(),
            Text::make(__('Roles'), 'role_names'),
            Boolean::make(__('Verified'), 'verified')->sortable(),
        ];
    }

    /**
     * Get the fields displayed by the resource.
     */
    public function fields(NovaRequest $request): array
    {
        return [
            Text::make(__('First name'), 'first_name')
                ->rules('required', 'string', 'max:255'),

            Text::make(__('Last name'), 'last_name')
                ->rules('required', 'string', 'max:255'),

            Text::make(__('Email'), 'email')
                ->rules('required', 'string', 'max:255')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            NovaInputmask::make(__('Phone'), 'phone_number')
                ->mask(phoneMaskFormat())
                ->storeRawValue()
                ->phoneNumberRule()
                ->canSeeWhen('systemUser', $this)
                ->sortable(),

            Boolean::make(__('Verified'), 'verified')
                ->default(true),

            Password::make(__('Password'), 'password')
                ->onlyOnForms()
                ->creationRules('required', Rules\Password::defaults())
                ->updateRules('nullable', Rules\Password::defaults()),

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

            HasOne::make(__('Documents'), 'documents', UserDocResource::class),
            MorphToMany::make(__('Roles'), 'roles', Role::class),
            MorphMany::make(__('Channels'), 'channels', Channel::class),
        ];
    }

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

    /**
     * Get the filters available for the resource.
     */
    public function filters(NovaRequest $request): array
    {
        return [
            new Filters\UserRoleFilter,
            new Filters\VerifiedUsersFilter,
        ];
    }

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

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