<?php

namespace App\Nova;

use App\Models\QuestionAnswer;
use App\Nova\Filters\UserAcademy;
use App\Nova\Filters\UserTerm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Text;

use \App\Models\User as UserModel;
use App\Models\UserQuestionAnswers;
use App\Nova\Filters\ApplicationDateFilter;
use App\Nova\Filters\UserAttemptStatus;

class User extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */





    public static $model = \App\Models\User::class;

    public static $perPageOptions = [50, 100, 150];

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

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

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

        $possibleAnswerIdsForThisUser = UserQuestionAnswers::where('user_id',  $request->viaResourceId)->pluck('question_answer_id');
        $answersMap = QuestionAnswer::whereIn('id', $possibleAnswerIdsForThisUser)->pluck('correct', 'id');

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

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

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

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

            Text::make('Phone'),

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

            BelongsToMany::make('Questions')->fields(function () use ($answersMap) {
                return [
                    Text::make('Correct', 'question_answer_id', function ($value) use ($answersMap) {
                        $result = '';
                        if (isset($answersMap[$value])) {
                            $result = $answersMap[$value]
                                ? '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" aria-labelledby="check-circle" role="presentation" class="fill-current text-success"><path d="M12 22a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm0-2a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm-2.3-8.7l1.3 1.29 3.3-3.3a1 1 0 0 1 1.4 1.42l-4 4a1 1 0 0 1-1.4 0l-2-2a1 1 0 0 1 1.4-1.42z"></path></svg>'
                                : '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" aria-labelledby="x-circle" role="presentation" class="fill-current text-danger"><path d="M4.93 19.07A10 10 0 1 1 19.07 4.93 10 10 0 0 1 4.93 19.07zm1.41-1.41A8 8 0 1 0 17.66 6.34 8 8 0 0 0 6.34 17.66zM13.41 12l1.42 1.41a1 1 0 1 1-1.42 1.42L12 13.4l-1.41 1.42a1 1 0 1 1-1.42-1.42L10.6 12l-1.42-1.41a1 1 0 1 1 1.42-1.42L12 10.6l1.41-1.42a1 1 0 1 1 1.42 1.42L13.4 12z"></path></svg>';
                        }


                        return $result;
                    })->asHtml()
                ];
            }),

            Text::make('Total questions', function () {
                return $this->questions()->count();
            }),

            Text::make('Correctly answered questions', function () {
                return $this->questionAnswers()->where('correct', true)->count();
            }),

            Text::make('Time', function () {
                if ($this->attempt()->exists()) {
                    if ($this->attempt->end_time != null) {
                        return \Carbon\Carbon::parse($this->attempt->end_time)->diff($this->attempt->start_time)->format('%i min %s seconds');
                    } else {
                        return 'Expired';
                    }
                } else {
                    return 'Never started';
                }
            }),
            BelongsTo::make('Term')
                ->exceptOnForms(),

            Text::make('Award')
                ->sortable()
        ];
    }

    /**
     * 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 [
            new ApplicationDateFilter,
            new UserAttemptStatus,
            new UserTerm,
            new UserAcademy,
        ];
    }

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