<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\FrontendController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Auth\ProfileController;

use App\Http\Controllers\Admin\DashboardController as AdminDashboard;
use App\Http\Controllers\Admin\CategoryController as AdminCategory;
use App\Http\Controllers\Admin\PackageController as AdminPackage;
use App\Http\Controllers\Admin\UserController as AdminUser;
use App\Http\Controllers\Admin\ResellerController as AdminReseller;
use App\Http\Controllers\Admin\InvoiceController as AdminInvoice;
use App\Http\Controllers\Admin\BlogController as AdminBlog;
use App\Http\Controllers\Admin\SettingsController as AdminSettings;

use App\Http\Controllers\Reseller\DashboardController as ResellerDashboard;
use App\Http\Controllers\Reseller\BlogController as ResellerBlog;
use App\Http\Controllers\Reseller\InvoiceController as ResellerInvoice;
use App\Http\Controllers\Reseller\PackageController as ResellerPackage;

use App\Http\Controllers\User\DashboardController as UserDashboard;
use App\Http\Controllers\User\InvoiceController as UserInvoice;
use App\Http\Controllers\User\PackageController as UserPackage;

use App\Http\Controllers\PayPalController;
use App\Http\Controllers\TapPaymentController;

/* PUBLIC */
Route::get('/', [FrontendController::class, 'home'])->name('home');
Route::get('/blogs', [FrontendController::class, 'blogs'])->name('blogs.index');
Route::get('/blog/{slug}', [FrontendController::class, 'show'])->name('blogs.show');
Route::get('/category/{slug}', [FrontendController::class, 'category'])->name('blogs.category');
Route::get('/author/{username}', [FrontendController::class, 'author'])->name('blogs.author');
Route::get('/packages', [FrontendController::class, 'packages'])->name('packages.public');
Route::get('/about', [FrontendController::class, 'about'])->name('about');
Route::get('/contact', [FrontendController::class, 'contact'])->name('contact');

/* AUTH */
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
    Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('/register', [RegisterController::class, 'register']);
    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');
});
Route::post('/logout', [LoginController::class, 'logout'])->name('logout')->middleware('auth');

/* DASHBOARD ROUTER */
Route::get('/dashboard', function () {
    $u = auth()->user();
    if (!$u) return redirect('/login');
    return match ($u->role) {
        'superadmin' => redirect()->route('admin.dashboard'),
        'reseller'   => redirect()->route('reseller.dashboard'),
        default      => redirect()->route('user.dashboard'),
    };
})->middleware('auth')->name('dashboard');

/* PROFILE */
Route::middleware('auth')->prefix('profile')->name('profile.')->group(function () {
    Route::get('/', [ProfileController::class, 'edit'])->name('edit');
    Route::put('/', [ProfileController::class, 'update'])->name('update');
    Route::put('/password', [ProfileController::class, 'updatePassword'])->name('password');
    Route::post('/avatar', [ProfileController::class, 'updateAvatar'])->name('avatar');
});

