<?php

namespace App\Nova;

use App\Nova\Metrics\NewProjects;
use App\Nova\Metrics\Projects;
use Illuminate\Http\Request;
use Laravel\Nova\Actions\ExportAsCsv;
use Laravel\Nova\Fields\BelongsTo;
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\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Rinvex\Categories\Models\Category;

class Project 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\Project::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(),
            Text::make('Title'),
            Text::make('Description')->hideFromIndex(),
            Text::make('Category', function () {
                return Category::find($this->category_id)->name;
            })->filterable(),
            Text::make('Status'),
            Text::make('Budget Type'),
            DateTime::make('created_at')->filterable()->sortable()->hideWhenUpdating(),            DateTime::make('Start At'),
            MorphToMany::make('Skills', 'skills', Skill::class),
            BelongsTo::make('Owner', 'user', User::class),
            HasMany::make('Proposal', 'proposals', Proposal::class),
//            HasOne::make('Contract','contract',Contract::class),
            HasMany::make('Files', 'files', ProjectFile::class),
            MorphTo::make('Worker', 'projectable'),
        ];
    }

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

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