<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Laravel\Fortify\Features;
use Livewire\Volt\Volt;

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

// Test Route für PGSQL - kann beliebig abgeändert werden (zum Testen)
Route::get('/test-zweite-db', function () {
    try {
        // Eine einfache Abfrage auf der zweiten Verbindung
        $result = DB::connection('pgsql_second')->select('SELECT NOW() as current_time');

        return response()->json([
            'status' => 'Erfolgreich verbunden mit zweiter Datenbank',
            'zeitstempel' => $result[0]->current_time,
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'status' => 'Fehler bei der Verbindung',
            'meldung' => $e->getMessage(),
        ], 500);
    }
});

Route::view('dashboard', 'dashboard')
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Volt::route('company-search', 'company-search')
    ->middleware(['auth', 'verified'])
    ->name('company-search');

Volt::route('transaction-review', 'transaction-review')
    ->middleware(['auth', 'verified'])
    ->name('transaction-review');

Volt::route('companies/{company}/transactions', 'companies.transactions')
    ->middleware(['auth', 'verified'])
    ->name('company.transactions');

Volt::route('upload', 'upload.index')
    ->middleware(['auth', 'verified'])
    ->name('upload');

Route::middleware(['auth'])->group(function () {
    Route::redirect('settings', 'settings/profile');

    Volt::route('settings/profile', 'settings.profile')->name('profile.edit');
    Volt::route('settings/password', 'settings.password')->name('password.edit');
    Volt::route('settings/appearance', 'settings.appearance')->name('appearance.edit');

    Volt::route('settings/two-factor', 'settings.two-factor')
        ->middleware(
            when(
                Features::canManageTwoFactorAuthentication()
                    && Features::optionEnabled(Features::twoFactorAuthentication(), 'confirmPassword'),
                ['password.confirm'],
                [],
            ),
        )
        ->name('two-factor.show');
});

require __DIR__.'/auth.php';
