<?php
// routes/web.php

use App\Http\Controllers\Auth\CustomerAuthController;
use App\Http\Controllers\CustomerDashboardController;
use App\Http\Controllers\Api\WebhookController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// Public Routes
Route::get('/', function () {
    return view('welcome');
})->name('home');

/*
|--------------------------------------------------------------------------
| Customer Authentication Routes
|--------------------------------------------------------------------------
*/



Route::get('customer/transactions/history', [CustomerDashboardController::class, 'transactionsHistory'])
    ->name('customer.transactions.history')
    ->middleware('auth:customer');



Route::prefix('customer')->name('customer.')->group(function () {
    // Login Routes
    Route::get('/login', [CustomerAuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [CustomerAuthController::class, 'login'])->name('login.submit');
    
    // Registration Routes (Multi-step)
    Route::get('/register', [CustomerAuthController::class, 'showRegistrationForm'])->name('register');
    
    // Step 1: Email & Password
    Route::post('/register/step1', [CustomerAuthController::class, 'step1'])->name('register.step1');
    
    // Step 2: Profile Picture
    Route::get('/register/step2', [CustomerAuthController::class, 'showStep2Form'])->name('register.step2');
    Route::post('/register/step2', [CustomerAuthController::class, 'step2'])->name('register.step2.submit');
    
    // Step 3: Personal Information
    Route::get('/register/step3', [CustomerAuthController::class, 'showStep3Form'])->name('register.step3');
    Route::post('/register/step3', [CustomerAuthController::class, 'step3'])->name('register.step3.submit');
    
    // Step 4: NRC Upload & Verification
    Route::get('/register/step4', [CustomerAuthController::class, 'showStep4Form'])->name('register.step4');
    Route::post('/register/step4', [CustomerAuthController::class, 'step4'])->name('register.step4.submit');
    
    // NRC Verification AJAX Endpoint
    Route::post('/register/verify-nrc', [CustomerAuthController::class, 'verifyNrc'])->name('register.verify.nrc');
    
    // Logout
    Route::post('/logout', [CustomerAuthController::class, 'logout'])->name('logout');
    
    // Forgot Password
    Route::get('/forgot-password', [CustomerAuthController::class, 'showForgotPasswordForm'])->name('password.request');
    Route::post('/forgot-password', [CustomerAuthController::class, 'sendOtp'])->name('password.otp');
    
    // ADD THIS LINE - The missing resend route
    Route::post('/forgot-password/resend', [CustomerAuthController::class, 'resendOtp'])->name('password.resend');

    // Step 2: OTP Verification
    Route::get('/verify-otp', [CustomerAuthController::class, 'showVerifyOtpForm'])->name('password.reset.form');
    Route::post('/verify-otp', [CustomerAuthController::class, 'verifyOtp'])->name('password.verify.submit');

    // Step 3: Final Password Change
    Route::get('/reset-password-final', [CustomerAuthController::class, 'showFinalResetForm'])->name('password.reset.final');
    Route::post('/reset-password-final', [CustomerAuthController::class, 'resetPassword'])->name('password.update');
    
    // ADD THIS LINE:
    Route::get('/payments/method-selection', [App\Http\Controllers\CustomerDashboardController::class, 'showMethodSelection'])->name('payments.method');
    
    // ADD THIS ROUTE for the polling mechanism
    Route::get('/payments/status/{referenceId}', [CustomerDashboardController::class, 'checkPaymentStatusPublic'])->name('payments.status');
    
    // Inside your customer middleware group
    Route::post('/payments/details', [CustomerDashboardController::class, 'showPaymentDetails'])->name('payments.details');
    
    // You will also need a route to handle the actual processing later:
    Route::post('/payments/process', [App\Http\Controllers\CustomerDashboardController::class, 'processPayment'])->name('payments.process');
    
    
});

/*
|--------------------------------------------------------------------------
| Protected Customer Routes
|--------------------------------------------------------------------------
*/


Route::middleware(['auth:customer'])->prefix('customer')->name('customer.')->group(function () {
    // Dashboard
    Route::get('/dashboard', [CustomerDashboardController::class, 'index'])->name('dashboard');
    
  
      Route::get('/documents/{document}/view', [CustomerDashboardController::class, 'viewDocument'])
        ->name('documents.view'); // Changed from 'customer.documents.view'
    
    Route::get('/documents/{document}/download', [CustomerDashboardController::class, 'downloadDocument'])
        ->name('documents.download'); // Changed from 'customer.documents.download'
    
  
  
  
    
         // Loan product requirements AJAX route
      Route::get('/get-requirements/{id}', [CustomerDashboardController::class, 'getLoanProductRequirements']);
    
    // Profile Management
    Route::prefix('profile')->name('profile.')->group(function () {
        Route::get('/', [CustomerDashboardController::class, 'profile'])->name('show');
        Route::put('/', [CustomerDashboardController::class, 'updateProfile'])->name('update');
        // ADD THIS LINE HERE:
        Route::post('/update-photo', [CustomerDashboardController::class, 'updatePhoto'])->name('update-photo');
    });
    
    // Payment Routes
    Route::prefix('payments')->name('payments.')->group(function () {
        Route::get('/make', [CustomerDashboardController::class, 'showMakePayment'])->name('make');
        Route::post('/process', [CustomerDashboardController::class, 'processPayment'])->name('process');
        
        // New API-style payment endpoint
        Route::post('/mobile-money/initiate', [CustomerDashboardController::class, 'initiateMobileMoneyPayment'])
            ->name('mobile-money.initiate');
    });
    
    // Loan Application Routes
    Route::prefix('loan-application')->name('loan.application.')->group(function () {
        Route::get('/', [CustomerDashboardController::class, 'showLoanApplication'])->name('show');
        Route::post('/', [CustomerDashboardController::class, 'submitLoanApplication'])->name('submit');
    });
    
    // Applications Management Routes
    Route::prefix('applications')->name('applications.')->group(function () {
        Route::get('/', [CustomerDashboardController::class, 'myApplications'])->name('index');
        Route::get('/{id}', [CustomerDashboardController::class, 'showApplication'])->name('show');
        Route::get('/{id}/edit', [CustomerDashboardController::class, 'editApplication'])->name('edit');
        Route::put('/{id}', [CustomerDashboardController::class, 'updateApplication'])->name('update');
        Route::post('/{id}/withdraw', [CustomerDashboardController::class, 'withdrawApplication'])->name('withdraw');
        Route::get('/{id}/download', [CustomerDashboardController::class, 'downloadApplication'])->name('download');
        
        
        
 

    // Submit loan application (multi-step with documents)
    Route::post('/loan-application/submit',[CustomerDashboardController::class, 'submitLoanApplication']
    )->name('customer.loan.application.submit');
  
        
        
    });
    
    // Loans Management Routes
    Route::prefix('loans')->name('loans.')->group(function () {
        Route::get('/', [CustomerDashboardController::class, 'myLoans'])->name('index');
        Route::get('/{id}', [CustomerDashboardController::class, 'showLoan'])->name('show');
    });
    
    // Application Statistics (API-like endpoints)
    Route::prefix('api')->name('api.')->group(function () {
        Route::get('/application-stats', [CustomerDashboardController::class, 'getApplicationStats'])->name('application.stats');
        Route::get('/loan-stats', [CustomerDashboardController::class, 'getLoanStats'])->name('loan.stats');
        
        // Mobile Money API endpoints
        Route::prefix('mobile-money')->name('mobile-money.')->group(function () {
            Route::post('/initiate-payment', [CustomerDashboardController::class, 'initiateMobileMoneyPayment'])
                ->name('initiate-payment');
            Route::post('/check-status', [CustomerDashboardController::class, 'checkPaymentStatus'])
                ->name('check-status');
        });
    });
});

Route::prefix('webhooks')->group(function () {
    Route::post('/lipila/collection', [WebhookController::class, 'handleCollectionCallback']);
    Route::post('/lipila/disbursement', [WebhookController::class, 'handleDisbursementCallback']);
    Route::get('/test', [WebhookController::class, 'testWebhook']);
    Route::get('/logs', [WebhookController::class, 'viewLogs']);
});

/*
|--------------------------------------------------------------------------
| Fallback Route
|--------------------------------------------------------------------------
*/
Route::fallback(function () {
    return redirect()->route('home');
})->name('fallback');