<?php

namespace App\Filament\Widgets;

use App\Models\Order;
use Filament\Widgets\ChartWidget;

class OrderStatusChartWidget extends ChartWidget
{
    protected ?string $heading = 'Order Status Distribution (Last 30 Days)';

    protected static ?int $sort = 3;

    protected function getData(): array
    {
        $startDate = now()->subDays(30);

        $pending = Order::where('created_at', '>=', $startDate)->where('status', 'pending')->count();
        $processing = Order::where('created_at', '>=', $startDate)->where('status', 'processing')->count();
        $shipped = Order::where('created_at', '>=', $startDate)->where('status', 'shipped')->count();
        $delivered = Order::where('created_at', '>=', $startDate)->where('status', 'delivered')->count();
        $cancelled = Order::where('created_at', '>=', $startDate)->where('status', 'cancelled')->count();

        return [
            'datasets' => [
                [
                    'label' => 'Orders Count',
                    'data' => [$pending, $processing, $shipped, $delivered, $cancelled],
                    'backgroundColor' => [
                        '#3b82f6', // blue (pending)
                        '#f59e0b', // warning (processing)
                        '#06b6d4', // cyan (shipped)
                        '#10b981', // success (delivered)
                        '#ef4444', // danger (cancelled)
                    ],
                ],
            ],
            'labels' => ['Pending', 'Processing', 'Shipped', 'Delivered', 'Cancelled'],
        ];
    }

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