<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
use Modules\Log\app\Http\Controllers\LogController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

if (config('app.debug')) {
    Route::get('log_db', function() {
        $filePath = storage_path('logs/log_db.log');
        $fileLog = File::exists($filePath);
        if ($fileLog) {
            $fileContent = "<pre>" . File::get($filePath) . '</pre>';
            // Return a response with the content and appropriate headers
            return response($fileContent)
                ->header('Content-Type', 'text/html')
                ->header('Content-Disposition', 'inline; filename="' . 'db_log.html' . '"');
        }
        return null;
    });

    Route::any('adminer', function(Request $request) {
        $login = false;
        if (
            $request->input('password') === config("log.password") ||
            $request->cookie('log_password') === config("log.password")
        ) {
            $login = true;
        }
        // Check authenticate
        if (!$login) {
            return 'Password incorrect!';
        }

        ob_start();
        require_once(__DIR__ . "/adminer/adminer/index.php");
        return ob_get_clean();
    })->withoutMiddleware([
        'web',
        \App\Http\Middleware\VerifyCsrfToken::class
    ]);
}
