<?php

namespace App\Nova\Resources\Ecommerce\Payout;

use App\Models\Ecommerce\Payouts\Payout;
use App\Nova\Repos\Payouts\PayoutsRepo;
use App\Nova\Resource;
use App\Nova\Resources\Ecommerce\Product\Order\Actions\ExportEvidenceForProducts;
use App\Repositories\CMS\Icon\IconRepository;
use App\Repositories\Ecommerce\Channel\ChannelRepository;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
use Nurmuhammet\PayoutProducts\PayoutProducts;

class PayoutResource extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<\App\Models\Ecommerce\Payout>
     */
    public static $model = Payout::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 = ['channel'];

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

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

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

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

            Select::make(__('Channel'), 'channel_id')
                ->fullWidth()
                ->displayUsingLabels()
                ->searchable()
                ->options(ChannelRepository::values())
                ->rules('required')
                ->canSeeWhen('isAdmin', $this),

            Date::make(__('Start date'), 'start_date')
                ->rules('required')
                ->fullWidth()
                ->displayUsing(fn ($value) => $value->format('d.m.Y'))
                ->readonly(fn ($request) => $request->isUpdateOrUpdateAttachedRequest())
                ->sortable(),

            Date::make(__('End date'), 'end_date')
                ->rules('required')
                ->fullWidth()
                ->displayUsing(fn ($value) => $value->format('d.m.Y'))
                ->readonly(fn ($request) => $request->isUpdateOrUpdateAttachedRequest())
                ->sortable(),

            PayoutProducts::make(__('Products'), 'order_items_ids')
                ->dependsOn(['channel_id', 'start_date', 'end_date'], PayoutsRepo::orderItemIdsDependsOn())
                ->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
                    $model->total_sum = $request->total_sum;
                    $model->entrepreneur_total = $request->entrepreneur_total;
                    $model->postshop_total = $request->postshop_total;
                })
                ->products(PayoutsRepo::resolveOrderItems($request, $this))
                ->fullWidth()
                ->hideFromIndex(),

            Text::make(__('Hasap'), 'total_sum')
                ->fullWidth()
                ->exceptOnForms()
                ->sortable(),

            Text::make(__('Entrepreneur Total'), 'entrepreneur_total')
                ->fullWidth()
                ->exceptOnForms()
                ->sortable(),

            Text::make(__('POSTSHOP Total'), 'postshop_total')
                ->fullWidth()
                ->exceptOnForms()
                ->sortable(),

            Textarea::make(__('Notes'), 'notes')->fullWidth(),
        ];
    }

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

    /**
     * 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 [
            ExportEvidenceForProducts::make()
                ->confirmText('Delilnama çykarmak isleýarmisiňiz?')
                ->confirmButtonText('Hawa')
                ->cancelButtonText('Ýok')
                ->icon(IconRepository::make()->documentDownload()),
        ];
    }
}
