<?php

namespace App\Filament\Widgets;

use App\Models\ActivityLog;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Database\Eloquent\Builder;

class ActivityLogWidget extends BaseWidget
{
    protected static ?int $sort = 2;
    protected int|string|array $columnSpan = 'full';
    protected static ?string $heading = 'Aktivitas Terbaru';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                ActivityLog::with('user')
                    ->where(function (Builder $query) {
                        $query->where('model_type', '!=', \App\Models\Activity::class)
                            ->orWhereIn('action', ['approved', 'verified', 'executed']);
                    })
                    ->latest()
                    ->limit(15)
            )
            ->columns([
                TextColumn::make('created_at')->label('Waktu')
                    ->dateTime('d M Y H:i')->sortable(),
                TextColumn::make('user.name')->label('Oleh')->default('Sistem'),
                TextColumn::make('action')->label('Aksi')->badge()
                    ->color(fn ($state) => match ($state) {
                        'created'        => 'success',
                        'verified'       => 'info',
                        'approved'       => 'success',
                        'rejected'       => 'danger',
                        'status_changed' => 'warning',
                        'voted'          => 'info',
                        default          => 'gray',
                    }),
                TextColumn::make('description')->label('Keterangan')->wrap(),
            ])
            ->paginated(false);
    }
}
