<?php

namespace App\Filament\Widgets;

use App\Models\Document;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class KnowledgeBaseStatsWidget extends BaseWidget
{
    protected static ?int $sort = 1;

    protected function getStats(): array
    {
        $totalDocuments = Document::count();
        $completedDocuments = Document::where('conversion_status', 'completed')->count();
        $failedDocuments = Document::where('conversion_status', 'failed')->count();

        $conversionRate = $totalDocuments > 0
            ? round(($completedDocuments / $totalDocuments) * 100, 1)
            : 0;

        return [
            Stat::make('文档总数', $totalDocuments)
                ->description('知识库中的文档总数')
                ->descriptionIcon('heroicon-m-document-text')
                ->color('primary'),

            Stat::make('转换完成', $completedDocuments)
                ->description("成功率: {$conversionRate}%")
                ->descriptionIcon('heroicon-m-check-circle')
                ->color('success'),

            Stat::make('转换失败', $failedDocuments)
                ->description('需要重新处理')
                ->descriptionIcon('heroicon-m-x-circle')
                ->color('danger'),
        ];
    }
}
