<?php

declare(strict_types=1);

namespace App\Filament\Widgets\Dashboard;

use App\Filament\Resources\FindingResource;
use App\Models\Finding;
use App\Models\InventoryItem;
use App\Models\Tenant;
use App\Support\Badges\BadgeDomain;
use App\Support\Badges\BadgeRenderer;
use App\Support\OpsUx\ActiveRuns;
use Filament\Facades\Filament;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Illuminate\Database\Eloquent\Builder;

class RecentDriftFindings extends TableWidget
{
    protected static bool $isLazy = false;

    public function table(Table $table): Table
    {
        $tenant = Filament::getTenant();

        return $table
            ->heading('Recent Drift Findings')
            ->query($this->getQuery())
            ->poll(fn (): ?string => ($tenant instanceof Tenant) && ActiveRuns::existForTenant($tenant) ? '10s' : null)
            ->paginated([10])
            ->columns([
                TextColumn::make('short_id')
                    ->label('ID')
                    ->state(fn (Finding $record): string => '#'.$record->getKey())
                    ->copyable()
                    ->copyableState(fn (Finding $record): string => (string) $record->getKey()),
                TextColumn::make('subject_display_name')
                    ->label('Subject')
                    ->placeholder('—')
                    ->limit(40)
                    ->tooltip(fn (Finding $record): ?string => $record->subject_display_name ?: null),
                TextColumn::make('severity')
                    ->badge()
                    ->formatStateUsing(BadgeRenderer::label(BadgeDomain::FindingSeverity))
                    ->color(BadgeRenderer::color(BadgeDomain::FindingSeverity))
                    ->icon(BadgeRenderer::icon(BadgeDomain::FindingSeverity))
                    ->iconColor(BadgeRenderer::iconColor(BadgeDomain::FindingSeverity)),
                TextColumn::make('status')
                    ->badge()
                    ->formatStateUsing(BadgeRenderer::label(BadgeDomain::FindingStatus))
                    ->color(BadgeRenderer::color(BadgeDomain::FindingStatus))
                    ->icon(BadgeRenderer::icon(BadgeDomain::FindingStatus))
                    ->iconColor(BadgeRenderer::iconColor(BadgeDomain::FindingStatus)),
                TextColumn::make('created_at')
                    ->label('Created')
                    ->since(),
            ])
            ->recordUrl(fn (Finding $record): ?string => $tenant instanceof Tenant
                ? FindingResource::getUrl('view', ['record' => $record], tenant: $tenant)
                : null)
            ->emptyStateHeading('No drift findings')
            ->emptyStateDescription('You\'re looking good — no drift findings to review yet.');
    }

    /**
     * @return Builder<Finding>
     */
    private function getQuery(): Builder
    {
        $tenant = Filament::getTenant();
        $tenantId = $tenant instanceof Tenant ? $tenant->getKey() : null;

        return Finding::query()
            ->addSelect([
                'subject_display_name' => InventoryItem::query()
                    ->select('display_name')
                    ->whereColumn('inventory_items.tenant_id', 'findings.tenant_id')
                    ->whereColumn('inventory_items.external_id', 'findings.subject_external_id')
                    ->limit(1),
            ])
            ->when($tenantId, fn (Builder $query) => $query->where('tenant_id', $tenantId))
            ->where('finding_type', Finding::FINDING_TYPE_DRIFT)
            ->latest('created_at');
    }
}
