<?php

namespace App\Nova;

use App\Nova\Metrics\NewServices;
use App\Nova\Metrics\Services;
use Illuminate\Http\Request;
use Laravel\Nova\Actions\ExportAsCsv;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphTo;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;

class Service extends Resource
{
    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
    public static $group = 'Projects/Services Group';
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Service::class;

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

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

    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 [
            ID::make()->sortable(),
            DateTime::make('created_at')->filterable()->sortable()->hideWhenUpdating(),
            Text::make('Title'),
            Textarea::make('description'),
            Boolean::make('Three Tiers'),
            Text::make('Status'),
//            Text::make('Category',function (){
//                return Category::find($this->category_id)->name;
//            })->filterable(),
            HasMany::make('Tiers Options', 'tierOptions', ServiceTierOption::class),
            MorphTo::make('Worker', 'serviceable'),
            HasMany::make('Files', 'images', ServiceImage::class),
            HasMany::make('Orders', 'Orders', ServiceOrder::class),
            MorphToMany::make('Skills', 'skills', Skill::class),
        ];
    }

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

    /**
     * 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(),

        ];
    }

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

    protected function tiersFilds()
    {
        $fields = [
            Number::make('Basic Tier Price'),
            Number::make('Basic Tier Duration'),
        ];
        if ($this->three_tiers) {
            $fields = array_merge($fields, [
                Number::make('Standard Tier Price'),
                Number::make('Standard Tier Duration'),
                Number::make('Pro Tier Price'),
                Number::make('Pro Tier Duration'),
            ]);
        }
        return $fields;
    }
}
