<?php

use App\Http\Controllers\AuditLogController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\LogoutController;
use App\Http\Controllers\Auth\ProfileController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Auth\TwoFactorChallengeController;
use App\Http\Controllers\Customer\CustomerController;
use App\Http\Controllers\Customer\CustomerPortalController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\HomepageController;
use App\Http\Controllers\Installer\InstallWizardController;
use App\Http\Controllers\Marketplace\MarketplaceController;
use App\Http\Controllers\Module\InstalledModuleController;
use App\Http\Controllers\Role\RoleController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\Staff\DepartmentController;
use App\Http\Controllers\Staff\DesignationController;
use App\Http\Controllers\Staff\StaffController;
use App\Http\Controllers\Update\UpdateController;
use App\Http\Controllers\Wallet\WalletController;
use Illuminate\Support\Facades\Route;

Route::get('/', HomepageController::class)->name('home');

Route::middleware('not.installed')->prefix('install')->name('installer.')->group(function () {
    Route::get('/', [InstallWizardController::class, 'welcome'])->name('welcome');
    Route::post('/', [InstallWizardController::class, 'storeWelcome'])->name('welcome.store');

    Route::middleware('installer.progress:requirements')->group(function () {
        Route::get('/requirements', [InstallWizardController::class, 'requirements'])->name('requirements');
        Route::post('/requirements', [InstallWizardController::class, 'storeRequirements'])->name('requirements.store');
    });

    Route::middleware('installer.progress:database')->group(function () {
        Route::get('/database', [InstallWizardController::class, 'database'])->name('database');
        Route::post('/database', [InstallWizardController::class, 'storeDatabase'])->name('database.store');
    });

    Route::middleware('installer.progress:mail')->group(function () {
        Route::get('/mail', [InstallWizardController::class, 'mail'])->name('mail');
        Route::post('/mail', [InstallWizardController::class, 'storeMail'])->name('mail.store');
    });

    Route::middleware('installer.progress:company')->group(function () {
        Route::get('/company', [InstallWizardController::class, 'company'])->name('company');
        Route::post('/company', [InstallWizardController::class, 'storeCompany'])->name('company.store');
    });

    Route::middleware('installer.progress:admin')->group(function () {
        Route::get('/admin', [InstallWizardController::class, 'admin'])->name('admin');
        Route::post('/admin', [InstallWizardController::class, 'storeAdmin'])->name('admin.store');
    });

    Route::middleware('installer.progress:review')->group(function () {
        Route::get('/review', [InstallWizardController::class, 'review'])->name('review');
        Route::post('/finalize', [InstallWizardController::class, 'finalize'])->name('finalize');
    });

    Route::get('/complete', [InstallWizardController::class, 'complete'])->name('complete');
});

Route::middleware(['installed', 'guest'])->group(function () {
    Route::get('/login', [LoginController::class, 'create'])->name('login');
    Route::post('/login', [LoginController::class, 'store'])->name('login.store');

    Route::get('/forgot-password', [ForgotPasswordController::class, 'create'])->name('password.request');
    Route::post('/forgot-password', [ForgotPasswordController::class, 'store'])->name('password.email');

    Route::get('/reset-password/{token}', [ResetPasswordController::class, 'create'])->name('password.reset');
    Route::post('/reset-password', [ResetPasswordController::class, 'store'])->name('password.update');

    Route::middleware('registration.enabled')->group(function () {
        Route::get('/register', [RegisterController::class, 'create'])->name('register');
        Route::post('/register', [RegisterController::class, 'store'])->name('register.store');
    });

    Route::get('/two-factor-challenge', [TwoFactorChallengeController::class, 'create'])->name('two-factor.challenge');
    Route::post('/two-factor-challenge', [TwoFactorChallengeController::class, 'store'])->name('two-factor.verify');
    Route::post('/two-factor-challenge/resend', [TwoFactorChallengeController::class, 'resend'])->name('two-factor.resend');
});

Route::middleware(['installed', 'auth', 'active.user'])->group(function () {
    Route::post('/logout', LogoutController::class)->name('logout');

    Route::get('/my-account', CustomerPortalController::class)->name('customer.home');

    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password');
    Route::put('/profile/two-factor', [ProfileController::class, 'updateTwoFactor'])->name('profile.two-factor');

    Route::middleware('staff')->group(function () {
        Route::get('/dashboard', DashboardController::class)->name('dashboard');

        Route::resource('staff', StaffController::class);
        Route::resource('departments', DepartmentController::class)->except(['show']);
        Route::resource('designations', DesignationController::class)->except(['show']);
        Route::resource('roles', RoleController::class)->except(['show']);

        Route::get('/customers/{customer}/edit', [CustomerController::class, 'edit'])->name('customers.edit');
        Route::put('/customers/{customer}', [CustomerController::class, 'update'])->name('customers.update');
        Route::patch('/customers/{customer}/status', [CustomerController::class, 'updateStatus'])->name('customers.status');

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

        Route::get('/updates', [UpdateController::class, 'index'])->name('updates.index');
        Route::post('/updates/check', [UpdateController::class, 'check'])->name('updates.check');
        Route::put('/updates/settings', [UpdateController::class, 'saveSettings'])->name('updates.settings');
        Route::post('/updates/apply', [UpdateController::class, 'apply'])->name('updates.apply');

        Route::get('/audit-logs', [AuditLogController::class, 'index'])->name('audit-logs.index');
        Route::get('/audit-logs/{auditLog}', [AuditLogController::class, 'show'])->name('audit-logs.show');

        Route::middleware('license')->group(function () {
            Route::get('/modules', [InstalledModuleController::class, 'index'])->name('modules.index');
            Route::get('/modules/{module}', [InstalledModuleController::class, 'show'])->name('modules.show');
            Route::delete('/modules/{module}', [InstalledModuleController::class, 'destroy'])->name('modules.destroy');

            Route::get('/marketplace', [MarketplaceController::class, 'index'])->name('marketplace.index');
            Route::post('/marketplace/{moduleUuid}/install', [MarketplaceController::class, 'install'])->name('marketplace.install');
            Route::get('/marketplace/{moduleUuid}', [MarketplaceController::class, 'show'])->name('marketplace.show');

            Route::get('/wallet', [WalletController::class, 'index'])->name('wallet.index');
        });
    });
});
