<?php

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

use App\Models\Ecommerce\Product\Order\Order;
use Illuminate\Support\Facades\DB;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Metrics\Value;

class NewOrders extends Value
{
    /**
     * Calculate the value of the metric.
     *
     * @return mixed
     */
    public function calculate(NovaRequest $request)
    {
        if ($request->user()->hasRole('vendor')) {
            $channel = $request->user()->channel();

            $vendor_orders = DB::table('order_items')->where('channel_id', $channel->id)->pluck('order_id');

            return $this->count($request, Order::whereIntegerInRaw('id', $vendor_orders));
        }

        return $this->count($request, Order::class);
    }

    /**
     * Get the ranges available for the metric.
     */
    public function ranges(): array
    {
        return [
            'TODAY' => __('Today'),
            30 => __('30 Days'),
            60 => __('60 Days'),
            365 => __('365 Days'),
            'MTD' => __('Month To Date'),
            'QTD' => __('Quarter To Date'),
            'YTD' => __('Year To Date'),
        ];
    }

    /**
     * Determine the amount of time the results of the metric should be cached.
     *
     * @return \DateTimeInterface|\DateInterval|float|int|null
     */
    public function cacheFor()
    {
        return now()->addMinutes(5);
    }

    /**
     * Get the URI key for the metric.
     */
    public function uriKey(): string
    {
        return 'orders-new-orders';
    }

    /**
     * Get the displayable name of the metric
     */
    public function name(): string
    {
        return __('Orders');
    }
}
