<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpProductOperationQueries\Pages;

use App\Business\Report\QueryCenterService;
use App\Filament\Resources\DberpProductOperationQueries\DberpProductOperationQueryResource;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;

class ListDberpProductOperationQueries extends ListRecords
{
    protected static string $resource = DberpProductOperationQueryResource::class;

    protected function getTableQuery(): Builder
    {
        $filters = QueryCenterService::productOperationFiltersFromTableState($this->tableFilters);
        $level = (string) (data_get($this->tableFilters, 'business_aggregate_level.value') ?? data_get($this->tableFilters, 'business_aggregate_level') ?? 'product');
        $service = app(QueryCenterService::class);

        if ($level === 'product_spec') {
            return $service->productSkuBusinessQuery($filters);
        }

        return $service->productBusinessQuery($filters);
    }
}
