<?php

use App\Http\Controllers\Api\V1\AddressAdminController;
use App\Http\Controllers\Api\V1\ApiKeyController;
use App\Http\Controllers\Api\V1\AuthController;
use App\Http\Controllers\Api\V1\BalanceController;
use App\Http\Controllers\Api\V1\CheckoutController;
use App\Http\Controllers\Api\V1\CurrencyController;
use App\Http\Controllers\Api\V1\CustomerController;
use App\Http\Controllers\Api\V1\EmailVerificationController;
use App\Http\Controllers\Api\V1\EstimateController;
use App\Http\Controllers\Api\V1\HealthController;
use App\Http\Controllers\Api\V1\InvoiceController;
use App\Http\Controllers\Api\V1\MerchantDashboardController;
use App\Http\Controllers\Api\V1\KycDocumentController;
use App\Http\Controllers\Api\V1\OperatorKycController;
use App\Http\Controllers\Api\V1\MerchantApprovalController;
use App\Http\Controllers\Api\V1\OperatorDashboardController;
use App\Http\Controllers\Api\V1\OperationsAlertController;
use App\Http\Controllers\Api\V1\PaymentController;
use App\Http\Controllers\Api\V1\PayoutApprovalController;
use App\Http\Controllers\Api\V1\PayoutBatchController;
use App\Http\Controllers\Api\V1\PayoutController;
use App\Http\Controllers\Api\V1\PayoutWalletController;
use App\Http\Controllers\Api\V1\SettlementController;
use App\Http\Controllers\Api\V1\SubMerchantController;
use App\Http\Controllers\Api\V1\SubscriptionController;
use App\Http\Controllers\Api\V1\SubscriptionPlanController;
use App\Http\Controllers\Api\V1\TreasuryCollectionController;
use App\Http\Controllers\Api\V1\TreasuryDashboardController;
use App\Http\Controllers\Api\V1\TreasuryWalletController;
use App\Http\Controllers\Api\V1\TwoFactorController;
use App\Http\Controllers\Api\V1\WebhookEndpointController;
use App\Http\Controllers\Api\V1\WebhookProbeController;
use App\Http\Controllers\Api\V1\WebhookReplayController;
use App\Http\Controllers\Api\V1\WebhookTestController;
use Illuminate\Support\Facades\Route;

Route::get('health/ready', HealthController::class);

