<?php

namespace App\Filament\Widgets;

use App\Enums\AssetStatusEnum;
use App\Models\Asset;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class LatestAssetsTable extends BaseWidget
{
    protected static ?string $heading = 'Aset Terbaru';

    protected static ?int $sort = 4;

    protected int | string | array $columnSpan = 'full';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Asset::query()
                    ->with(['category', 'opd'])
                    ->latest()
                    ->limit(10)
            )
            ->columns([

                Tables\Columns\TextColumn::make('name')
                    ->label('Nama Aset')
                    ->searchable()
                    ->limit(40),

                Tables\Columns\TextColumn::make('category.name')
                    ->label('Kategori')
                    ->badge(),

                Tables\Columns\TextColumn::make('opd.name')
                    ->label('OPD')
                    ->limit(30),

                Tables\Columns\TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->formatStateUsing(fn ($state) => $state instanceof AssetStatusEnum ? $state->label() : AssetStatusEnum::from($state)->label())
                    ->color(fn ($state) => $state instanceof AssetStatusEnum ? $state->color() : AssetStatusEnum::from($state)->color()),

                Tables\Columns\IconColumn::make('is_featured')
                    ->label('Unggulan')
                    ->boolean(),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('Dibuat')
                    ->since(),
            ])
            ->paginated(false);
    }
}