<?php

use App\Http\Controllers\AjoGroupController;
use App\Http\Controllers\ArrearsController;
use App\Http\Controllers\BillingController;
use App\Http\Controllers\BusinessSettingsController;
use App\Http\Controllers\ContributionController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\InstallerController;
use App\Http\Controllers\MemberController;
use App\Http\Controllers\PayoutController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\StaffController;
use App\Http\Controllers\TwoFactorController;
use Illuminate\Support\Facades\Route;

Route::prefix('install')->name('install.')->middleware('not-installed')->group(function (): void {
    Route::get('/', [InstallerController::class, 'welcome'])->name('welcome');
    Route::get('requirements', [InstallerController::class, 'requirements'])->name('requirements');
    Route::get('database', [InstallerController::class, 'database'])->name('database');
    Route::post('database', [InstallerController::class, 'saveDatabase'])->middleware('throttle:10,1')->name('database.save');
    Route::get('account', [InstallerController::class, 'account'])->name('account');
    Route::post('account', [InstallerController::class, 'install'])->middleware('throttle:3,1')->name('run');
    Route::get('complete', [InstallerController::class, 'complete'])->name('complete');
});

Route::get('/', function () {
    return view('welcome');
});

Route::view('/subscription/expired', 'subscription-expired')->middleware('auth')->name('subscription.expired');
Route::middleware(['auth', 'verified', 'business', 'two-factor'])->group(function (): void {
    Route::get('billing', [BillingController::class, 'index'])->name('billing.index');
    Route::post('billing/checkout/{plan}', [BillingController::class, 'checkout'])->middleware('throttle:5,1')->name('billing.checkout');
    Route::get('billing/callback', [BillingController::class, 'callback'])->middleware('throttle:10,1')->name('billing.callback');
});

Route::middleware(['auth', 'verified', 'business', 'two-factor', 'subscribed'])->group(function () {
    Route::get('/dashboard', DashboardController::class)->name('dashboard');
    Route::resource('groups', AjoGroupController::class)->except('show');
    Route::resource('members', MemberController::class)->except('show');
    Route::resource('contributions', ContributionController::class)->except('show');
    Route::resource('payouts', PayoutController::class)->except('show');
    Route::get('arrears', ArrearsController::class)->name('arrears.index');
    Route::get('reports', [ReportController::class, 'index'])->name('reports.index');
    Route::get('reports/export', [ReportController::class, 'export'])->name('reports.export');
    Route::get('settings/business', [BusinessSettingsController::class, 'edit'])->name('settings.business.edit');
    Route::put('settings/business', [BusinessSettingsController::class, 'update'])->name('settings.business.update');
    Route::resource('staff', StaffController::class)->except('show');
});

Route::middleware('auth')->group(function () {
    Route::get('two-factor/setup', [TwoFactorController::class, 'setup'])->name('two-factor.setup');
    Route::post('two-factor/enable', [TwoFactorController::class, 'enable'])->middleware('throttle:6,1')->name('two-factor.enable');
    Route::delete('two-factor', [TwoFactorController::class, 'disable'])->name('two-factor.disable');
    Route::get('two-factor/challenge', [TwoFactorController::class, 'challenge'])->name('two-factor.challenge');
    Route::post('two-factor/challenge', [TwoFactorController::class, 'verifyChallenge'])->middleware('throttle:6,1')->name('two-factor.verify');
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
