<?php

use App\Http\Controllers\ApiLogController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\BookmarkletController;
use App\Http\Controllers\CcliPasteController;
use App\Http\Controllers\ExportProFileController;
use App\Http\Controllers\LabelImportController;
use App\Http\Controllers\MacroAssignmentController;
use App\Http\Controllers\MacroImportController;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\ServiceImageController;
use App\Http\Controllers\ServiceMacroOverrideController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SongPdfController;
use App\Http\Controllers\SongSectionController;
use App\Http\Controllers\SyncController;
use App\Http\Controllers\TranslationController;
use App\Models\Service;
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);

            $service = Service::nextUpcoming()->first();

            return $service
                ? redirect()->route('services.edit', $service->id)
                : redirect()->route('services.index');
        })->name('dev-login');
    });
}

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

Route::get('/bookmarklets/ccli-import.js', [BookmarkletController::class, 'show'])->name('bookmarklets.ccli');

Route::middleware('auth')->group(function () {
    Route::get('/', function () {
        $service = Service::nextUpcoming()->first();

        if ($service) {
            return redirect()->route('services.edit', $service->id);
        }

        return redirect()->route('services.index');
    });

    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-preview', [ServiceController::class, 'downloadPreview'])->name('services.download-preview');
    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::post('/services/{service}/key-visual', [ServiceImageController::class, 'storeKeyVisual'])->name('services.key-visual.store');
    Route::post('/services/{service}/background', [ServiceImageController::class, 'storeBackground'])->name('services.background.store');
    Route::patch('/services/{service}/name-overrides', [ServiceController::class, 'updateNameOverrides'])->name('services.name-overrides.update');
    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('/songs/import-from-ccli-paste', [CcliPasteController::class, 'showImportPage'])
        ->name('songs.import-from-ccli-paste');

    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::post('/songs/{song}/sections', [SongSectionController::class, 'store'])->name('songs.sections.store');
    Route::patch('/songs/{song}/sections/{section}', [SongSectionController::class, 'update'])->name('songs.sections.update');
    Route::delete('/songs/{song}/sections/{section}', [SongSectionController::class, 'destroy'])->name('songs.sections.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');

    /*
    |--------------------------------------------------------------------------
    | Makro- und Label-Import (ProPresenter)
    |--------------------------------------------------------------------------
    */
    Route::post('/settings/export-pro-files', [ExportProFileController::class, 'store'])->name('settings.export-pro-files.store');
    Route::delete('/settings/export-pro-files/{exportProFile}', [ExportProFileController::class, 'destroy'])->name('settings.export-pro-files.destroy');
    Route::patch('/settings/export-pro-files/{exportProFile}/keyword', [ExportProFileController::class, 'updateKeyword'])->name('settings.export-pro-files.update-keyword');
    Route::post('/settings/macros/import', [MacroImportController::class, 'store'])->name('settings.macros.import');
    Route::post('/settings/labels/import', [LabelImportController::class, 'store'])->name('settings.labels.import');

    /*
    |--------------------------------------------------------------------------
    | Globale Makro-Zuweisungen
    |--------------------------------------------------------------------------
    */
    Route::get('/settings/macro-assignments', [MacroAssignmentController::class, 'index'])->name('settings.macro-assignments.index');
    Route::post('/settings/macro-assignments/reorder', [MacroAssignmentController::class, 'reorder'])->name('settings.macro-assignments.reorder');
    Route::post('/settings/macro-assignments', [MacroAssignmentController::class, 'store'])->name('settings.macro-assignments.store');
    Route::patch('/settings/macro-assignments/{macroAssignment}', [MacroAssignmentController::class, 'update'])->name('settings.macro-assignments.update');
    Route::delete('/settings/macro-assignments/{macroAssignment}', [MacroAssignmentController::class, 'destroy'])->name('settings.macro-assignments.destroy');

    /*
    |--------------------------------------------------------------------------
    | Service-spezifische Makro-Overrides
    |--------------------------------------------------------------------------
    */
    Route::post('/services/{service}/macro-overrides', [ServiceMacroOverrideController::class, 'store'])->name('services.macro-overrides.store');
    Route::delete('/services/{service}/macro-overrides', [ServiceMacroOverrideController::class, 'destroy'])->name('services.macro-overrides.destroy');
    Route::post('/services/{service}/macro-assignments', [ServiceMacroOverrideController::class, 'storeAssignment'])->name('services.macro-assignments.store');
    Route::patch('/services/{service}/macro-assignments/{serviceMacroAssignment}', [ServiceMacroOverrideController::class, 'updateAssignment'])->name('services.macro-assignments.update');
    Route::delete('/services/{service}/macro-assignments/{serviceMacroAssignment}', [ServiceMacroOverrideController::class, 'destroyAssignment'])->name('services.macro-assignments.destroy');
});
