<?php

use App\Http\Controllers\Web\Bo\AuthController as BoAuthController;
use App\Http\Controllers\Web\Bo\DashboardController as BoDashboardController;
use App\Http\Controllers\Web\Bo\DisputeModerationController;
use App\Http\Controllers\Web\Bo\FinanceController;
use App\Http\Controllers\Web\Bo\FinanceGatewaySettingsController;
use App\Http\Controllers\Web\Bo\FinancePaymentController;
use App\Http\Controllers\Web\Bo\FinancePayoutController;
use App\Http\Controllers\Web\Bo\KycController;
use App\Http\Controllers\Web\Bo\KycModerationController;
use App\Http\Controllers\Web\Bo\MissionController as BoMissionController;
use App\Http\Controllers\Web\Bo\PayoutController as BoPayoutController;
use App\Http\Controllers\Web\Bo\PushController;
use App\Http\Controllers\Web\Bo\ProofModerationController;
use App\Http\Controllers\Web\Bo\UserController as BoUserController;
use App\Http\Controllers\Web\PasswordResetController;
use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $availableLiners = User::query()
        ->where('role', 'liner')
        ->where('is_available', true)
        ->whereNotNull('current_latitude')
        ->whereNotNull('current_longitude')
        ->orderByDesc('current_location_updated_at')
        ->limit(120)
        ->get(['id', 'name', 'current_latitude', 'current_longitude', 'current_location_updated_at'])
        ->map(fn (User $liner): array => [
            'id' => $liner->id,
            'name' => $liner->name,
            'latitude' => (float) $liner->current_latitude,
            'longitude' => (float) $liner->current_longitude,
            'updated_at' => $liner->current_location_updated_at?->toIso8601String(),
        ])
        ->values();

    return view('welcome', [
        'availableLiners' => $availableLiners,
    ]);
});

Route::get('/health', function () {
    return response()->json([
        'status' => 'ok',
        'service' => 'lineup-france-web',
    ]);
});

Route::get('/paypal/return', function (\Illuminate\Http\Request $request) {
    return response()->view('paypal-return', [
        'status' => 'success',
        'title' => 'Validation PayPal terminée',
        'message' => "La liaison PayPal a été validée. Revenez dans l'application pour terminer l'ajout du moyen de paiement.",
        'tokenId' => (string) $request->query('approval_token_id', ''),
        'sessionId' => (string) $request->query('approval_session_id', ''),
    ]);
})->name('paypal.return');

Route::get('/paypal/cancel', function (\Illuminate\Http\Request $request) {
    return response()->view('paypal-return', [
        'status' => 'cancelled',
        'title' => 'Validation PayPal annulée',
        'message' => "La liaison PayPal a été annulée. Revenez dans l'application pour réessayer si nécessaire.",
        'tokenId' => (string) $request->query('approval_token_id', ''),
        'sessionId' => (string) $request->query('approval_session_id', ''),
    ]);
})->name('paypal.cancel');

Route::get('/reset-password/{token}', [PasswordResetController::class, 'show'])
    ->name('password.reset');
Route::post('/reset-password', [PasswordResetController::class, 'store'])
    ->name('password.update');

Route::get('/login', function () {
    return redirect()->route('bo.login');
})->name('login');

Route::middleware('guest')->prefix('bo')->name('bo.')->group(function (): void {
    Route::get('/login', [BoAuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [BoAuthController::class, 'login'])->name('login.perform');
});

Route::middleware(['auth', 'role:admin'])->prefix('bo')->name('bo.')->group(function (): void {
    Route::get('/dashboard', BoDashboardController::class)->name('dashboard');
    Route::resource('users', BoUserController::class)->except(['show']);
    Route::post('/missions/{mission}/applications/{application}/accept', [BoMissionController::class, 'acceptApplication'])->name('missions.applications.accept');
    Route::post('/missions/{mission}/applications/{application}/reject', [BoMissionController::class, 'rejectApplication'])->name('missions.applications.reject');
    Route::resource('missions', BoMissionController::class);
    Route::get('/kyc', KycController::class)->name('kyc.index');
    Route::get('/payouts', BoPayoutController::class)->name('payouts.index');
    Route::get('/finance', FinanceController::class)->name('finance');
    Route::post('/kyc/{kycDocument}/approve', [KycModerationController::class, 'approve'])->name('kyc.approve');
    Route::post('/kyc/{kycDocument}/reject', [KycModerationController::class, 'reject'])->name('kyc.reject');
    Route::post('/proofs/{missionProof}/approve', [ProofModerationController::class, 'approve'])->name('proofs.approve');
    Route::post('/proofs/{missionProof}/reject', [ProofModerationController::class, 'reject'])->name('proofs.reject');
    Route::post('/disputes/{missionDispute}/resolve', [DisputeModerationController::class, 'resolve'])->name('disputes.resolve');
    Route::post('/disputes/{missionDispute}/reject', [DisputeModerationController::class, 'reject'])->name('disputes.reject');
    Route::post('/push/test', [PushController::class, 'sendTest'])->name('push.test');
    Route::post('/finance/payments/{payment}/retry', [FinancePaymentController::class, 'retry'])->name('finance.payments.retry');
    Route::post('/finance/payments/{payment}/mark-paid', [FinancePaymentController::class, 'markPaid'])->name('finance.payments.mark_paid');
    Route::post('/finance/payments/{payment}/mark-failed', [FinancePaymentController::class, 'markFailed'])->name('finance.payments.mark_failed');
    Route::post('/finance/payouts/{payout}/process', [FinancePayoutController::class, 'process'])->name('finance.payouts.process');
    Route::post('/finance/payouts/{payout}/mark-paid', [FinancePayoutController::class, 'markPaid'])->name('finance.payouts.mark_paid');
    Route::post('/finance/payouts/{payout}/mark-failed', [FinancePayoutController::class, 'markFailed'])->name('finance.payouts.mark_failed');
    Route::post('/finance/gateway-settings', [FinanceGatewaySettingsController::class, 'update'])->name('finance.gateway_settings.update');
    Route::post('/logout', [BoAuthController::class, 'logout'])->name('logout');
});
