<?php

namespace App\Filament\Resources\StatsOverviewResource\Widgets;

use App\Models\News;
use App\Models\Publication;
use App\Models\ReformProject;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget\Card;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class StatsOverview extends BaseWidget
{
    protected function getStats(): array
    {

        return [
            Stat::make('Total Users', User::count())
                ->description('Registered users')
                ->descriptionIcon('heroicon-o-user-group')
                ->color('primary')
                ->chart($this->getMonthlyData(User::class))
                ->extraAttributes(['class' => 'hover:shadow-lg transition-shadow']),

            Stat::make('Total Reform Projects', ReformProject::count())
                ->description('Submitted reforms')
                ->descriptionIcon('heroicon-o-document-text')
                ->color('success')
                ->chart($this->getMonthlyData(ReformProject::class))
                ->extraAttributes(['class' => 'hover:shadow-lg transition-shadow']),

            Stat::make('Publications', Publication::count())
                ->description('Available documents')
                ->descriptionIcon('heroicon-o-document-text')
                ->color('info')
                ->chart($this->getMonthlyData(Publication::class))
                ->extraAttributes(['class' => 'hover:shadow-lg transition-shadow']),

            Stat::make('News Published', News::count())
                ->description('Latest updates')
                ->descriptionIcon('heroicon-o-newspaper')
                ->color('warning')
                ->chart($this->getMonthlyData(News::class))
                ->extraAttributes(['class' => 'hover:shadow-lg transition-shadow']),

            Stat::make('Active Reform Projects', ReformProject::where('status', 'In Progress')->count())
                ->description('Currently in progress')
                ->descriptionIcon('heroicon-o-arrow-trending-up')
                ->color('info')
                ->chart($this->getStatusData('active'))
                ->extraAttributes(['class' => 'hover:shadow-lg transition-shadow']),
        ];
    }

    private function getMonthlyData(string $model): array
    {
        return $model::query()
            ->selectRaw('MONTH(created_at) as month, COUNT(*) as count')
            ->whereYear('created_at', now()->year)
            ->groupBy('month')
            ->orderBy('month')
            ->get()
            ->pluck('count')
            ->toArray();
    }

    private function getStatusData(string $status): array
    {
        return ReformProject::query()
            ->selectRaw('MONTH(created_at) as month, COUNT(*) as count')
            ->where('status', $status)
            ->whereYear('created_at', now()->year)
            ->groupBy('month')
            ->orderBy('month')
            ->get()
            ->pluck('count')
            ->toArray();
    }
}












<?php

namespace App\Filament\Resources\ReformNewsChartResource\Widgets;

use App\Models\News;
use App\Models\ReformProject;
use Filament\Widgets\ChartWidget;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;

class ReformNewsChart extends ChartWidget
{

    protected static ?string $heading = 'Reforms vs News (Last 6 Months)';
    protected static ?int $sort = 2;
    protected static ?string $maxHeight = '300px';
    protected static ?string $pollingInterval = '30s';

    protected function getData(): array
    {
        // Get data for last 6 months
        $months = collect(range(5, 0))->map(function ($i) {
            return now()->subMonths($i)->format('M Y');
        });

        // Reform projects data
        $reformData = $this->getDataForModel(ReformProject::class, 6);
        $newsData = $this->getDataForModel(News::class, 6);

        return [
            'datasets' => [
                [
                    'label' => 'Reform Projects',
                    'data' => $reformData,
                    'backgroundColor' => '#10B981',
                    'borderColor' => '#047857',
                    'borderWidth' => 1,
                ],
                [
                    'label' => 'News Published',
                    'data' => $newsData,
                    'backgroundColor' => '#F59E0B',
                    'borderColor' => '#B45309',
                    'borderWidth' => 1,
                ],
            ],
            'labels' => $months,
        ];
    }

    private function getDataForModel(string $model, int $months): array
    {
        $data = [];
        for ($i = $months - 1; $i >= 0; $i--) {
            $date = now()->subMonths($i);
            $count = $model::whereYear('created_at', $date->year)
                ->whereMonth('created_at', $date->month)
                ->count();
            $data[] = $count;
        }
        return $data;
    }

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

