<?php

use Illuminate\Support\Facades\Route;

// Controllers
use App\Http\Controllers\LandingController;
use App\Http\Controllers\SubscriptionController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\Auth\RedirectDashboardController;

// Admin Controllers
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\PlanController as AdminPlanController;
use App\Http\Controllers\Admin\PaymentReviewController as AdminPaymentReviewController;
use App\Http\Controllers\Admin\CmsController as AdminCmsController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Admin\LeasingController as AdminLeasingController;
use App\Http\Controllers\Admin\MemberController as AdminMemberController;
use App\Http\Controllers\Admin\RecordController as AdminRecordController;
use App\Http\Controllers\Admin\AuditLogController as AdminAuditLogController;

// Leasing Controllers
use App\Http\Controllers\Leasing\DashboardController as LeasingDashboardController;
use App\Http\Controllers\Leasing\ProfileController as LeasingProfileController;
use App\Http\Controllers\Leasing\BranchController as LeasingBranchController;
use App\Http\Controllers\Leasing\UploadController as LeasingUploadController;
use App\Http\Controllers\Leasing\RecordController as LeasingRecordController;
use App\Http\Controllers\Leasing\SubscriptionController as LeasingSubscriptionController;
use App\Http\Controllers\Leasing\PaymentController as LeasingPaymentController;

// Member Controllers
use App\Http\Controllers\Member\DashboardController as MemberDashboardController;
use App\Http\Controllers\Member\SearchController as MemberSearchController;
use App\Http\Controllers\Member\SubscriptionController as MemberSubscriptionController;
use App\Http\Controllers\Member\PaymentController as MemberPaymentController;

// Public
Route::get('/', [LandingController::class, 'index'])->name('landing');


Route::middleware('auth')->group(function () {
    
    // Shared Dashboard Redirector
    Route::get('/dashboard', RedirectDashboardController::class)->name('dashboard');

    // Profile (Accessible for everyone)
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    // Notifications
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/{id}/mark-as-read', [NotificationController::class, 'markAsRead'])->name('notifications.mark-as-read');
    Route::post('/notifications/mark-all-as-read', [NotificationController::class, 'markAllAsRead'])->name('notifications.mark-all-as-read');

    // 1. ADMIN ROUTES
    Route::middleware(['role:admin'])->prefix('admin')->name('admin.')->group(function () {
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
        
        Route::get('users', [AdminUserController::class, 'index'])->name('users.index');
        Route::get('users/{user}', [AdminUserController::class, 'show'])->name('users.show');
        Route::post('users/{user}/toggle-status', [AdminUserController::class, 'toggleStatus'])->name('users.toggle-status');

        Route::get('leasing', [AdminLeasingController::class, 'index'])->name('leasing.index');
        Route::get('member', [AdminMemberController::class, 'index'])->name('member.index');
        
        Route::get('records', [AdminRecordController::class, 'index'])->name('records.index');
        Route::get('audit-logs', [AdminAuditLogController::class, 'index'])->name('audit-logs.index');

        Route::resource('plans', AdminPlanController::class);
        Route::get('payment-reviews', [AdminPaymentReviewController::class, 'index'])->name('payment-reviews.index');
        Route::get('payment-reviews/{payment}/proof', [AdminPaymentReviewController::class, 'showProof'])->name('payment-reviews.proof');
        Route::post('payment-reviews/{payment}/approve', [AdminPaymentReviewController::class, 'approve'])->name('payment-reviews.approve');
        Route::post('payment-reviews/{payment}/reject', [AdminPaymentReviewController::class, 'reject'])->name('payment-reviews.reject');
        
        Route::get('cms', [AdminCmsController::class, 'index'])->name('cms.index');
        Route::post('cms', [AdminCmsController::class, 'update'])->name('cms.update');
    });

    // 2. ACTIVATION & ONBOARDING (Common for Leasing & Member)
    Route::middleware(['activation.access'])->group(function () {
        Route::get('/subscription/required', [SubscriptionController::class, 'required'])->name('payment.required');
        
        // Leasing Activation & Payment
        Route::get('/leasing/plans', [LeasingSubscriptionController::class, 'plans'])->name('leasing.plans.index');
        Route::post('/leasing/plans/{plan}', [LeasingSubscriptionController::class, 'select'])->name('leasing.plans.select');
        Route::get('/leasing/payment', [LeasingPaymentController::class, 'show'])->name('leasing.payment.show');
        Route::post('/leasing/payment', [LeasingPaymentController::class, 'store'])->name('leasing.payment.store');

        // Member Activation & Payment
        Route::get('/member/plans', [MemberSubscriptionController::class, 'plans'])->name('member.plans.index');
        Route::post('/member/plans/{plan}', [MemberSubscriptionController::class, 'select'])->name('member.plans.select');
        Route::get('/member/payment', [MemberPaymentController::class, 'show'])->name('member.payment.show');
        Route::post('/member/payment', [MemberPaymentController::class, 'store'])->name('member.payment.store');

        // Legacy/Generic (Can remove if fully migrated)
        Route::get('/subscription/checkout/{plan}', [SubscriptionController::class, 'checkout'])->name('payment.checkout');
        Route::post('/subscription/store', [SubscriptionController::class, 'store'])->name('payment.store');
    });

    // 3. LEASING ACTIVE ROUTES
    Route::middleware(['role:leasing', 'subscription.active', 'leasing.profile.exists'])->prefix('leasing')->name('leasing.')->group(function () {
        Route::get('/dashboard', [LeasingDashboardController::class, 'index'])->name('dashboard');
        
        Route::get('/profile', [LeasingProfileController::class, 'edit'])->name('profile.edit');
        Route::patch('/profile', [LeasingProfileController::class, 'update'])->name('profile.update');

        Route::resource('branches', LeasingBranchController::class);
        
        Route::get('uploads', [LeasingUploadController::class, 'index'])->name('uploads.index');
        Route::get('uploads/create', [LeasingUploadController::class, 'create'])->name('uploads.create');
        Route::post('uploads', [LeasingUploadController::class, 'store'])->name('uploads.store');
        Route::get('uploads/template', [LeasingUploadController::class, 'downloadTemplate'])->name('uploads.template');
        Route::get('uploads/{upload}', [LeasingUploadController::class, 'show'])->name('uploads.show');

        Route::resource('records', LeasingRecordController::class);
    });

    // 4. MEMBER ACTIVE ROUTES
    Route::middleware(['role:member', 'subscription.active'])->prefix('member')->name('member.')->group(function () {
        Route::get('/dashboard', [MemberDashboardController::class, 'index'])->name('dashboard');
        Route::get('/search', [MemberSearchController::class, 'index'])->name('search.index');
        Route::get('/search/{id}', [MemberSearchController::class, 'show'])->name('search.show');
    });

});

require __DIR__.'/auth.php';