Route::prefix('v1')->group(function () {
    Route::prefix('auth')->group(function () {
        Route::post('register', [AuthController::class, 'register'])->middleware('throttle:5,1');
        Route::post('login', [AuthController::class, 'login'])->middleware('throttle:dashboard-login');
        Route::post('logout', [AuthController::class, 'logout'])->middleware('auth:sanctum');
    });

    Route::get('email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])
        ->middleware('signed')
        ->name('verification.verify');

    Route::middleware(['auth:sanctum', 'merchant.bind'])->group(function () {
        Route::get('email/verify', [EmailVerificationController::class, 'notice'])->name('verification.notice');
        Route::post('email/verification-notification', [EmailVerificationController::class, 'send'])->middleware('throttle:6,1')->name('verification.send');

        Route::middleware(['merchant.verified', 'merchant.active'])->group(function () {
            Route::get('me', [AuthController::class, 'me']);
            Route::apiResource('api-keys', ApiKeyController::class)->only(['index', 'store', 'destroy']);
            Route::post('api-keys/{apiKey}/rotate', [ApiKeyController::class, 'rotate']);
            Route::post('2fa/setup', [TwoFactorController::class, 'setup']);
            Route::post('2fa/enable', [TwoFactorController::class, 'enable']);
            Route::post('2fa/step-up', [TwoFactorController::class, 'stepUp']);
            Route::get('kyc/documents', [KycDocumentController::class, 'index']);
            Route::post('kyc/documents', [KycDocumentController::class, 'store']);
            Route::get('kyc/documents/{id}', [KycDocumentController::class, 'show']);
            Route::apiResource('webhook-endpoints', WebhookEndpointController::class)->only(['index', 'store', 'destroy']);
            Route::post('webhook-deliveries/{deliveryId}/replay', WebhookReplayController::class);
            Route::post('webhooks/probe', WebhookProbeController::class);
        });
    });

    Route::middleware(['auth:sanctum', 'merchant.bind', 'merchant.verified', 'merchant.active', 'operator'])->prefix('operator')->group(function () {
        Route::post('payouts/{payoutId}/approve', [PayoutApprovalController::class, 'approve']);
        Route::get('dashboard', OperatorDashboardController::class);
        Route::get('treasury/dashboard', TreasuryDashboardController::class);
        Route::get('treasury/wallets', [TreasuryWalletController::class, 'index']);
        Route::post('treasury/wallets', [TreasuryWalletController::class, 'store']);
        Route::post('treasury/wallets/sync-balances', [TreasuryWalletController::class, 'sync']);
        Route::get('treasury/collections', [TreasuryCollectionController::class, 'index']);
        Route::post('treasury/collections', [TreasuryCollectionController::class, 'store']);
        Route::post('treasury/collections/sync', [TreasuryCollectionController::class, 'sync']);
        Route::get('operations/alerts', [OperationsAlertController::class, 'index']);
        Route::post('operations/alerts/{id}/acknowledge', [OperationsAlertController::class, 'acknowledge']);
        Route::get('merchants', [MerchantApprovalController::class, 'index']);
        Route::post('merchants/{merchant}/approve', [MerchantApprovalController::class, 'approve']);
        Route::post('merchants/{merchant}/decline', [MerchantApprovalController::class, 'decline']);
        Route::get('kyc/documents', [OperatorKycController::class, 'index']);
        Route::post('kyc/documents/{id}/approve', [OperatorKycController::class, 'approve']);
        Route::post('kyc/documents/{id}/reject', [OperatorKycController::class, 'reject']);
    });

    Route::middleware(['api.key', 'api.environment', 'merchant.bind', 'api.merchant.verified', 'merchant.active'])->group(function () {
        Route::get('dashboard/summary', MerchantDashboardController::class)->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::get('currencies', CurrencyController::class)->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('estimate', EstimateController::class)->middleware(['api.scope:payments:read', 'throttle:estimate']);
        Route::get('customers', [CustomerController::class, 'index'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('customers', [CustomerController::class, 'store'])->middleware(['api.scope:payments:write', 'throttle:payment-create']);
        Route::get('customers/{id}', [CustomerController::class, 'show'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::patch('customers/{id}', [CustomerController::class, 'update'])->middleware(['api.scope:payments:write']);
        Route::delete('customers/{id}', [CustomerController::class, 'destroy'])->middleware(['api.scope:payments:write']);
        Route::post('invoices', [InvoiceController::class, 'store'])->middleware(['api.scope:payments:write', 'throttle:payment-create']);
        Route::get('invoices', [InvoiceController::class, 'index'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::get('invoices/{id}', [InvoiceController::class, 'show'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('invoices/{id}/send-email', [InvoiceController::class, 'sendEmail'])->middleware(['api.scope:payments:write']);
        Route::get('invoices/{id}/pdf', [InvoiceController::class, 'pdf'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::get('checkout/{paymentId}', [CheckoutController::class, 'show'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('checkout/{paymentId}/select-currency', [CheckoutController::class, 'selectCurrency'])->middleware(['api.scope:payments:write']);
        Route::post('subscriptions/plans', [SubscriptionPlanController::class, 'store'])->middleware(['api.scope:payments:write']);
        Route::get('subscriptions/plans', [SubscriptionPlanController::class, 'index'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('subscriptions', [SubscriptionController::class, 'store'])->middleware(['api.scope:payments:write']);
        Route::get('subscriptions', [SubscriptionController::class, 'index'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('subscriptions/{id}/cancel', [SubscriptionController::class, 'cancel'])->middleware(['api.scope:payments:write']);
        Route::get('payout-wallets', [PayoutWalletController::class, 'index'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::post('payout-wallets', [PayoutWalletController::class, 'store'])->middleware(['api.scope:payouts:write']);
        Route::get('payout-wallets/{id}', [PayoutWalletController::class, 'show'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::patch('payout-wallets/{id}', [PayoutWalletController::class, 'update'])->middleware(['api.scope:payouts:write']);
        Route::delete('payout-wallets/{id}', [PayoutWalletController::class, 'destroy'])->middleware(['api.scope:payouts:write']);
        Route::post('payout-batches', [PayoutBatchController::class, 'store'])->middleware(['api.scope:payouts:write', 'throttle:payout-create']);
        Route::get('payout-batches', [PayoutBatchController::class, 'index'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::get('payout-batches/{id}', [PayoutBatchController::class, 'show'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::post('payout-batches/{id}/approve', [PayoutBatchController::class, 'approve'])->middleware(['api.scope:payouts:write']);
        Route::post('payments', [PaymentController::class, 'store'])->middleware(['api.scope:payments:write', 'throttle:payment-create']);
        Route::get('payments', [PaymentController::class, 'index'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::get('payments/{paymentId}', [PaymentController::class, 'show'])->middleware(['api.scope:payments:read', 'throttle:api-read']);
        Route::post('payments/{paymentId}/cancel', [PaymentController::class, 'cancel'])->middleware('api.scope:payments:write');
        Route::get('balances', BalanceController::class)->middleware(['api.scope:balances:read', 'throttle:api-read']);
        Route::post('payouts', [PayoutController::class, 'store'])->middleware(['api.scope:payouts:write', 'throttle:payout-create']);
        Route::get('payouts', [PayoutController::class, 'index'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::post('settlements', [SettlementController::class, 'store'])->middleware(['api.scope:payouts:write', 'throttle:payout-create']);
        Route::get('settlements', [SettlementController::class, 'index'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::get('settlements/{id}', [SettlementController::class, 'show'])->middleware(['api.scope:payouts:read', 'throttle:api-read']);
        Route::post('webhooks/test', WebhookTestController::class)->middleware('api.scope:webhooks:write');
        Route::get('sub-merchants', [SubMerchantController::class, 'index'])->middleware('api.scope:merchants:read');
        Route::post('sub-merchants', [SubMerchantController::class, 'store'])->middleware('api.scope:merchants:write');
        Route::get('addresses', [AddressAdminController::class, 'index'])->middleware('api.scope:wallets:read');
        Route::patch('addresses/{id}', [AddressAdminController::class, 'update'])->middleware('api.scope:wallets:write');
    });
});


