<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Ysv\NovaLeader\NovaLeader;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Http\Requests\NovaRequest;

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

    public static $group = 'Subscription';

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

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

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {

        return [
            ID::make(__('ID'), 'id')->sortable(),
            Number::make('Процент сайта', 'procent_site')->min(1)->step(0.01)->rules('required')->default(function ($request) {
                return nova_get_setting('vote_procent_site');
            }),
            Number::make('Процент лидера', 'procent_top')->min(1)->step(0.01)->rules('required')->default(function ($request) {
                return nova_get_setting('vote_procent_leader');
            }),
            Number::make('Процент локального лидера', 'procent_local')->min(1)->step(0.01)->rules('required')->default(function ($request) {
                return nova_get_setting('vote_procent_local_leader');
            }),
            Boolean::make('Платный режим включен', 'type')->rules('required')->default(function ($request) {
                return nova_get_setting('vote_paid_mode');
            }),
            DateTime::make('Created At')->format('DD MMM YYYY'),
            BelongsToMany::make('Users')->fields(function () {
                return [
                    Text::make('Payment'),
                ];
            }),
        ];
    }

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

    /**
     * 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 [];
    }

    // protected static function afterCreationValidation(NovaRequest $request, $validator)
    // {
    //     // $validator->errors()->add('field', 'Something is wrong with this field!');
    // }
}
