<?php

namespace App\Filament\Widgets;

use App\Models\Tenant;
use Filament\Tables;
use Filament\Widgets\TableWidget as BaseWidget;

class TopTenantsByRevenue extends BaseWidget
{
    protected static ?string $heading = null;

    protected static ?int $sort = 2;

    protected ?string $pollingInterval = '120s';

    protected function getHeading(): ?string
    {
        return __('admin.widgets.top_tenants_by_revenue.heading');
    }

    public function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->query(
                Tenant::query()
                    ->withSum('purchases', 'price')
                    ->withCount('purchases')
                    ->orderByDesc('purchases_sum_price')
                    ->limit(10)
            )
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label(__('admin.common.tenant'))
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('purchases_sum_price')
                    ->label(__('admin.widgets.top_tenants_by_revenue.total'))
                    ->money('EUR')
                    ->sortable(),
                Tables\Columns\TextColumn::make('purchases_count')
                    ->label(__('admin.widgets.top_tenants_by_revenue.count'))
                    ->badge()
                    ->sortable(),
            ])
            ->paginated(false);
    }
}
