<?php

namespace App\Nova;

use App\Nova\Metrics\NewServiceOrders;
use App\Nova\Metrics\ServicesOrders;
use Illuminate\Http\Request;
use Laravel\Nova\Actions\ExportAsCsv;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class ServiceOrder 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\ServiceOrder::class;

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

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

    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('Tier', function () {
                return ($this->tier == 1 ? "Basic" : $this->tier == 2) ? "Standard" : "Pro";
            }),
            Text::make('Status'),
            BelongsTo::make('Service', 'service', Service::class),
            BelongsTo::make('User', 'user', User::class,),
            Image::make('client_signature','client_signature')->thumbnail(function ($client_signature){
                return $client_signature;
            })->displayUsing(
                function ($client_signature){
                    return $client_signature;
                }
            )->preview(function ($client_signature){
                return $client_signature;
            })->hideWhenCreating()->hideWhenUpdating(),
        ];
    }

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

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