<?php

namespace App\Nova;

use App\Nova\Actions\SendEmail;
use App\Nova\Actions\VerifyWorker;
use App\Nova\Metrics\NewFirms;
use Illuminate\Http\Request;
use Laravel\Nova\Actions\ExportAsCsv;
use Laravel\Nova\Fields\Avatar;
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 Firm 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\Firm::class;

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



    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('Firm Info', $this->personalInfoFields()),
            new Panel('Engineering licences and data', $this->licences()),
            MorphToMany::make('Specialties', 'Categories', \App\Nova\Category::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}}'),

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

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

            Text::make("Website", 'website')->hideFromIndex()->readonly(),

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

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

            BelongsTo::make('Firm Size', 'FirmSize', FirmSize::class)->filterable(),

            BelongsTo::make('Firm Type', 'FirmType', FirmType::class)->filterable(),

            MorphMany::make('Projects', 'projects', Project::class),

            MorphMany::make('Services', 'services', Service::class),
        ];
    }



    protected function licences()
    {
        return [
            Text::make('Commercial Registration no', 'commercial_registration_number')->hideFromIndex()->readonly(),
            File::make('Commercial Registration licence', 'commercial_registration_path')->hideFromIndex()->readonly(),
            File::make('Company Profile', 'company_profile_path')->hideFromIndex()->readonly(),
            File::make('Owner ID', 'owner_id_path')->hideFromIndex()->readonly(),
            File::make('Contact Person ID', 'contact_person_id_path')->hideFromIndex()->readonly(),
        ];
    }

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

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