<?php

namespace App\Nova;

use App\Nova\Actions\SendEmail;
use App\Nova\Actions\VerifyWorker;
use App\Nova\Metrics\NewEngineers;
use Illuminate\Http\Request;
use Laravel\Nova\Actions\ExportAsCsv;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\File;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphMany;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Rinvex\Categories\Models\Category;

class Engineer extends Resource
{

    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
    public static $group = 'Users Group';
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Engineer::class;


    public static $with = ['user', 'LevelOfExperience', 'projects', 'services', 'contracts'];
    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'name';

    public function toSearchableArray()
    {
        $array = $this->toArray();

        $array = $this->transform($array);
        $array['categories']=[\App\Models\Category::find($this->category_id)->name];
        foreach ($this->specialties() as $specialty){
            $array['categories'][]=$specialty->name;
        }
        return $array;
    }


    public static function authorizable()
    {
        return false;
    }

    public static function authorizedToCreate(Request $request)
    {
        return false;
    }

    /**
     * Get the fields displayed by the resource.
     *
     * @param NovaRequest $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        return [
            DateTime::make('created_at')->filterable()->sortable()->hideWhenUpdating(),            new Panel('Personal Info', $this->personalInfoFields()),
            new Panel('Engineering licences and data', $this->licences()),
            MorphToMany::make('Specialties', 'Categories', \App\Nova\Category::class)->readonly(),
            MorphMany::make('Projects', 'projects', Project::class),
            MorphMany::make('Services', 'services', Service::class),
        ];
    }

    protected function personalInfoFields()
    {
        return [
            ID::make()->sortable(),

            //Avatar::make('Photo',"profile_photo_path")->disableDownload()->maxWidth(50),

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

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

            Boolean::make('Verified', function () {
                return $this->verified_at != null;
            }),

            Text::make("About", 'about')->hideFromIndex()->readonly(),

            Text::make('Tag line', 'tag_line')->hideFromIndex()->readonly(),

            Text::make('Category', function () {
                return Category::find($this->category_id)->name;
            })->filterable(),


            BelongsTo::make('Level Of Experience', 'levelOfExperience', LevelOfExperience::class)->filterable(),

        ];
    }

    protected function licences()
    {
        return [
            Text::make('Licence no.', 'engineering_licence_number')->hideFromIndex()->readonly(),
            File::make('Engineering licence', 'engineering_licence_path')->hideFromIndex()->readonly(),
            File::make('Engineering degree', 'engineering_degree_path')->hideFromIndex()->readonly(),
            File::make('Passport', 'passport_path')->hideFromIndex()->readonly(),
            File::make('CV', 'cv_path')->hideFromIndex()->readonly(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param NovaRequest $request
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [
            new NewEngineers(),
        ];
    }

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

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

    /**
     * Get the actions available for the resource.
     *
     * @param NovaRequest $request
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [
            ExportAsCsv::make(),
            (VerifyWorker::make())->showInline()->withoutConfirmation(),
            SendEmail::make(),
        ];
    }

    public function authorizedToUpdate(Request $request): bool
    {
        return false;
    }

}
