<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;

/*
|--------------------------------------------------------------------------
| Controllers
|--------------------------------------------------------------------------
*/

use App\Http\Controllers\ClaimsController;
use App\Http\Controllers\CoversReportController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DependantController;
use App\Http\Controllers\GroupController;
use App\Http\Controllers\KycDocumentController;
use App\Http\Controllers\MemberController;
use App\Http\Controllers\MemberManagementController;
use App\Http\Controllers\MemberprofileController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\OtpController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\PaymentProcessingController;
use App\Http\Controllers\PaymentReportController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SendSmsController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SmsController;
use App\Http\Controllers\TrendsController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\UserLogController;

/*
|--------------------------------------------------------------------------
| Dashboard
|--------------------------------------------------------------------------
*/

Route::middleware('auth')->group(function () {
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
});

Route::get('/claims/{reference}', [ClaimsController::class, 'getByReference']);
/*
|--------------------------------------------------------------------------
| Authenticated Routes
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->group(function () {

    /*
    |--------------------------------------------------------------------------
    | Notifications
    |--------------------------------------------------------------------------
    */
    Route::get('/mark-as-read', function () {
        auth()->user()->unreadNotifications->markAsRead();
        return back();
    })->name('markRead');

    Route::get('/notify', [NotificationController::class, 'index'])->name('notifications');
    Route::post('/search_date_notification', [NotificationController::class, 'searchdate']);

    /*
    |--------------------------------------------------------------------------
    | Settings
    |--------------------------------------------------------------------------
    */
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::post('/settings', [SettingsController::class, 'store'])->name('settings.store');

    /*
    |--------------------------------------------------------------------------
    | Trends & Analytics
    |--------------------------------------------------------------------------
    */
    Route::prefix('trends')->group(function () {
        Route::get('/current-members-by-gender', [TrendsController::class, 'currentMembersByGender']);
        Route::get('/members-by-age-group', [TrendsController::class, 'membersByAgeGroup']);
        Route::get('/membership-growth', [TrendsController::class, 'membershipGrowthTrends']);
        Route::get('/contribution-rates', [TrendsController::class, 'contributionRates']);
        Route::get('/payment-patterns', [TrendsController::class, 'paymentPatterns']);
    });

    /*
    |--------------------------------------------------------------------------
    | Covers Reports
    |--------------------------------------------------------------------------
    */
    Route::prefix('covers-report')->group(function () {
        Route::get('/popular', [CoversReportController::class, 'popularCoverStructures']);
        Route::get('/benefit-by-age', [CoversReportController::class, 'benefitPopularityByAgeGroup']);
        Route::get('/lapse-analysis', [CoversReportController::class, 'lapseAnalysisByCover']);
        Route::get('/new-vs-lapsed', [CoversReportController::class, 'newVsLapsedMembers']);
    });

    /*
    |--------------------------------------------------------------------------
    | Claims
    |--------------------------------------------------------------------------
    */


    Route::get('/member/{member}/claims/create', [ClaimsController::class, 'create'])
        ->name('claims.create');

    Route::post('/member/{member}/claims', [ClaimsController::class, 'store'])
        ->name('claims.store');


    // Claims Routes without "admin" prefix
    Route::get('claims', [ClaimsController::class, 'index'])->name('claims.index');


    Route::post('/claims/{claim}/review', [ClaimsController::class, 'review'])->name('claims.review');

    Route::get('claim/{id}', [ClaimsController::class, 'show'])->name('claims.show');
    Route::get('claim/edit/{claim}', [ClaimsController::class, 'edit'])->name('claims.edit');
    Route::delete('claims/{claim}', [ClaimsController::class, 'destroy'])->name('claims.delete');
    Route::put('/claims/{claim}', [ClaimsController::class, 'update'])->name('claims.update');
    Route::get('claims/{claim}/print', [ClaimsController::class, 'print'])->name('claims.print');


    Route::get('member/{id}/claims', [ClaimsController::class, 'memberClaims'])->name('claims.member');

    Route::post(
        '/claims/{claim}/email-underwriter',
        [ClaimsController::class, 'emailUnderwriter']
    )->name('claims.emailUnderwriter');

    Route::get(
        '/claims/view/{token}',
        [ClaimsController::class, 'viewByToken']
    )->name('claims.view.token');





    // Claim Routes
    Route::get('claims/{claim}/edit', [ClaimsController::class, 'edit'])->name('claims.edit');
    //Route::post('claims/{claim}/update', [ClaimsController::class, 'update'])->name('claims.update');

    // Claim Documents
    Route::post('claims/{claim}/documents', [ClaimsController::class, 'uploadDocument'])->name('claims.documents.upload');
    Route::delete('claims/documents/{document}/delete', [ClaimsController::class, 'deleteDocument'])->name('claims.documents.delete');


    Route::post('claims/{claim}/approve', [ClaimsController::class, 'approve'])
        ->name('claims.approve');

    Route::post('claims/{claim}/reject', [ClaimsController::class, 'reject'])
        ->name('claims.reject');


    Route::get('/claims-management', [ClaimsController::class, 'statsIndex']);


    /*
    |--------------------------------------------------------------------------
    | Members
    |--------------------------------------------------------------------------
    */
    Route::get('/member', [MemberController::class, 'index']);
    Route::post('/member', [MemberController::class, 'store']);
    Route::get('/member/{id}', [MemberController::class, 'view']);
    Route::put('/member/{id}', [MemberController::class, 'update']);
    Route::get('/member_profile/{id}', [MemberprofileController::class, 'index']);
  // Refresh Status
    Route::get('/allstatus', [MemberprofileController::class, 'status']);

    Route::get('/print_profile/{id}', [MemberController::class, 'print']);
    Route::get('/history/{id}', [MemberController::class, 'history']);

    Route::get('/active', [MemberController::class, 'active']);
    Route::get('/pending', [MemberController::class, 'pending']);
    Route::get('/elapsed', [MemberController::class, 'elapsed']);
    Route::get('/restart', [MemberController::class, 'restart']);
        Route::get('/deceased', [MemberController::class, 'deceased']);

    Route::get('/memberpermonth/{date}', [MemberController::class, 'date']);

    Route::get('/allmembers', [MemberController::class, 'all']);
    Route::get('/findmember', [MemberController::class, 'find']);
    Route::get('/findMember/{national_id}', [MemberController::class, 'find_id']);


    
    Route::put('/toggle-deceased-status/{id}', [MemberController::class, 'toggleDeceasedStatus']);

        Route::get('/predelete_member/{id}', [MemberController::class, 'predelete']);
    Route::post('/delete_member', [MemberController::class, 'delete']);

    

    //Members

    Route::get('/notify-member/{id}', [MemberManagementController::class, 'notify_member']);


        // Group Routes
    Route::get('/group', [GroupController::class, 'index']);
    Route::get('/allgroups', [GroupController::class, 'all']);
    Route::post('/group', [GroupController::class, 'store']);
    Route::get('/group/{id}', [GroupController::class, 'view']);
    Route::put('/group/{id}', [GroupController::class, 'update']);
    Route::get('/delete_group/{id}', [GroupController::class, 'delete']);
    Route::get('/groups/{group}/members', [GroupController::class, 'members']);

    /*
    |--------------------------------------------------------------------------
    | KYC
    |--------------------------------------------------------------------------
    */
    Route::prefix('kyc')->group(function () {
        Route::get('/{memberId}', [KycDocumentController::class, 'index'])->name('kyc.index');
        Route::get('/{memberId}/upload', [KycDocumentController::class, 'create'])->name('kyc.create');
        Route::post('/{memberId}/store', [KycDocumentController::class, 'store'])->name('kyc.store');
        Route::get('/view/{id}', [KycDocumentController::class, 'show'])->name('kyc.show');
        Route::post('/update-status/{id}', [KycDocumentController::class, 'updateStatus'])->name('kyc.updateStatus');
        Route::delete('/delete/{id}', [KycDocumentController::class, 'destroy'])->name('kyc.destroy');
    });

    /*
    |--------------------------------------------------------------------------
    | Payments
    |--------------------------------------------------------------------------
    */

    //Process Payment
    //Route::post('/payment', [PaymentController::class, 'store']);

    Route::post('/payment', [PaymentProcessingController::class, 'paymentStore']);





    Route::get('/new_payment/{id}', [PaymentController::class, 'index']);
    Route::get('/payment/{id}', [PaymentController::class, 'view']);
    Route::put('/payment/{id}', [PaymentController::class, 'update']);
    Route::get('/delete_payment/{id}/{member_id}', [PaymentController::class, 'delete']);
    Route::get('/receipt/{id}', [PaymentController::class, 'receipt']);
    Route::get('/payment_preview/{id}', [PaymentController::class, 'preview']);

    Route::get('/allpayments', [PaymentController::class, 'all']);
    Route::get('/deletedpayments', [PaymentController::class, 'alldeleted']);
    Route::get('/wrongpayment', [PaymentController::class, 'wrong']);

    Route::get('/memberpayments/{id}', [PaymentController::class, 'all1']);

    //SMS
    Route::get('/sms', [SmsController::class, 'index']);
    Route::get('/sms/{id}', [SmsController::class, 'view']);
    Route::post('/sms', [SmsController::class, 'search']);
    Route::post('/search_sms', [SmsController::class, 'searchSms']);
    Route::get('/admin/resend-failed-sms', [SendSmsController::class, 'resendFailedSms'])
        ->middleware('auth')
        ->name('admin.resend.failed.sms');

    /*
    |--------------------------------------------------------------------------
    | Reports
    |--------------------------------------------------------------------------
    */
    Route::get('/paymentsreport', [ReportController::class, 'payment']);
    Route::get('/coversreport', [ReportController::class, 'cover']);
    Route::get('/arrearsreport', [ReportController::class, 'arrear']);
    Route::get('/statement/{id}', [ReportController::class, 'statement']);

    Route::get('/user-payments-report', [PaymentReportController::class, 'showUserPaymentsForm']);
    Route::post('/user-payments-report', [PaymentReportController::class, 'generateUserPaymentsReport']);
    Route::get('/admin/payment-stats', [PaymentReportController::class, 'paymentStats'])
        ->middleware('auth')
        ->name('admin.payment.stats');

    /*
    |--------------------------------------------------------------------------
    | Users
    |--------------------------------------------------------------------------
    */
    Route::get('/user', [UserController::class, 'index']);
    Route::get('/allusers', [UserController::class, 'all']);
    Route::post('/user', [UserController::class, 'store']);
    Route::get('/user/{id}', [UserController::class, 'view']);
    Route::put('/user/{id}', [UserController::class, 'update']);
    Route::get('/user_profile/{id}', [UserController::class, 'profile']);
    Route::get('/user/status/{id}/{status}', [UserController::class, 'changeStatus']);

    Route::get('/new_dependant/{id}', [DependantController::class, 'index']);
    Route::get('/alldependants/{id}', [DependantController::class, 'all']);
    Route::post('/dependant', [DependantController::class, 'store']);
    Route::get('/dependant/{id}', [DependantController::class, 'view']);
    Route::put('/dependant/{id}', [DependantController::class, 'update']);
    Route::delete('/delete_dependant/{id}', [DependantController::class, 'delete']);

    //Beneficiary
    Route::get('/new_beneficiary/{id}', 'App\Http\Controllers\BeneficiaryController@index');
    Route::get('/allbeneficiaries/{id}', 'App\Http\Controllers\BeneficiaryController@all');
    Route::post('/beneficiary', 'App\Http\Controllers\BeneficiaryController@store');
    Route::get('/beneficiary/{id}', 'App\Http\Controllers\BeneficiaryController@view');
    Route::put('/beneficiary/{id}', 'App\Http\Controllers\BeneficiaryController@update');
    Route::get('/delete_beneficiary/{id}', 'App\Http\Controllers\BeneficiaryController@delete');


    Route::get('/logs', [UserLogController::class, 'index'])->middleware('auth');

    Route::get('/agentprofile/{id}', [UserController::class, 'profile'])
        ->name('agent.profile');
    Route::post('/agent_monthcovered', [UserController::class, 'monthcovered'])
        ->name('agent.monthcovered');
});

/*
|--------------------------------------------------------------------------
| Utilities
|--------------------------------------------------------------------------
*/
Route::get('/clear', function () {
    Artisan::call('optimize:clear');
    return 'System cache cleared';
});

/*
|--------------------------------------------------------------------------
| OTP
|--------------------------------------------------------------------------
*/
Route::get('/otp-verify', [OtpController::class, 'showVerifyForm'])->name('otp.verify');
Route::post('/otp-verify', [OtpController::class, 'verifyOtp'])->name('otp.verify.post');

/*
|--------------------------------------------------------------------------
| Auth
|--------------------------------------------------------------------------
*/
require __DIR__ . '/auth.php';
