<?php

namespace App\Nova\Resources\Ecommerce\Channel;

use App\Models\Ecommerce\Channel\Channel as ChannelModel;
use App\Models\User;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Channel\Fields\ChannelFieldsForIndex;
use App\Nova\Resources\Ecommerce\Product\Product\Product;
use App\Nova\Resources\User\UserSearchResource;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphMany;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\URL;
use Laravel\Nova\Http\Requests\NovaRequest;
use Outl1ne\NovaSortable\Traits\HasSortableRows;
use Trin4ik\NovaSwitcher\NovaSwitcher;

class Channel extends Resource
{
    /**
     * Sortable behavior
     */
    use HasSortableRows;

    /**
     * The model the resource corresponds to.
     *
     * @var class-string<ChannelModel>
     */
    public static $model = ChannelModel::class;

    /**
     * The relationships that should be eager loaded on index queries.
     *
     * @var array
     */
    public static $with = ['media', 'user'];

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

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

    /**
     * Get the displayable label of the resource.
     */
    public static function label(): string
    {
        return __('Channels');
    }

    /**
     * Get the displayable singular label of the resource.
     */
    public static function singularLabel(): string
    {
        return __('Channel');
    }

    /**
     * Fields for index
     */
    public function fieldsForIndex(): array
    {
        return ChannelFieldsForIndex::make();
    }

    /**
     * Get the fields displayed by the resource.
     */
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),

            Images::make(__('Image'), 'uploads')
                ->conversionOnIndexView('thumb400x400')
                ->rules('required')
                ->required(),

            Text::make(__('Name'), 'name')
                ->rules(['required', 'string', 'max:255']),

            Text::make(__('Description'), 'description')
                ->rules(['nullable', 'string', 'max:255']),

            URL::make('URL'),
            Hidden::make('is_default')->default(true),
            Hidden::make('timezone')->default('Asia/Ashgabat'),

            DateTime::make(__('Created at'), 'created_at')
                ->default(now())
                ->displayUsing(fn ($value) => $value->format('H:i, d.m.Y'))
                ->exceptOnForms()
                ->sortable(),

            BelongsTo::make(__('User'), 'user', UserSearchResource::class)
                ->searchable()
                ->withSubtitles()
                ->readonly(fn () => $this->id === tmpostChannel()->id)
                ->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
                    $model->channelables_type = User::class;
                    $model->channelables_id = $request->input('user');
                }),

            NovaSwitcher::make(__('Active'), 'is_visible')
                ->default(true),

            MorphMany::make(__('Products'), 'products', Product::class),
        ];
    }

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

    /**
     * Get the filters available for the resource.
     *
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @return array
     */
    public function lenses(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [];
    }
}
