<?php

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

use App\Models\Ecommerce\Product\Order\OrderItem;
use DateTimeInterface;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Metrics\Trend;

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

            return $this->countByDays($request, OrderItem::where('channel_id', $channel->id))->showLatestValue();
        }

        return $this->countByDays($request, OrderItem::class)->showLatestValue();
    }

    /**
     * Get the ranges available for the metric.
     */
    public function ranges(): array
    {
        return [
            30 => __('30 Days'),
            60 => __('60 Days'),
            90 => __('90 Days'),
        ];
    }

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

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

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