<?php

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

use App\Models\Ecommerce\Product\Order\OrderItem as OrderItemModel;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Order\OrderItem\OrderItemFieldsForCreate;
use App\Nova\Resources\Ecommerce\Product\Order\OrderItem\OrderItemFieldsForIndex;
use App\Nova\Resources\Ecommerce\Product\Order\OrderItem\OrderItemFieldsForUpdate;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class OrderItem extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<OrderItemModel>
     */
    public static $model = OrderItemModel::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 = [
        'id',
    ];

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

    /**
     * The number of resources to show per page via relationships.
     *
     * @var int
     */
    public static $perPageViaRelationship = 20;

    /**
     * Build an "index" query for the given resource.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     */
    public static function indexQuery(NovaRequest $request, $query): Builder
    {
        $user = $request->user();

        if ($user->hasRole('vendor')) {
            $query->with(['product', 'product.media'])
                ->where('channel_id', $request->user()->channel()->id);
        } else {
            $query->with(['product', 'product.media', 'channel']);
        }

        return $query;
    }

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

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

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

    /**
     * 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/orders/%s', $resource->order_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/orders/%s', $resource->order_id);
    }

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

            Text::make(__('Quantity'), 'quantity'),
            BelongsTo::make(__('Order'), 'order', Order::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 [];
    }
}