/* SUPERADMIN */
Route::middleware(['auth', 'role:superadmin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminDashboard::class, 'index'])->name('dashboard');
    Route::resource('categories', AdminCategory::class)->except(['show']);
    Route::resource('packages', AdminPackage::class)->except(['show']);

    Route::get('users', [AdminUser::class, 'index'])->name('users.index');
    Route::get('users/{user}/edit', [AdminUser::class, 'edit'])->name('users.edit');
    Route::put('users/{user}', [AdminUser::class, 'update'])->name('users.update');
    Route::delete('users/{user}', [AdminUser::class, 'destroy'])->name('users.destroy');
    Route::post('users/{user}/toggle-status', [AdminUser::class, 'toggleStatus'])->name('users.toggle');

    Route::get('resellers/create', [AdminReseller::class, 'create'])->name('resellers.create');
    Route::post('resellers', [AdminReseller::class, 'store'])->name('resellers.store');
    Route::get('resellers/{user}/quota', [AdminReseller::class, 'editQuota'])->name('resellers.quota.edit');
    Route::put('resellers/{user}/quota', [AdminReseller::class, 'updateQuota'])->name('resellers.quota.update');

    Route::get('invoices', [AdminInvoice::class, 'index'])->name('invoices.index');
    Route::get('invoices/create', [AdminInvoice::class, 'create'])->name('invoices.create');
    Route::post('invoices', [AdminInvoice::class, 'store'])->name('invoices.store');
    Route::get('invoices/{invoice}', [AdminInvoice::class, 'show'])->name('invoices.show');
    Route::post('invoices/{invoice}/mark-paid', [AdminInvoice::class, 'markPaid'])->name('invoices.markPaid');
    Route::post('invoices/{invoice}/mark-unpaid', [AdminInvoice::class, 'markUnpaid'])->name('invoices.markUnpaid');
    Route::delete('invoices/{invoice}', [AdminInvoice::class, 'destroy'])->name('invoices.destroy');

    Route::get('blogs', [AdminBlog::class, 'index'])->name('blogs.index');
    Route::get('blogs/create', [AdminBlog::class, 'create'])->name('blogs.create');
    Route::post('blogs', [AdminBlog::class, 'store'])->name('blogs.store');
    Route::get('blogs/{blog}/edit', [AdminBlog::class, 'edit'])->name('blogs.edit');
    Route::put('blogs/{blog}', [AdminBlog::class, 'update'])->name('blogs.update');
    Route::delete('blogs/{blog}', [AdminBlog::class, 'destroy'])->name('blogs.destroy');

    // Settings (general / seo / security tabs)
    Route::get('settings', [AdminSettings::class, 'edit'])->name('settings.edit');
    Route::put('settings/general',  [AdminSettings::class, 'updateGeneral'])->name('settings.general');
    Route::put('settings/seo',      [AdminSettings::class, 'updateSeo'])->name('settings.seo');
    Route::put('settings/security', [AdminSettings::class, 'updateSecurity'])->name('settings.security');
    Route::put('settings/templates', [AdminSettings::class, 'updateTemplates'])->name('settings.templates');
    Route::put('settings/smtp', [AdminSettings::class, 'updateSmtp'])->name('settings.smtp');
    Route::put('settings/payment', [AdminSettings::class, 'updatePayment'])->name('settings.payment');
});

/* RESELLER */
Route::middleware(['auth', 'role:reseller'])->prefix('reseller')->name('reseller.')->group(function () {
    Route::get('/dashboard', [ResellerDashboard::class, 'index'])->name('dashboard');
    Route::resource('blogs', ResellerBlog::class);
    Route::get('invoices', [ResellerInvoice::class, 'index'])->name('invoices.index');
    Route::get('invoices/{invoice}', [ResellerInvoice::class, 'show'])->name('invoices.show');
    Route::get('invoices/{invoice}/pay', [ResellerInvoice::class, 'pay'])->name('invoices.pay');
    Route::get('packages', [ResellerPackage::class, 'index'])->name('packages.index');
    Route::post('packages/{package}/buy', [ResellerPackage::class, 'buy'])->name('packages.buy');
});

/* NORMAL USER */
Route::middleware(['auth', 'role:user'])->prefix('user')->name('user.')->group(function () {
    Route::get('/dashboard', [UserDashboard::class, 'index'])->name('dashboard');
    Route::get('invoices', [UserInvoice::class, 'index'])->name('invoices.index');
    Route::get('invoices/{invoice}', [UserInvoice::class, 'show'])->name('invoices.show');
    Route::get('invoices/{invoice}/pay', [UserInvoice::class, 'pay'])->name('invoices.pay');
    Route::get('packages', [UserPackage::class, 'index'])->name('packages.index');
    Route::post('packages/{package}/buy', [UserPackage::class, 'buy'])->name('packages.buy');
});

/* PAYPAL */
Route::middleware('auth')->prefix('paypal')->name('paypal.')->group(function () {
    Route::get('/process/{invoice}', [PayPalController::class, 'process'])->name('process');
    Route::get('/success', [PayPalController::class, 'success'])->name('success');
    Route::get('/cancel', [PayPalController::class, 'cancel'])->name('cancel');
});

/* TAP PAYMENT */
Route::middleware('auth')->prefix('tap')->name('tap.')->group(function () {
    Route::get('/process/{invoice}', [TapPaymentController::class, 'process'])->name('process');
    Route::get('/success', [TapPaymentController::class, 'success'])->name('success');
    Route::get('/cancel', [TapPaymentController::class, 'cancel'])->name('cancel');
});
