<?php

namespace App\Filament\Resources\Concerns;

use Filament\Widgets\WidgetConfiguration;

/**
 * KPI header for a list page, wired so the stats respect the ACTIVE table
 * filters instead of computing over the unfiltered query (a deliberate parity
 * choice at port time, but wrong for the new UX — filtering to one branch
 * should change the totals).
 *
 *   use HasKpiHeader;
 *   protected static string $kpiWidget = SalesOrderTotalsOverview::class;
 *
 * The widget must consume {@see Filament\Widgets\Concerns\InteractsWithPageTable}
 * (or declare the same #[Reactive] props) and compute its stats from
 * `getPageTableQuery()` — the page's filter-, search-, sort- and tab-applied
 * query. Each page render re-supplies the current table state as widget props,
 * and Livewire's #[Reactive] attribute pushes the update into the widget
 * without a round-trip of its own.
 */
trait HasKpiHeader
{
    protected function getHeaderWidgets(): array
    {
        return [
            new WidgetConfiguration(static::$kpiWidget, [
                'activeTab' => $this->activeTab ?? null,
                'paginators' => $this->paginators ?? [],
                'tableColumnSearches' => $this->tableColumnSearches ?? [],
                'tableFilters' => $this->tableFilters ?? null,
                'tableGrouping' => $this->tableGrouping ?? null,
                'tableRecordsPerPage' => $this->tableRecordsPerPage ?? null,
                'tableSearch' => $this->tableSearch ?? null,
                'tableSort' => $this->tableSort ?? null,
            ]),
        ];
    }
}
