<?php

namespace App\Filament\Widgets;

use App\Models\User;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Actions\Action;
use Filament\Widgets\TableWidget as BaseWidget;

class RecentUsersTable extends BaseWidget
{
    protected static ?int $sort = 5;
    protected int | string | array $columnSpan = 'full';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                User::query()->latest()->limit(10)
            )
            ->columns([
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Joined')
                    ->dateTime()
                    ->sortable(),
                Tables\Columns\TextColumn::make('name')
                    ->searchable(),
                Tables\Columns\TextColumn::make('email')
                    ->searchable(),
                Tables\Columns\IconColumn::make('role')
                    ->label('Admin')
                    ->boolean()
                    ->getStateUsing(fn ($record) => $record?->role === 'admin'),
            ])
            ->actions([
                Action::make('view')
                    ->url(fn ($record): ?string => $record ? url("/admin/users/{$record->id}/edit") : null)
                    ->icon('heroicon-m-eye'),
            ]);
    }
}
