<?php

namespace App\Nova\Resources\CMS\Media;

use App\Models\CMS\Media\Banner as BannerModel;
use App\Models\System\Settings\OS;
use App\Nova\Filters\AppTypeFilter;
use App\Nova\Filters\VisableFilter;
use App\Nova\Resource;
use App\Repositories\CMS\Media\Banner\BannerRepository;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\URL;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Outl1ne\NovaSortable\Traits\HasSortableRows;
use Trin4ik\NovaSwitcher\NovaSwitcher;

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

    /**
     * The model the resource corresponds to.
     *
     * @var class-string<\App\Models\CMS\Media\Banner>
     */
    public static $model = BannerModel::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 = [
        'name',
    ];

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

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

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

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

                Images::make(__('Image'), 'main')
                    ->conversionOnIndexView('thumb200x200')
                    ->rules('required')
                    ->required(),

                Select::make(__('App'), 'app')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(OS::apps())
                    ->default(OS::WEBSITE),

                Select::make(__('Place'), 'place')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(BannerModel::places())
                    ->rules('required'),

                Text::make(__('Name'), 'title')
                    ->translatable()
                    ->rules('nullable', 'string', 'max:255'),

                URL::make(__('Link'), 'link')
                    ->displayUsing(fn () => $this->link)
                    ->rules('nullable', 'string', 'max:2048')
                    ->hideFromIndex(),

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

            new Panel(__('Additional information'), [
                Select::make(__('Resource type to attach'), 'resource_type')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(BannerModel::attachableResources())
                    ->rules('nullable')
                    ->onlyOnForms(),

                Select::make(__('Resource to attach'), 'resource_id')
                    ->displayUsingLabels()
                    ->searchable()
                    ->rules('nullable')
                    ->dependsOn(['resource_type'], function ($field, $request, $formData) {
                        if ($formData->resource_type) {
                            $field->options(BannerRepository::getNovaResource($formData->resource_type))->rules('required');
                        }
                    })
                    ->onlyOnForms(),

                Text::make(
                    __('Attached resource'),
                    fn () => view('vendor.nova.resources.banner.resource-type', ['resource' => $this])->render()
                )->asHtml()->hideFromIndex(),

                Text::make(__('Note'), 'description')
                    ->translatable()
                    ->rules('nullable', 'string', 'max:255')
                    ->hideFromIndex(),
            ]),
        ];
    }

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

    /**
     * Get the filters available for the resource.
     */
    public function filters(NovaRequest $request): array
    {
        return [
            new VisableFilter(),
            new AppTypeFilter(),
        ];
    }

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

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