<?php

namespace App\Nova\Resources\Ecommerce\Product\Coupon;

use App\Models\Ecommerce\Product\Coupon\Coupon as CouponModel;
use App\Nova\Resource;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Trin4ik\NovaSwitcher\NovaSwitcher;

class Coupon extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<CouponModel>
     */
    public static $model = CouponModel::class;

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

    /**
     * Indicates if the resource should be globally searchable.
     *
     * @var bool
     */
    public static $globallySearchable = true;

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

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

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

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

            Text::make(__('Code'), 'code')
                ->rules('required', 'max:255')
                ->creationRules('unique:coupons,code'),

            Select::make(__('Discount Type'), 'discount_type')
                ->displayUsingLabels()
                ->searchable()
                ->options(CouponModel::discountTypes())
                ->default(CouponModel::defaultDiscountType())
                ->rules('required'),

            Text::make(__('Discount Value'), 'discount_value')
                ->dependsOn('discount_type', function ($field, $request, $formData) {
                    if ($formData->discount_type === 'percentage') {
                        $field->rules('required', 'numeric', 'min:1', 'max:100');
                    }
                })
                ->rules('required', 'numeric'),

            Text::make(__('Notes'), 'notes')
                ->rules('nullable', 'max:255')
                ->hideFromIndex(),

            NovaSwitcher::make(__('Active'), 'is_active')
                ->default(true),
        ];
    }

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