<?php

namespace App\Http\Controllers\Web\Bo;

use App\Http\Controllers\Controller;
use App\Models\Payout;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Illuminate\View\View;

class PayoutController extends Controller
{
    public function __invoke(Request $request): View
    {
        $search = trim((string) $request->string('q'));
        $status = trim((string) $request->string('status'));
        $provider = trim((string) $request->string('provider'));
        $dateFrom = trim((string) $request->string('date_from'));
        $dateTo = trim((string) $request->string('date_to'));
        $amountMin = $request->string('amount_min')->toString();
        $amountMax = $request->string('amount_max')->toString();
        $hasPaymentMethodsTable = Schema::hasTable('user_payment_methods');

        $payoutsQuery = Payout::query()
            ->with(['liner:id,name,email'])
            ->when($search !== '', function ($query) use ($search): void {
                $query->where(function ($subQuery) use ($search): void {
                    $subQuery
                        ->where('provider_reference', 'like', '%' . $search . '%')
                        ->orWhereHas('liner', function ($userQuery) use ($search): void {
                            $userQuery
                                ->where('name', 'like', '%' . $search . '%')
                                ->orWhere('email', 'like', '%' . $search . '%');
                        });
                });
            })
            ->when($status !== '', fn ($query) => $query->where('status', $status))
            ->when($provider !== '', fn ($query) => $query->where('provider', $provider))
            ->when($dateFrom !== '', fn ($query) => $query->whereDate('requested_at', '>=', $dateFrom))
            ->when($dateTo !== '', fn ($query) => $query->whereDate('requested_at', '<=', $dateTo))
            ->when(is_numeric($amountMin), fn ($query) => $query->where('amount', '>=', (float) $amountMin))
            ->when(is_numeric($amountMax), fn ($query) => $query->where('amount', '<=', (float) $amountMax));

        if ($hasPaymentMethodsTable) {
            $payoutsQuery->with('paymentMethod:id,label,provider');
        }

        $payouts = $payoutsQuery
            ->latest()
            ->paginate(20)
            ->withQueryString();

        return view('bo.payouts.index', [
            'payouts' => $payouts,
            'search' => $search,
            'status' => $status,
            'provider' => $provider,
            'dateFrom' => $dateFrom,
            'dateTo' => $dateTo,
            'amountMin' => $amountMin,
            'amountMax' => $amountMax,
            'hasPaymentMethodsTable' => $hasPaymentMethodsTable,
            'providers' => Payout::query()
                ->select('provider')
                ->distinct()
                ->orderBy('provider')
                ->pluck('provider')
                ->filter()
                ->values(),
        ]);
    }
}
