<?php

use App\Http\Controllers\DocumentPrintController;
use App\Http\Controllers\InventoryWarningExportController;
use App\Http\Controllers\Report\ReportExportController;
use App\Http\Controllers\Settlement\SettlementStatementExportController;
use App\Services\Install\DBErpInstallStatus;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Route;

$documentRoutes = Route::middleware(['auth', 'dberp.installed']);
$panelPath = trim((string) config('dberp.admin.panel_path', ''), '/');
if ($panelPath !== '') {
    $documentRoutes = $documentRoutes->prefix($panelPath);
}

$documentRoutes->group(function (): void {
    Route::get('/inventory-warnings/export', InventoryWarningExportController::class)
        ->name('dberp.inventory-warnings.export');

    Route::get('/reports/export/{report}', ReportExportController::class)
        ->name('dberp.reports.export');

    Route::get('/settlement-statements/{statement}/export', SettlementStatementExportController::class)
        ->name('dberp.settlement-statements.export');

    Route::get('/documents/{documentKey}/{record}/print', DocumentPrintController::class)
        ->name('dberp.documents.print');
});

if ($panelPath === '') {
    Route::get('admin', function (): RedirectResponse {
        if (! app(DBErpInstallStatus::class)->isInstalled()) {
            return redirect()->route('install.welcome');
        }

        return redirect()->route('filament.admin.auth.login');
    });
}
