<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\MorphMany;
use App\Domain\Feeds\Enums\StatusEnum;
use Laravel\Nova\Http\Requests\NovaRequest;
use App\Nova\Filters\SubscriptionStatusFilter;
use App\Domain\Feeds\Models\Feed as ModelsFeed;
use Ebess\AdvancedNovaMediaLibrary\Fields\Files;
use Carbon\Carbon;
use Laravel\Nova\Fields\Boolean;

class FeedApproved extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = ModelsFeed::class;
    public static $group = 'Content';

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

    /**
     * Get the value that should be displayed to represent the resource.
     *
     * @return string
     */
    public function title()
    {
        if(empty($this->title)){
            return $this->id;
        }
        return $this->title;
    }

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

    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query->where('status', '!=', 1)->where('is_ads', false)->with('user.subscription');
    }

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),
            BelongsTo::make('Creator', 'user', 'App\Nova\User')
                ->onlyOnIndex(),

            Select::make('Тип пользователя', 'userType')->options([
                1 => 'Физ. лицо',
                2 => 'Самозанятый',
                3 => 'Юридическое лицо',
                4 => 'ИП',
            ])->displayUsingLabels()->readonly(),

            Boolean::make('Подписка', function () {
                $last = $this->user->subscription->first();
                if ($last && $last->ends_at > Carbon::now()) {
                    return true;
                }
                return false;
            })->onlyOnIndex(),

            Files::make('Media Preview', 'preview')->readonly(),
            Files::make('Media', 'common')->readonly(),
            Files::make('Media Paid', 'paid')->readonly(),
            Text::make('Title'),
            Text::make('Slug')->readonly()->hideFromIndex(),
            Select::make('Type')->options([
                'images' => 'Изображение',
                'musics' => 'Аудио',
                'videos' => 'Видео',
            ])->readonly()->displayUsingLabels(),
            Text::make('Price')->readonly(),
            Textarea::make('Body')->alwaysShow()->hideFromIndex(),
            HasMany::make('Comments'),
            HasMany::make('Complaints'),
            DateTime::make('Created At')->format('DD MMM YYYY'),
            DateTime::make('Updated At')->hideFromIndex(),

            Text::make('StatusText', function ($model) {
                if($model->status === StatusEnum::PENDING()){
                    return 'Нужно проверить';
                }
                if($model->status === StatusEnum::BANNED()){
                    return 'Рассмотрено - есть проблемы';
                }
                if($model->status === StatusEnum::EDITABLE()){
                    return 'Нужно повторно рассмотреть';
                }
            })->onlyOnIndex(),

            Select::make('Status')->options([
                (string) StatusEnum::APPROVED() => 'Одобрено',
                (string) StatusEnum::BANNED() => 'Есть недочеты',
            ])->hideFromIndex()->displayUsingLabels()->hideWhenCreating(),

            Textarea::make('Status Note')->hideWhenCreating(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [
            new SubscriptionStatusFilter(),
        ];
    }

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

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}
