<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Membership\Public\MembershipApplicationController;
use App\Http\Controllers\Membership\Public\MembershipCategoryController as PublicMembershipCategoryController;
use App\Http\Controllers\Membership\Public\LoanProductController as PublicLoanProductController;
use App\Http\Controllers\Membership\Admin\MembershipDashboardController;
use App\Http\Controllers\Membership\Admin\MembershipCategoryController as AdminMembershipCategoryController;
use App\Http\Controllers\Membership\Admin\ContributionController;
use App\Http\Controllers\Membership\Admin\LoanProductController;
use App\Http\Controllers\Membership\Admin\LoanManagementController;
use App\Http\Controllers\Membership\Admin\MembershipRenewalConfigController;
use App\Http\Controllers\Membership\Member\MemberPortalController;
use App\Http\Controllers\Membership\Member\LoanApplicationController as MemberLoanApplicationController;
use App\Http\Controllers\Membership\Member\MemberCertificateController;
use App\Http\Controllers\Membership\Member\MemberRenewalController;

/*
|--------------------------------------------------------------------------
| Public Routes (No Authentication Required)
|--------------------------------------------------------------------------
*/

// Membership Categories (Public)
Route::prefix('membership')->name('membership.')->group(function () {
    Route::get('/categories', [PublicMembershipCategoryController::class, 'index'])->name('categories.index');
    Route::get('/categories/{membershipCategory}', [PublicMembershipCategoryController::class, 'show'])->name('categories.show');
    
    // Membership Application (Public)
    Route::get('/apply', [MembershipApplicationController::class, 'create'])->name('apply');
    Route::post('/apply', [MembershipApplicationController::class, 'store'])->name('apply.store');
    Route::get('/apply/success/{referenceNumber}', [MembershipApplicationController::class, 'success'])->name('apply.success');
    Route::get('/status/{referenceNumber}', [MembershipApplicationController::class, 'status'])->name('status');
    Route::get('/check-status', [MembershipApplicationController::class, 'checkStatusForm'])->name('check-status');
    Route::post('/check-status', [MembershipApplicationController::class, 'checkStatus'])->name('check-status.submit');
});

// Loan Products (Public)
Route::prefix('loans')->name('loans.')->group(function () {
    Route::get('/', [PublicLoanProductController::class, 'index'])->name('index');
    Route::get('/{loanProduct}', [PublicLoanProductController::class, 'show'])->name('show');
    Route::post('/{loanProduct}/calculate', [PublicLoanProductController::class, 'calculate'])->name('calculate');
});

