<?php

use App\Http\Controllers\Web\HomeController;
use Illuminate\Http\Request;
use App\Http\Controllers\Web\Auth\LoginController;
use App\Http\Controllers\Web\Auth\RegisterController;
use App\Http\Controllers\Web\Auth\ForgotPasswordController;
use App\Http\Controllers\Web\Auth\ResetPasswordController;
use App\Http\Controllers\Web\DashboardController;
use App\Http\Controllers\Web\WalletController;
use App\Http\Controllers\Web\AirtimeController;
use App\Http\Controllers\Web\DataController;
use App\Http\Controllers\Web\CableController;
use App\Http\Controllers\Web\ElectricityController;
use App\Http\Controllers\Web\ExamPinController;
use App\Http\Controllers\Web\TransactionController;
use App\Http\Controllers\Web\BeneficiaryController;
use App\Http\Controllers\Web\NotificationController;
use App\Http\Controllers\Web\ProfileController;
use App\Http\Controllers\Web\Admin\AdminDashboardController;
use App\Http\Controllers\Web\Admin\AdminUserController;
use App\Http\Controllers\Web\Admin\AdminTransactionController;
use App\Http\Controllers\Web\Admin\AdminServiceController;
use App\Http\Controllers\Web\Admin\AdminSettingController;
use Illuminate\Support\Facades\Route;

// Public routes
Route::get('/', [HomeController::class, 'index'])->name('home');

Route::get('about', fn() => view('pages.about'))->name('about');
Route::get('contact', fn() => view('pages.contact'))->name('contact');
Route::get('terms', fn() => view('pages.terms'))->name('terms');
Route::get('privacy', fn() => view('pages.privacy'))->name('privacy');

Route::get('verify-email/{id}/{hash}', function (Request $request, string $id, string $hash) {
    $user = \App\Models\User::find($id);
    if (!$user || sha1($user->email) !== $hash || !$request->hasValidSignature()) {
        abort(403, 'Invalid or expired verification link.');
    }
    $user->markEmailAsVerified();
    return redirect()->route('dashboard')->with('success', 'Your email has been verified!');
})->middleware(['signed'])->name('verification.verify');

Route::post('verify-email/resend', function (Request $request) {
    $request->user()->sendEmailVerificationNotification();
    return back()->with('success', 'Verification email sent!');
})->middleware(['auth'])->name('verification.resend');

