<?php

use App\Http\Controllers\BioPageController;
use App\Http\Controllers\RedirectController;
use App\Http\Middleware\ResolveWorkspace;
use App\Livewire\Pages\Ai\AbGenerator;
use App\Livewire\Pages\Ai\Anomalies;
use App\Livewire\Pages\Ai\Insights;
use App\Livewire\Pages\Dashboard\Overview;
use App\Livewire\Pages\Links\Index;
use App\Livewire\Pages\Profile\PersonalInfo;
use App\Livewire\Pages\Profile\Preferences;
use App\Livewire\Pages\Profile\Security;
use App\Livewire\Pages\Settings\ApiTokens;
use App\Livewire\Pages\Settings\Webhooks;
use App\Livewire\Pages\Team\Members;
use Illuminate\Support\Facades\Route;

Route::get('/health', function () {
    return response()->json(['status' => 'ok', 'timestamp' => now()->toISOString()]);
});

Route::view('/', 'welcome');

Route::get('dashboard', function () {
    // Redirect to first workspace or fall back to static view
    $user = auth()->user();
    $workspace = $user->workspaces()->first();
    if ($workspace) {
        return redirect()->route('w.dashboard', $workspace->ulid);
    }

    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::get('profile', fn () => redirect()->route('profile.personal'))->middleware(['auth'])->name('profile');

Route::middleware(['auth'])->group(function () {
    Route::get('/profile/personal', PersonalInfo::class)->name('profile.personal');
    Route::get('/profile/preferences', Preferences::class)->name('profile.preferences');
    Route::get('/profile/security', Security::class)->name('profile.security');
});

// Workspace-scoped routes — placed before slug catch-all
Route::middleware(['auth', 'verified', ResolveWorkspace::class])
    ->prefix('w/{workspace}')
    ->name('w.')
    ->group(function () {
        Route::get('/', Overview::class)->name('dashboard');
        Route::get('/links', Index::class)->name('links.index');
        Route::get('/qr', App\Livewire\Pages\QrCodes\Index::class)->name('qr.index');
        Route::get('/bio', App\Livewire\Pages\Bio\Index::class)->name('bio.index');
        Route::get('/analytics', App\Livewire\Pages\Analytics\Index::class)->name('analytics.index');
        Route::get('/domains', App\Livewire\Pages\Domains\Index::class)->name('domains.index');
        Route::get('/settings', App\Livewire\Pages\Settings\Index::class)->name('settings.index');
        Route::get('/settings/api-tokens', ApiTokens::class)->name('settings.api-tokens');
        Route::get('/settings/webhooks', Webhooks::class)->name('settings.webhooks');
        Route::get('/billing', App\Livewire\Pages\Billing\Index::class)->name('billing.index');
        Route::get('/team', Members::class)->name('team.index');
        Route::get('/ai/insights', Insights::class)->name('ai.insights');
        Route::get('/ai/anomalies', Anomalies::class)->name('ai.anomalies');
        Route::get('/ai/ab-generator', AbGenerator::class)->name('ai.ab-generator');
    });

// Bio page routes — must be before the slug catch-all
// /bio/{slug} for app.nimuli.com, /b/{slug} for nimu.li shorthand
Route::get('/bio/{slug}', [BioPageController::class, 'show'])->name('bio.public');
Route::get('/b/{slug}', [BioPageController::class, 'show'])->name('bio.short');

require __DIR__.'/auth.php';

// Redirect service — catch-all, must be last
Route::get('/{slug}', RedirectController::class)
    ->where('slug', '[a-zA-Z0-9_-]+')
    ->name('redirect');