/*
|--------------------------------------------------------------------------
| Admin Routes (Requires Authentication + Permission)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'verified'])->prefix('admin')->name('admin.')->group(function () {
    
    // Membership Dashboard
    Route::get('/membership', [MembershipDashboardController::class, 'index'])->name('membership.dashboard');
    
    // Membership Applications
    Route::prefix('members/applications')->name('members.applications.')->group(function () {
        Route::get('/', [MembershipDashboardController::class, 'pendingApplications'])->name('index');
        Route::get('/{application}', [MembershipDashboardController::class, 'showApplication'])->name('show');
        Route::post('/{application}/approve', [MembershipDashboardController::class, 'approveApplication'])->name('approve');
        Route::post('/{application}/reject', [MembershipDashboardController::class, 'rejectApplication'])->name('reject');
    });
    
    // Members Management
    Route::prefix('members')->name('members.')->group(function () {
        Route::get('/', [MembershipDashboardController::class, 'members'])->name('index');
        Route::get('/create', [MembershipDashboardController::class, 'createMember'])->name('create');
        Route::post('/', [MembershipDashboardController::class, 'storeMember'])->name('store');
        Route::get('/{member}', [MembershipDashboardController::class, 'showMember'])->name('show');
        Route::post('/{member}/suspend', [MembershipDashboardController::class, 'suspendMember'])->name('suspend');
        Route::post('/{member}/reactivate', [MembershipDashboardController::class, 'reactivateMember'])->name('reactivate');
        Route::post('/{member}/issue-certificate', [MembershipDashboardController::class, 'issueCertificate'])->name('issue-certificate');
        Route::post('/{member}/process-renewal', [MembershipDashboardController::class, 'processRenewal'])->name('process-renewal');
        Route::post('/{member}/collect-renewal-payment', [MembershipDashboardController::class, 'collectRenewalPayment'])->name('collect-renewal-payment');
    });
    
    // Membership Categories Management
    Route::prefix('membership-categories')->name('membership-categories.')->group(function () {
        Route::get('/', [AdminMembershipCategoryController::class, 'index'])->name('index');
        Route::get('/create', [AdminMembershipCategoryController::class, 'create'])->name('create');
        Route::post('/', [AdminMembershipCategoryController::class, 'store'])->name('store');
        Route::get('/{membershipCategory}', [AdminMembershipCategoryController::class, 'show'])->name('show');
        Route::get('/{membershipCategory}/edit', [AdminMembershipCategoryController::class, 'edit'])->name('edit');
        Route::put('/{membershipCategory}', [AdminMembershipCategoryController::class, 'update'])->name('update');
        Route::delete('/{membershipCategory}', [AdminMembershipCategoryController::class, 'destroy'])->name('destroy');
        
        // Subscription Fees
        Route::get('/{membershipCategory}/subscription-fees', [AdminMembershipCategoryController::class, 'subscriptionFees'])->name('subscription-fees');
        Route::post('/{membershipCategory}/subscription-fees', [AdminMembershipCategoryController::class, 'storeSubscriptionFee'])->name('subscription-fees.store');
        
        // Contribution Fees
        Route::get('/{membershipCategory}/contribution-fees', [AdminMembershipCategoryController::class, 'contributionFees'])->name('contribution-fees');
        Route::post('/{membershipCategory}/contribution-fees', [AdminMembershipCategoryController::class, 'storeContributionFee'])->name('contribution-fees.store');
        Route::post('/{membershipCategory}/penalties', [AdminMembershipCategoryController::class, 'storePenalty'])->name('penalties.store');
    });
    
    // Contributions Management
    Route::prefix('contributions')->name('contributions.')->group(function () {
        Route::get('/', [ContributionController::class, 'index'])->name('index');
        Route::get('/create', [ContributionController::class, 'create'])->name('create');
        Route::post('/', [ContributionController::class, 'store'])->name('store');
        Route::get('/{contribution}', [ContributionController::class, 'show'])->name('show');
        Route::post('/generate-bulk', [ContributionController::class, 'generateBulk'])->name('generate-bulk');
        Route::post('/apply-penalties', [ContributionController::class, 'applyPenalties'])->name('apply-penalties');
        Route::get('/export', [ContributionController::class, 'export'])->name('export');
    });
    
    // Loan Products Management
    Route::prefix('loan-products')->name('loan-products.')->group(function () {
        Route::get('/', [LoanProductController::class, 'index'])->name('index');
        Route::get('/create', [LoanProductController::class, 'create'])->name('create');
        Route::post('/', [LoanProductController::class, 'store'])->name('store');
        Route::get('/{loanProduct}', [LoanProductController::class, 'show'])->name('show');
        Route::get('/{loanProduct}/edit', [LoanProductController::class, 'edit'])->name('edit');
        Route::put('/{loanProduct}', [LoanProductController::class, 'update'])->name('update');
        Route::delete('/{loanProduct}', [LoanProductController::class, 'destroy'])->name('destroy');
    });
    
    // Loan Applications Management
    Route::prefix('loan-applications')->name('loan-applications.')->group(function () {
        Route::get('/', [LoanManagementController::class, 'index'])->name('index');
        Route::get('/{loanApplication}', [LoanManagementController::class, 'show'])->name('show');
        Route::post('/{loanApplication}/manager-review', [LoanManagementController::class, 'managerReview'])->name('manager-review');
        Route::post('/{loanApplication}/super-manager-approve', [LoanManagementController::class, 'superManagerApprove'])->name('super-manager-approve');
        Route::post('/{loanApplication}/disburse', [LoanManagementController::class, 'disburse'])->name('disburse');
        Route::post('/documents/{document}/verify', [LoanManagementController::class, 'verifyDocument'])->name('documents.verify');
    });
    
    // Active Loans
    Route::get('/loans/active', [LoanManagementController::class, 'activeLoans'])->name('loans.active');
    Route::get('/loans/overdue', [LoanManagementController::class, 'overdueLoans'])->name('loans.overdue');

    // Renewal Period Configurations
    Route::prefix('renewal-configs')->name('renewal-configs.')->group(function () {
        Route::get('/', [MembershipRenewalConfigController::class, 'index'])->name('index');
        Route::post('/', [MembershipRenewalConfigController::class, 'store'])->name('store');
        Route::put('/{config}', [MembershipRenewalConfigController::class, 'update'])->name('update');
        Route::delete('/{config}', [MembershipRenewalConfigController::class, 'destroy'])->name('destroy');
    });
});

/*
|--------------------------------------------------------------------------
| Member Portal Routes (Requires Authentication + Member Role)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'verified'])->prefix('member')->name('member.')->group(function () {
    
    // Dashboard
    Route::get('/dashboard', [MemberPortalController::class, 'dashboard'])->name('dashboard');
    
    // Profile
    Route::get('/profile', [MemberPortalController::class, 'profile'])->name('profile');
    Route::put('/profile', [MemberPortalController::class, 'updateProfile'])->name('profile.update');
    
    // Contributions
    Route::get('/contributions', [MemberPortalController::class, 'contributions'])->name('contributions');
    Route::get('/contributions/{contribution}/pay', [MemberPortalController::class, 'payContribution'])->name('contributions.pay');
    Route::post('/contributions/{contribution}/process-payment', [MemberPortalController::class, 'processContributionPayment'])->name('contributions.process-payment');
    
    // Loans
    Route::prefix('loans')->name('loans.')->group(function () {
        Route::get('/products', [MemberLoanApplicationController::class, 'products'])->name('products');
        Route::get('/apply/{loanProduct}', [MemberLoanApplicationController::class, 'create'])->name('apply');
        Route::post('/apply/{loanProduct}', [MemberLoanApplicationController::class, 'store'])->name('store');
        Route::get('/', [MemberLoanApplicationController::class, 'index'])->name('index');
        Route::get('/{loanApplication}', [MemberLoanApplicationController::class, 'show'])->name('show');
        Route::get('/{loanApplication}/schedule', [MemberLoanApplicationController::class, 'repaymentSchedule'])->name('schedule');
        Route::get('/{loanApplication}/repay', [MemberLoanApplicationController::class, 'makeRepayment'])->name('repay');
        Route::post('/{loanApplication}/process-repayment', [MemberLoanApplicationController::class, 'processRepayment'])->name('process-repayment');
        Route::get('/{loanApplication}/payment-status/{transaction}', [MemberLoanApplicationController::class, 'paymentStatus'])->name('payment-status');
        Route::get('/{loanApplication}/payment-status-check/{transaction}', [MemberLoanApplicationController::class, 'paymentStatusCheck'])->name('payment-status-check');
    });

    // Certificate
    Route::prefix('certificate')->name('certificate.')->group(function () {
        Route::get('/', [MemberCertificateController::class, 'index'])->name('index');
        Route::get('/{certificate}/download', [MemberCertificateController::class, 'download'])->name('download');
        Route::post('/regenerate', [MemberCertificateController::class, 'regenerate'])->name('regenerate');
    });

    // Renewal
    Route::prefix('renewal')->name('renewal.')->group(function () {
        Route::get('/settings', [MemberRenewalController::class, 'settings'])->name('settings');
        Route::put('/settings', [MemberRenewalController::class, 'updateSettings'])->name('settings.update');
        Route::get('/history', [MemberRenewalController::class, 'index'])->name('index');
        Route::post('/', [MemberRenewalController::class, 'store'])->name('store');
    });
});
