<?php

namespace App\Nova;

use Carbon\Carbon;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\BelongsToMany;
use Vyuldashev\NovaPermission\RoleBooleanGroup;
use Ebess\AdvancedNovaMediaLibrary\Fields\Files;
use Vyuldashev\NovaPermission\PermissionBooleanGroup;

class User extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\User::class;

    public static $with = ['media', 'roles', 'permissions', 'subscription'];

    public static $group = 'Users';

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public function title()
    {
        return $this->first_name;
    }

    public function subtitle()
    {
        return "Username: {$this->username}";
    }

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


    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            // Gravatar::make()->maxWidth(50),

            Text::make('Имя', 'Name', function () {
                return $this->first_name.' '.$this->last_name;
            })->onlyOnIndex(),

            Text::make('Username')
            ->rules('required', 'max:255')
            ->creationRules('unique:users,username')
            ->updateRules('unique:users,username,{{resourceId}}'),

            Text::make('Имя', 'first_name')
                ->onlyOnForms()
                ->rules('required', 'max:255'),

            Text::make('Фамилия', 'last_name')
                ->onlyOnForms(),

            Text::make('Tелефон', 'phone'),

            Text::make('ИНН', 'inn')->hideFromIndex(),
            Text::make('Расчетный счет', 'checking_account')->hideFromIndex(),
            Text::make('Бик банка', 'bik')->hideFromIndex(),

            Text::make('Почта', 'email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make('Password')
                ->onlyOnForms()
                ->creationRules('required', 'string', 'min:4')
                ->updateRules('nullable', 'string', 'min:4'),

            RoleBooleanGroup::make('Roles'),
            PermissionBooleanGroup::make('Permissions'),
            BelongsToMany::make('Votes')->fields(function () {
                return [
                    Text::make('Payment'),
                ];
            }),
            Boolean::make('Вер.: телефон', 'phone_verified'),
            Boolean::make('Вер.: паспорт', 'passport_verified'),
            Boolean::make('Загружен док.?', function () {
                return $this->media->count() ? true : false;
            })->onlyOnIndex(),
            Boolean::make('Подписка', function () {
                $last = $this->subscription()->first();
                if (empty($last)) {
                    return false;
                }
                if ($last->ends_at > Carbon::now()) {
                    return true;
                }
                return false;
            })->onlyOnIndex(),
            Files::make('Документ', 'documents')->readonly(),

        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}
