<?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-user', function () {
            return view('auth.login', [
                'title' => 'Distributor Login - AKRSera MLM',
                'heading' => 'Distributor Login',
                'formAction' => route('login.user.submit'),
                'registerLink' => route('register.user'),
                'expectedRole' => 'user',
            ]);
        })->name('login.user');
        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 - AKRSera MLM',
                'heading' => 'Franchise Login',
                '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('/register-user', [AuthController::class, 'showRegister'])->name('register.user');
        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');
    });

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

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