<?php

use App\Http\Controllers\AppPagesController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\LiveController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return auth()->check()
        ? app(AppPagesController::class)->home()
        : redirect()->route('guest.welcome');
})->name('app.home');

Route::get('/welcome', function () {
    return view('guest.welcome');
})->middleware('guest')->name('guest.welcome');

Route::get('/dashboard', function () {
    return redirect()->route('app.home');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware(['auth', 'locale'])->group(function () {
    Route::get('/app', fn () => redirect()->route('app.home'))->name('app');

    Route::prefix('/admin')->middleware('super_admin')->group(function () {
        Route::get('/', [AdminController::class, 'dashboard'])->name('admin.dashboard');
        Route::get('/users', [AdminController::class, 'users'])->name('admin.users');
        Route::get('/users/{user}', [AdminController::class, 'showUser'])->name('admin.users.show');
        Route::post('/users/{user}/impersonate', [AdminController::class, 'impersonate'])->name('admin.users.impersonate');
        Route::patch('/users/{user}/super-admin', [AdminController::class, 'updateUserSuperAdmin'])->name('admin.users.super-admin');
        Route::delete('/users/{user}', [AdminController::class, 'destroyUser'])->name('admin.users.destroy');
        Route::get('/spaces', [AdminController::class, 'spaces'])->name('admin.spaces');
        Route::get('/spaces/{spaceId}', [AdminController::class, 'showSpace'])->name('admin.spaces.show');
    });

    Route::post('/admin/impersonation/leave', [AdminController::class, 'leaveImpersonation'])
        ->name('admin.impersonation.leave');

    Route::get('/onboarding', [AppPagesController::class, 'onboarding'])->name('app.onboarding');
    Route::post('/onboarding', [AppPagesController::class, 'completeOnboarding'])->name('app.onboarding.complete');

    Route::middleware(['onboarded'])->group(function () {
        Route::get('/live/poll', [LiveController::class, 'poll'])->name('live.poll');
        Route::get('/groups', [AppPagesController::class, 'groups'])->name('app.groups');
        Route::get('/group/{id}', [AppPagesController::class, 'groupDetail'])->name('app.group.detail');
        Route::get('/group/{id}/members', [AppPagesController::class, 'groupMembers'])->name('app.group.members');
        Route::post('/group/{id}/members/add', [AppPagesController::class, 'addMemberToGroup'])->name('app.group.members.add');
        Route::post('/group/{id}/members/{personId}/remove', [AppPagesController::class, 'removeMemberFromGroup'])->name('app.group.members.remove');
        Route::post('/group/{id}/update', [AppPagesController::class, 'updateGroup'])->name('app.group.update');
        Route::post('/group/{id}/invitations/{invitationId}/revoke', [AppPagesController::class, 'revokeGroupInvitationFromGroup'])->name('app.group.invitations.revoke');
        Route::get('/group/{groupId}/expense/{expenseId}', [AppPagesController::class, 'expenseDetail'])->name('app.group.expense.detail');

        Route::get('/activity', [AppPagesController::class, 'activity'])->name('app.activity');
        Route::get('/account', [AppPagesController::class, 'account'])->name('app.account');
        Route::get('/account/profile', [AppPagesController::class, 'editAccountProfile'])->name('app.account.profile.edit');
        Route::post('/account/profile', [AppPagesController::class, 'updateAccountProfile'])->name('app.account.profile.update');
        Route::post('/account/preferences', [AppPagesController::class, 'updateAccountPreferences'])->name('app.account.preferences.update');
        Route::post('/account/invitations/{id}/revoke', [AppPagesController::class, 'revokeGroupInvitation'])->name('app.account.invitations.revoke');
        Route::get('/insights', [AppPagesController::class, 'insights'])->name('app.insights');

        Route::get('/add-expense', [AppPagesController::class, 'addExpense'])->name('app.add-expense');
        Route::post('/add-expense', [AppPagesController::class, 'storeExpense'])->name('app.add-expense.store');
        Route::get('/create-group', [AppPagesController::class, 'createGroup'])->name('app.create-group');
        Route::post('/create-group', [AppPagesController::class, 'storeGroup'])->name('app.create-group.store');
        Route::get('/settle/{id}', [AppPagesController::class, 'settleUp'])->name('app.settle-up');
        Route::post('/settle/{id}', [AppPagesController::class, 'storeGroupSettlement'])->name('app.settle-up.store');

        Route::get('/shared-lists/intro', [AppPagesController::class, 'sharedListsIntro'])->name('app.shared-lists.intro');
        Route::get('/shared-lists/new', [AppPagesController::class, 'sharedListsNew'])->name('app.shared-lists.new');
        Route::post('/shared-lists/new', [AppPagesController::class, 'storeSharedList'])->name('app.shared-lists.store');
        Route::get('/shared-lists/{id}/settle', [AppPagesController::class, 'settleSharedList'])->name('app.shared-lists.settle');
        Route::post('/shared-lists/{id}/settle', [AppPagesController::class, 'storeSharedListSettlement'])->name('app.shared-lists.settle.store');
        Route::get('/shared-lists/{id}/expense/{expenseId}', [AppPagesController::class, 'expenseDetail'])->name('app.shared-lists.expense.detail');
        Route::get('/shared-lists/{id}', [AppPagesController::class, 'sharedListDetail'])->name('app.shared-lists.detail');
        Route::get('/shared-lists/{id}/members', [AppPagesController::class, 'sharedListMembers'])->name('app.shared-lists.members');
        Route::post('/shared-lists/{id}/members/add', [AppPagesController::class, 'addMemberToSharedList'])->name('app.shared-lists.members.add');
        Route::post('/shared-lists/{id}/checkout', [AppPagesController::class, 'checkoutSharedList'])->name('app.shared-lists.checkout');
        Route::post('/shared-lists/{id}/items', [AppPagesController::class, 'addSharedListItem'])->name('app.shared-lists.items.add');
        Route::post('/shared-lists/{id}/items/{itemId}', [AppPagesController::class, 'updateSharedListItem'])->name('app.shared-lists.items.update');
        Route::post('/shared-lists/{id}/items/{itemId}/delete', [AppPagesController::class, 'deleteSharedListItem'])->name('app.shared-lists.items.delete');
        Route::post('/shared-lists/{id}/items/{itemId}/toggle', [AppPagesController::class, 'toggleSharedListItem'])->name('app.shared-lists.items.toggle');
        Route::post('/shared-lists/{id}/notes', [AppPagesController::class, 'updateSharedListNotes'])->name('app.shared-lists.notes.update');
        Route::post('/shared-lists/{id}/receipt/settle-all', [AppPagesController::class, 'settleSharedListReceiptAll'])->name('app.shared-lists.receipt.settleAll');

        Route::get('/shared-purchases/intro', [AppPagesController::class, 'sharedPurchasesIntro'])->name('app.shared-purchases.intro');
        Route::get('/shared-purchases/new', [AppPagesController::class, 'sharedPurchasesNew'])->name('app.shared-purchases.new');
        Route::post('/shared-purchases/new', [AppPagesController::class, 'storeSharedPurchase'])->name('app.shared-purchases.store');
        Route::get('/shared-purchases/{id}', [AppPagesController::class, 'sharedPurchaseDetail'])->name('app.shared-purchases.detail');
        Route::post('/shared-purchases/{id}', [AppPagesController::class, 'updateSharedPurchase'])->name('app.shared-purchases.update');

        Route::post('/push/subscribe', [\App\Http\Controllers\PushSubscriptionController::class, 'store'])->name('push.subscribe');
        Route::post('/push/unsubscribe', [\App\Http\Controllers\PushSubscriptionController::class, 'destroy'])->name('push.unsubscribe');
    });
});

Route::middleware('auth')->group(function () {
    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';
