<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use App\Business\Workbench\BusinessOverviewMetricService;
use App\Services\DberpPermissionService;
use Filament\Widgets\ChartWidget;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Auth;

class PurchaseSalesCompareChartWidget extends ChartWidget
{
    protected static bool $isLazy = true;

    protected ?string $pollingInterval = null;

    protected ?string $maxHeight = '280px';

    protected int|string|array $columnSpan = [
        'default' => 'full',
        'lg' => 1,
    ];

    protected string $color = 'warning';

    public static function canView(): bool
    {
        return app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::VIEW_WORKBENCH_OVERVIEW);
    }

    public function getHeading(): string|Htmlable|null
    {
        return __('dberp.widgets.business_overview_workbench.chart_purchase_sales_heading');
    }

    public function getDescription(): string|Htmlable|null
    {
        return __('dberp.widgets.business_overview_workbench.chart_purchase_sales_description');
    }

    protected function getType(): string
    {
        return 'bar';
    }

    /**
     * @return array<string, mixed>
     */
    protected function getData(): array
    {
        return app(BusinessOverviewMetricService::class)->purchaseSalesComparison();
    }

    /**
     * @return array<string, mixed>|null
     */
    protected function getOptions(): ?array
    {
        return [
            'maintainAspectRatio' => false,
            'plugins' => [
                'legend' => [
                    'display' => true,
                ],
            ],
            'scales' => [
                'y' => [
                    'beginAtZero' => true,
                ],
            ],
        ];
    }
}
