<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\WebhookController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

// Webhook Routes (no auth required)
Route::post('/webhooks/stripe', [WebhookController::class, 'stripe'])->name('webhooks.stripe');
Route::post('/webhooks/paypal', [WebhookController::class, 'paypal'])->name('webhooks.paypal');

Route::middleware('auth')->group(function () {
    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');

    // Admin Routes
    Route::middleware(['role:super_admin,admin'])->prefix('admin')->name('admin.')->group(function () {
        Route::resource('classes', \App\Http\Controllers\Admin\CbtClassController::class);
        Route::resource('question-sets', \App\Http\Controllers\Admin\QuestionSetController::class);
        
        // Question Routes (Extended)
        Route::get('questions/bulk', [\App\Http\Controllers\Admin\QuestionController::class, 'bulkCreate'])->name('questions.bulk');
        Route::post('questions/bulk', [\App\Http\Controllers\Admin\QuestionController::class, 'bulkStore'])->name('questions.bulk.store');
        Route::get('questions/import', [\App\Http\Controllers\Admin\QuestionController::class, 'import'])->name('questions.import');
        Route::post('questions/import', [\App\Http\Controllers\Admin\QuestionController::class, 'processImport'])->name('questions.import.process');
        Route::resource('questions', \App\Http\Controllers\Admin\QuestionController::class);
        
        // Settings Routes
        Route::get('settings', [\App\Http\Controllers\Admin\SettingController::class, 'index'])->name('settings.index');
        Route::patch('settings', [\App\Http\Controllers\Admin\SettingController::class, 'update'])->name('settings.update');
        Route::get('settings/payments', [\App\Http\Controllers\Admin\SettingController::class, 'payments'])->name('settings.payments');
        Route::patch('settings/payments', [\App\Http\Controllers\Admin\SettingController::class, 'updatePayments'])->name('settings.payments.update');

        // Monetization Routes
        Route::resource('plans', \App\Http\Controllers\Admin\PlanController::class)->only(['index', 'edit', 'update']);
        Route::get('payments', [\App\Http\Controllers\Admin\PaymentController::class, 'index'])->name('payments.index');
    });

    // Student Routes
    Route::middleware(['role:user'])->group(function () {
        Route::get('/classes', [\App\Http\Controllers\Student\CbtClassController::class, 'index'])->name('classes.index');
        Route::get('/classes/{cbtClass}', [\App\Http\Controllers\Student\CbtClassController::class, 'show'])->name('classes.show');
        
        // Test Taking Routes
        Route::post('/test/start/{questionSet}', [\App\Http\Controllers\Student\TestSessionController::class, 'start'])->name('test.start');
        Route::get('/test/{session}', [\App\Http\Controllers\Student\TestSessionController::class, 'show'])->name('test.show');
        Route::post('/test/{session}/answer', [\App\Http\Controllers\Student\TestSessionController::class, 'submitAnswer'])->name('test.submit');
        
        // Results Route
        Route::get('/results', [\App\Http\Controllers\Student\TestSessionController::class, 'results'])->name('results.index');

        // Subscription Routes
        Route::get('/subscriptions', [\App\Http\Controllers\Student\SubscriptionController::class, 'index'])->name('subscriptions.index');
        Route::get('/subscriptions/select/{plan}', [\App\Http\Controllers\Student\SubscriptionController::class, 'selectPlan'])->name('subscriptions.select');
        
        // Payment Checkout Routes
        Route::post('/subscriptions/checkout/stripe/{plan}', [\App\Http\Controllers\Student\PaymentController::class, 'stripeCheckout'])->name('checkout.stripe');
        Route::post('/subscriptions/checkout/paypal/{plan}', [\App\Http\Controllers\Student\PaymentController::class, 'paypalCheckout'])->name('checkout.paypal');
        
        // Payment Callback Routes
        Route::get('/payment/stripe/success/{plan}', [\App\Http\Controllers\Student\PaymentController::class, 'stripeSuccess'])->name('payment.stripe.success');
        Route::get('/payment/stripe/cancel', [\App\Http\Controllers\Student\PaymentController::class, 'stripeCancel'])->name('payment.stripe.cancel');
        Route::get('/payment/paypal/success/{plan}', [\App\Http\Controllers\Student\PaymentController::class, 'paypalSuccess'])->name('payment.paypal.success');
        Route::get('/payment/paypal/cancel', [\App\Http\Controllers\Student\PaymentController::class, 'paypalCancel'])->name('payment.paypal.cancel');
    });
});

require __DIR__.'/auth.php';
