<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});
if (config('app.env') === 'local') {
    Route::get('no-report-found', function () {
        $report = App\Models\ProjectReport::find(287);
        return new App\Mail\ReportNotFound($report);
    });
    Route::get('/mailable/{id}', function ($id) {
        if ($project_mailing = App\Models\ProjectMailing::find($id)) {
            return new App\Mail\ReportMailer($project_mailing);
        }
        return abort(404);
    })->name('mail');
}



Route::middleware([
    'auth:sanctum',
    config('jetstream.auth_session'),
    'verified',
])->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');
    Route::get('/projects/{projectName}/{currentTab?}',
        [\App\Http\Controllers\ProjectController::class, 'show'])->name('project.show');
    Route::get('/projects/{projectReport}/download',
        [\App\Http\Controllers\ProjectReportController::class, 'download'])->name('project.report.download');
    Route::get('/projects', [\App\Http\Controllers\ProjectController::class, 'index'])->name('project');
});

Route::get('download/{token}', function ($token) {
    $report = App\Models\ProjectReport::where('token', $token)->first();
    if ($report) {
        $path = $report->project->download_path.'/'.$report->path;
        $report->updateStatistics();
        return response()->download(Storage::path($path));
    }
    return abort(404);
})->name('project.report.download_with_token');
