<?php

use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\Admin\MemberController;
use App\Http\Controllers\Installer\InstallController;
use App\Http\Controllers\Marketing\LeadController;
use Illuminate\Support\Facades\Route;

Route::prefix('install')->name('install.')->middleware('install.only')->group(function () {
    Route::get('/', [InstallController::class, 'requirements'])->name('requirements');
    Route::get('/database', [InstallController::class, 'database'])->name('database');
    Route::post('/database/test', [InstallController::class, 'testDatabase'])->name('database.test');
    Route::post('/database', [InstallController::class, 'storeDatabase'])->name('database.store');
    Route::get('/admin', [InstallController::class, 'admin'])->name('admin');
    Route::post('/admin', [InstallController::class, 'storeAdmin'])->name('admin.store');
    Route::get('/company', [InstallController::class, 'company'])->name('company');
    Route::post('/company', [InstallController::class, 'storeCompany'])->name('company.store');
    Route::get('/cron', [InstallController::class, 'cron'])->name('cron');
    Route::post('/complete', [InstallController::class, 'complete'])->name('complete');
});

Route::middleware('installed')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('/login/member', function () {
            return view('auth.login', [
                'title' => 'Member Login - Lakhsman Wellness',
                'heading' => 'Member Login',
                'subheading' => 'Access your member dashboard, wallet, and team reports.',
                'formAction' => route('login.member.submit'),
                'registerLink' => route('register.user'),
                'expectedRole' => 'user',
            ]);
        })->name('login.member');
        Route::get('/login-user', fn () => redirect()->route('login.member'))->name('login.user');
        Route::post('/login/member', [AuthController::class, 'login'])
            ->defaults('expected_role', 'user')
            ->middleware('throttle:login')
            ->name('login.member.submit');
        Route::post('/login-user', [AuthController::class, 'login'])
            ->defaults('expected_role', 'user')
            ->middleware('throttle:login')
            ->name('login.user.submit');

        Route::get('/login/franchise', function () {
            return view('auth.login', [
                'title' => 'Franchise Login - Lakhsman Wellness',
                'heading' => 'Franchise Login',
                'subheading' => 'Manage stock, sales, commission, and franchise operations.',
                'formAction' => route('login.franchise.submit'),
                'registerLink' => route('register.franchise'),
                'expectedRole' => 'franchise',
            ]);
        })->name('login.franchise');
        Route::post('/login/franchise', [AuthController::class, 'login'])
            ->defaults('expected_role', 'franchise')
            ->middleware('throttle:login')
            ->name('login.franchise.submit');

        Route::get('/login-franchise', fn () => redirect()->route('login.franchise'))->name('login.franchise.alias');
        Route::post('/login-franchise', [AuthController::class, 'login'])
            ->defaults('expected_role', 'franchise')
            ->middleware('throttle:login')
            ->name('login.franchise.alias.submit');

        Route::get('/register/member', [AuthController::class, 'showRegister'])->name('register.member');
        Route::get('/register-user', fn () => redirect()->route('register.member'))->name('register.user');
        Route::post('/register/member', [AuthController::class, 'register'])->name('register.member.submit');
        Route::post('/register-user', [AuthController::class, 'register'])->name('register.user.submit');

        Route::get('/register/franchise', function () {
            return view('marketing.franchise-register');
        })->name('register.franchise');
        Route::post('/register/franchise', [LeadController::class, 'store'])
            ->defaults('lead_type', 'franchise')
            ->name('register.franchise.submit');
        Route::get('/register-franchise', fn () => redirect()->route('register.franchise'))->name('register.franchise.alias');
        Route::post('/register-franchise', [LeadController::class, 'store'])
            ->defaults('lead_type', 'franchise')
            ->name('register.franchise.alias.submit');
    });

    require __DIR__.'/auth.php';
    require __DIR__.'/marketing.php';
    require __DIR__.'/user.php';
    require __DIR__.'/franchise.php';
    require __DIR__.'/admin.php';

    Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');
    Route::post('/impersonate/stop', [MemberController::class, 'stopImpersonate'])
        ->middleware('auth')
        ->name('impersonate.stop');
});
