<?php

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

use App\Models\Ecommerce\Product\Product\Product as ProductModel;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\Variant\VariantFieldsForCreate;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\Variant\VariantFieldsForDetail;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\Variant\VariantFieldsForIndex;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\Variant\VariantFieldsForPreview;
use App\Nova\Resources\Ecommerce\Product\Product\Concerns\Variant\VariantFieldsForUpdate;
use App\Rules\CommaSeparatedIntegers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;
use ShuvroRoy\NovaTabs\Traits\HasTabs;

class ProductVariant extends Resource
{
    /**
     * Supports tabs
     */
    use HasTabs;

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

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

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

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

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

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

    /**
     * Build an "index" query for the given resource.
     */
    public static function indexQuery(NovaRequest $request, $query)
    {
        if (Validator::make($request->all(), [
            'ids' => ['required', 'string', new CommaSeparatedIntegers],
        ])->passes()) {
            $query->whereIntegerInRaw('id', explode(',', $request->ids));

            return $query;
        }

        $query->whereNotNull('parent_id');

        return $query;
    }

    /**
     * Get the fields for index.
     */
    public function fieldsForIndex(NovaRequest $request): array
    {
        return VariantFieldsForIndex::make($request, $this);
    }

    /**
     * Get fields for preview
     */
    public function fieldsForPreview(NovaRequest $request): array
    {
        return VariantFieldsForPreview::make($request, $this);
    }

    /**
     * Get the fields for create.
     */
    public function fieldsForDetail(NovaRequest $request): array
    {
        return VariantFieldsForDetail::make($request, $this);
    }

    /**
     * Get the fields for create.
     */
    public function fieldsForCreate(NovaRequest $request): array
    {
        return VariantFieldsForCreate::make($request);
    }

    /**
     * Get the fields displayed by the resource on detail page.
     */
    public function fieldsForUpdate(NovaRequest $request): array
    {
        return VariantFieldsForUpdate::make($request, $this);
    }

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

    /**
     * After resource has been created
     */
    public static function afterCreate(NovaRequest $request, Model $model): void
    {
        VariantFieldsForCreate::afterCreate($request, $model);
    }

    /**
     * After resource has been updated
     */
    public static function afterUpdate(NovaRequest $request, Model $model): void
    {
        VariantFieldsForUpdate::afterUpdate($request, $model);
    }

    /**
     * Return the location to redirect the user after creation.
     *
     * @param  \Laravel\Nova\Resource  $resource
     * @return \Laravel\Nova\URL|string
     */
    public static function redirectAfterCreate(NovaRequest $request, $resource)
    {
        return sprintf('/resources/products/%s', $resource->parent_id);
    }

    /**
     * Return the location to redirect the user after update.
     *
     * @param  \Laravel\Nova\Resource  $resource
     * @return \Laravel\Nova\URL|string
     */
    public static function redirectAfterUpdate(NovaRequest $request, $resource)
    {
        return sprintf('/resources/products/%s', $resource->parent_id);
    }
}