Route::middleware('guest')->group(function () {
    Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('login', [LoginController::class, 'login'])->middleware('throttle:auth');
    Route::get('register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('register', [RegisterController::class, 'register'])->middleware('throttle:auth');
    Route::get('forgot-password', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
    Route::post('forgot-password', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');
    Route::get('reset-password/{token}', [ResetPasswordController::class, 'showResetForm'])->name('password.reset');
    Route::post('reset-password', [ResetPasswordController::class, 'reset'])->name('password.update');
});

// Authenticated routes
Route::middleware('auth')->group(function () {
    Route::post('logout', [LoginController::class, 'logout'])->name('logout');

    Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Wallet
    Route::prefix('wallet')->name('wallet.')->group(function () {
        Route::get('/', [WalletController::class, 'index'])->name('index');
        Route::post('fund', [WalletController::class, 'fund'])->name('fund')->middleware('throttle:purchase');
        Route::get('paystack/callback', [WalletController::class, 'paystackCallback'])->name('paystack.callback');
        Route::get('transactions', [WalletController::class, 'transactions'])->name('transactions');
    });

    // Airtime
    Route::prefix('airtime')->name('airtime.')->group(function () {
        Route::get('/', [AirtimeController::class, 'index'])->name('index');
        Route::post('purchase', [AirtimeController::class, 'purchase'])->name('purchase')->middleware(['throttle:purchase', 'idempotent']);
    });

    // Data
    Route::prefix('data')->name('data.')->group(function () {
        Route::get('/', [DataController::class, 'index'])->name('index');
        Route::get('plans/{network}', [DataController::class, 'plans'])->name('plans');
        Route::post('purchase', [DataController::class, 'purchase'])->name('purchase')->middleware(['throttle:purchase', 'idempotent']);
    });

    // Cable
    Route::prefix('cable')->name('cable.')->group(function () {
        Route::get('/', [CableController::class, 'index'])->name('index');
        Route::get('bouquets/{provider}', [CableController::class, 'bouquets'])->name('bouquets');
        Route::post('validate', [CableController::class, 'validateSmartcard'])->name('validate');
        Route::post('purchase', [CableController::class, 'purchase'])->name('purchase')->middleware(['throttle:purchase', 'idempotent']);
    });

    // Electricity
    Route::prefix('electricity')->name('electricity.')->group(function () {
        Route::get('/', [ElectricityController::class, 'index'])->name('index');
        Route::get('plans/{provider}', [ElectricityController::class, 'plans'])->name('plans');
        Route::post('validate', [ElectricityController::class, 'validateMeter'])->name('validate');
        Route::post('purchase', [ElectricityController::class, 'purchase'])->name('purchase')->middleware(['throttle:purchase', 'idempotent']);
    });

    // Exam PINs
    Route::prefix('exam-pins')->name('exampin.')->group(function () {
        Route::get('/', [ExamPinController::class, 'index'])->name('index');
        Route::get('variations/{type}', [ExamPinController::class, 'variations'])->name('variations');
        Route::post('validate-profile', [ExamPinController::class, 'validateProfile'])->name('validate-profile');
        Route::post('purchase', [ExamPinController::class, 'purchase'])->name('purchase')->middleware(['throttle:purchase', 'idempotent']);
    });

    // Transactions
    Route::prefix('transactions')->name('transactions.')->group(function () {
        Route::get('/', [TransactionController::class, 'index'])->name('index');
        Route::get('{reference}', [TransactionController::class, 'show'])->name('show');
        Route::get('{reference}/receipt', [TransactionController::class, 'receipt'])->name('receipt');
        Route::get('{reference}/receipt/pdf', [TransactionController::class, 'downloadReceipt'])->name('receipt.pdf');
    });

    // Beneficiaries
    Route::resource('beneficiaries', BeneficiaryController::class);

    // Notifications
    Route::prefix('notifications')->name('notifications.')->group(function () {
        Route::get('/', [NotificationController::class, 'index'])->name('index');
        Route::put('{notification}/read', [NotificationController::class, 'markAsRead'])->name('read');
        Route::put('read-all', [NotificationController::class, 'markAllAsRead'])->name('read-all');
    });

    // Profile
    Route::prefix('profile')->name('profile.')->group(function () {
        Route::get('/', [ProfileController::class, 'index'])->name('index');
        Route::put('/', [ProfileController::class, 'update'])->name('update');
        Route::put('password', [ProfileController::class, 'updatePassword'])->name('password');
    });

    // Admin routes
    Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () {
        Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');

        Route::resource('users', AdminUserController::class)->only(['index', 'show', 'edit', 'update']);
        Route::post('users/{user}/suspend', [AdminUserController::class, 'suspend'])->name('users.suspend');
        Route::post('users/{user}/activate', [AdminUserController::class, 'activate'])->name('users.activate');

        Route::get('transactions', [AdminTransactionController::class, 'index'])->name('transactions.index');
        Route::get('transactions/{transaction}', [AdminTransactionController::class, 'show'])->name('transactions.show');

        Route::get('services', [AdminServiceController::class, 'index'])->name('services.index');
        Route::post('services', [AdminServiceController::class, 'store'])->name('services.store');
        Route::put('services/{service}', [AdminServiceController::class, 'update'])->name('services.update');
        Route::post('services/sync', [AdminServiceController::class, 'sync'])->name('services.sync');

        Route::get('settings', [AdminSettingController::class, 'index'])->name('settings.index');
        Route::put('settings', [AdminSettingController::class, 'update'])->name('settings.update');
    });
});
