<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use ErlandMuchasaj\LaravelFileUploader\FileUploader;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\FileController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('uplog', [FileController::class, 'upload']);

Route::post('/files', function (Request $request) {
    $max_size = 15360;

    $extensions = implode(',', FileUploader::documents());

    $user_id = $request->get('user_id');


    $request->validate([
        'file' => [
            'required',
            'file',
            '',
            'mimes:' . $extensions,
            'max:' . $max_size,
        ]
    ]);

    $file = $request->file('file');

    $response = FileUploader::store($file, ['user_id' => !empty($user_id) ? $user_id : 0]);
    return $response;
})->name('files.store');
