<?php

namespace App\Nova\Actions;

use App\Models\VerificationRequest;
use App\Notifications\VerificationRequestAccepted;
use App\Notifications\VerificationRequestDeclined;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class declineVerificationRequest extends Action
{
    use InteractsWithQueue, Queueable;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        foreach ($models as $model) {
            if($model->action ==null){
                $model->action = false;
                $model->notes = $fields->Notes;
                $model->save();
                $model->entity->notify(new VerificationRequestDeclined($fields->Notes));
            }else{
                return Action::danger('this request has ' . $model->action ? 'already verified': 'already declined');
            }

        }
        return Action::message('Declined!');
    }

    /**
     * Get the fields available on the action.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        return [
            Text::make('Notes'),
        ];
    }
}
