<?php

use App\Http\Controllers\Admin\AdminAuthController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\MemberController;
use App\Http\Controllers\Admin\NotificationSettingsController;
use App\Http\Controllers\Admin\PointsController;
use App\Http\Controllers\Admin\TierController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Member\ProfileController;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

// ─── Member Auth ──────────────────────────────────────────────────────────────

Route::get('/', function () {
    if (auth()->check()) {
        return redirect()->route('member.dashboard');
    }
    return redirect()->route('login');
});

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLogin'])->name('login');
    Route::post('/login', [LoginController::class, 'login'])->middleware('throttle:5,1');
    Route::post('/register', [RegisterController::class, 'register'])->name('register')->middleware('throttle:10,1');
    Route::get('/forgot-password', fn () => view('auth.forgot-password'))->name('password.request');
    Route::post('/forgot-password', function (Request $request) {
        $request->validate(['email' => 'required|email']);
        $status = \Illuminate\Support\Facades\Password::sendResetLink($request->only('email'));
        return $status === \Illuminate\Support\Facades\Password::RESET_LINK_SENT
            ? back()->with('status', 'Reset link sent — check your email.')
            : back()->withErrors(['email' => __($status)]);
    })->name('password.email');
});

Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// Email verification
Route::get('/email/verify', fn () => view('auth.verify-email'))->middleware('auth')->name('verification.notice');

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect()->route('member.dashboard')->with('success', 'Email verified successfully!');
})->middleware(['auth', 'signed'])->name('verification.verify');

Route::post('/email/verification-notification', function (Request $request) {
    $request->user()->sendEmailVerificationNotification();
    return back()->with('status', 'Verification link sent!');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');

// ─── Member Portal ────────────────────────────────────────────────────────────

Route::prefix('member')->name('member.')->middleware(['member', 'verified'])->group(function () {
    Route::get('/dashboard', fn () => view('member.dashboard'))->name('dashboard');
    Route::get('/points', fn () => view('member.points'))->name('points');
    Route::get('/benefits', fn () => view('member.benefits'))->name('benefits');
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
});

// ─── Admin Auth ───────────────────────────────────────────────────────────────

Route::prefix('admin')->name('admin.')->group(function () {

    Route::middleware('guest:admin')->group(function () {
        Route::get('/login', [AdminAuthController::class, 'showLogin'])->name('login');
        Route::post('/login', [AdminAuthController::class, 'login'])->name('login.submit')->middleware('throttle:5,1');
    });

    Route::post('/logout', [AdminAuthController::class, 'logout'])->name('logout');

    // ─── Admin Panel ──────────────────────────────────────────────────────────
    Route::middleware('admin')->group(function () {

        // Dashboard
        Route::get('/dashboard', fn () => view('admin.dashboard'))->name('dashboard');

        // Members
        Route::get('/members', [MemberController::class, 'index'])->name('members.index');
        Route::get('/members/create', [MemberController::class, 'create'])->name('members.create');
        Route::post('/members', [MemberController::class, 'store'])->name('members.store');
        Route::get('/members/{user}', [MemberController::class, 'show'])->name('members.show');
        Route::get('/members/{user}/edit', [MemberController::class, 'edit'])->name('members.edit');
        Route::put('/members/{user}', [MemberController::class, 'update'])->name('members.update');
        Route::post('/members/{user}/suspend', [MemberController::class, 'suspend'])->name('members.suspend');
        Route::post('/members/{user}/deactivate', [MemberController::class, 'deactivate'])->name('members.deactivate');
        Route::post('/members/{user}/reactivate', [MemberController::class, 'reactivate'])->name('members.reactivate');

        // Points
        Route::get('/points', [PointsController::class, 'ledger'])->name('points.ledger');
        Route::post('/members/{user}/points/credit', [PointsController::class, 'credit'])->name('points.credit');
        Route::post('/members/{user}/points/debit', [PointsController::class, 'debit'])->name('points.debit');
        Route::get('/members/{user}/points/history', [PointsController::class, 'history'])->name('points.history');

        // Tiers
        Route::get('/tiers', [TierController::class, 'index'])->name('tiers.index');
        Route::post('/tiers/toggle-auto-suggest', [TierController::class, 'toggleAutoSuggest'])->name('tiers.toggle-auto-suggest');
        Route::post('/tiers/run-assessment', [TierController::class, 'runAssessment'])->name('tiers.run-assessment');
        Route::get('/members/{user}/tier/evaluate', [TierController::class, 'evaluate'])->name('tiers.evaluate');
        Route::post('/members/{user}/tier/override', [TierController::class, 'override'])->name('tiers.override');
        Route::post('/tiers/assessments/{assessment}/action', [TierController::class, 'action'])->name('tiers.action');
    });

    // ─── Super Admin Only ─────────────────────────────────────────────────────
    Route::middleware('admin:super_admin')->group(function () {
        Route::get('/admins', [AdminUserController::class, 'index'])->name('admins.index');
        Route::post('/admins', [AdminUserController::class, 'store'])->name('admins.store');
        Route::post('/admins/{admin}/toggle-status', [AdminUserController::class, 'toggleStatus'])->name('admins.toggle-status');

        // Notification Settings
        Route::get('/settings/notifications', [NotificationSettingsController::class, 'index'])->name('settings.notifications');
        Route::put('/settings/notifications', [NotificationSettingsController::class, 'update'])->name('settings.notifications.update');
    });
});
