<?php
namespace App\Services;

use App\Models\AvailableApp;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ApiService
{
     public int $perPage;
    public function __construct(Request $request) {
        $this->perPage = filter_var(  // Quantidade por página dinâmica e se nao passado dados por (default 10)
            $request->input('per_page', 10),
            FILTER_VALIDATE_INT,
            ["options" => ["default" => 10, "min_range" => 1, "max_range" => 100]]
        );
    }


    public function getEnterpriseAvailableApps (Request $request)
    {
        $query = AvailableApp::query()->when($request->filled('searcher'), fn($q) => $q->where('searcher', 'like', "%{$request->searcher}%"))
            ->when($request->filled('app_size'), fn($q) => $q->where('app_size', $request->app_size))
            ->when($request->filled('app_price'), fn($q) => $q->where('app_price', $request->app_price))

            ->when($request->filled('start_date') && $request->filled('end_date'), fn($q) => $q->whereBetween('created_at', [
                $request->start_date . ' 00:00:00',
                $request->end_date   . ' 23:59:59'
            ]));

        $enterprise_apps = $query
        ->latest()
        ->paginate($this->perPage); // Paginação dinâmica
        return $enterprise_apps;   

    }
}
