<?php

use App\Http\Controllers\ApiLogController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SongPdfController;
use App\Http\Controllers\SyncController;
use App\Http\Controllers\TranslationController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::get('/auth/churchtools', [AuthController::class, 'redirect'])->name('auth.churchtools');
    Route::get('/auth/churchtools/callback', [AuthController::class, 'callback'])->name('auth.churchtools.callback');
});

if (app()->environment('local', 'testing')) {
    Route::middleware('guest')->group(function () {
        Route::post('/dev-login', function () {
            $user = \App\Models\User::updateOrCreate(
                ['email' => 'test@local.dev'],
                [
                    'name' => 'Test Benutzer',
                    'churchtools_id' => 99999,
                    'password' => '',
                    'avatar' => null,
                    'churchtools_groups' => [],
                    'churchtools_roles' => [],
                ]
            );
            Auth::login($user);

            return redirect()->route('dashboard');
        })->name('dev-login');
    });
}

Route::post('/logout', [AuthController::class, 'logout'])
    ->middleware('auth')
    ->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/', function () {
        return redirect()->route('dashboard');
    });

    Route::get('/dashboard', function () {
        return Inertia::render('Dashboard');
    })->name('dashboard');

    Route::get('/services', [ServiceController::class, 'index'])->name('services.index');
    Route::post('/services/{service}/finalize', [ServiceController::class, 'finalize'])->name('services.finalize');
    Route::post('/services/{service}/reopen', [ServiceController::class, 'reopen'])->name('services.reopen');
    Route::delete('/services/{service}', [ServiceController::class, 'destroy'])->name('services.destroy');
    Route::get('/services/{service}/download', [ServiceController::class, 'download'])->name('services.download');
    Route::get('/services/{service}/download-bundle/{blockType}', [ServiceController::class, 'downloadBundle'])->name('services.download-bundle');
    Route::get('/services/{service}/agenda-items/{agendaItem}/download', [ServiceController::class, 'downloadAgendaItem'])->name('services.agenda-item.download');
    Route::get('/services/{service}/edit', [ServiceController::class, 'edit'])->name('services.edit');
    Route::get('/songs/{song}/translate', [TranslationController::class, 'page'])->name('songs.translate');

    Route::get('/songs', function () {
        return Inertia::render('Songs/Index');
    })->name('songs.index');

    Route::get('/api-logs', [ApiLogController::class, 'index'])->name('api-logs.index');
    Route::get('/api-logs/{log}/response-body', [ApiLogController::class, 'responseBody'])->name('api-logs.response-body');

    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::patch('/settings', [SettingsController::class, 'update'])->name('settings.update');

    Route::post('/songs/{song}/arrangements', '\\App\\Http\\Controllers\\ArrangementController@store')->name('arrangements.store');
    Route::post('/arrangements/{arrangement}/clone', '\\App\\Http\\Controllers\\ArrangementController@clone')->name('arrangements.clone');
    Route::put('/arrangements/{arrangement}', '\\App\\Http\\Controllers\\ArrangementController@update')->name('arrangements.update');
    Route::delete('/arrangements/{arrangement}', '\\App\\Http\\Controllers\\ArrangementController@destroy')->name('arrangements.destroy');

    Route::get('/songs/{song}/arrangements/{arrangement}/pdf', [SongPdfController::class, 'download'])->name('songs.pdf');
    Route::get('/songs/{song}/arrangements/{arrangement}/preview', [SongPdfController::class, 'preview'])->name('songs.preview');
    Route::post('/sync', [SyncController::class, 'sync'])->name('sync');

    /*
    |--------------------------------------------------------------------------
    | Folien-Verwaltung
    |--------------------------------------------------------------------------
    */
    Route::post('/slides', '\\App\\Http\\Controllers\\SlideController@store')->name('slides.store');
    Route::delete('/slides/bulk', '\\App\\Http\\Controllers\\SlideController@destroyBulk')->name('slides.bulk-destroy');
    Route::post('/slides/reorder', '\\App\\Http\\Controllers\\SlideController@reorder')->name('slides.reorder');
    Route::delete('/slides/{slide}', '\\App\\Http\\Controllers\\SlideController@destroy')->name('slides.destroy');
    Route::patch('/slides/{slide}/expire-date', '\\App\\Http\\Controllers\\SlideController@updateExpireDate')->name('slides.update-expire-date');
});
