<?php

namespace App\Filament\Widgets;

use App\Models\Scan;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class RecentScans extends BaseWidget
{
    protected static ?int $sort = 2;

    public function table(Table $table): Table
    {
        return $table
            ->query(Scan::query()->with("tag")->latest("scanned_at")->limit(20))
            ->columns([
                Tables\Columns\TextColumn::make("scanned_at")->dateTime("d/m H:i"),
                Tables\Columns\TextColumn::make("tag.tag_code")->label("Tag"),
                Tables\Columns\TextColumn::make("location_shared")->label("Location")->formatStateUsing(fn ($state) => $state ? "shared" : "no"),
                Tables\Columns\TextColumn::make("scanner_phone")->label("Finder phone"),
                Tables\Columns\IconColumn::make("alert_sent")->boolean()->label("Alert"),
            ]);
    }
}
