<?php

use App\Http\Controllers\Admin\UsageController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\ConversationController;
use App\Http\Controllers\FileController;
use App\Http\Controllers\Ops\OpsController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return Auth::check() ? redirect()->route('chat.index') : view('welcome');
});

Route::get('/dashboard', function () {
    return redirect()->route('chat.index');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    // Profil (Breeze)
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    // Chat / conversations
    Route::get('/chat', [ConversationController::class, 'index'])->name('chat.index');
    Route::post('/conversations', [ConversationController::class, 'store'])->name('conversations.store');
    Route::patch('/conversations/{conversation}', [ConversationController::class, 'update'])->name('conversations.update');
    Route::delete('/conversations/{conversation}', [ConversationController::class, 'destroy'])->name('conversations.destroy');
    Route::get('/conversations/{conversation}/export', [ConversationController::class, 'export'])->name('conversations.export');

    // Envoi d'un message (réponse en streaming SSE) — borné par les quotas
    Route::post('/conversations/{conversation}/stream', [ChatController::class, 'stream'])
        ->middleware('quota')
        ->name('chat.stream');

    // Fichiers
    Route::post('/files/upload', [FileController::class, 'upload'])->name('files.upload');
    Route::get('/files/{attachment}/download', [FileController::class, 'download'])->name('files.download');

    // Administration
    Route::get('/admin/usage', [UsageController::class, 'index'])
        ->middleware('admin')
        ->name('admin.usage');
});

// Endpoints d'exploitation (déploiement/cron sans SSH — hébergement mutualisé).
// Protégés par DEPLOY_TOKEN (X-Deploy-Token ou ?token=) ; 404 si non configuré.
// Exemptés de CSRF (voir bootstrap/app.php) : appelés par Jenkins / cron OVH.
Route::post('/ops/migrate', [OpsController::class, 'migrate'])->name('ops.migrate');
Route::post('/ops/extract', [OpsController::class, 'extract'])->name('ops.extract');
Route::match(['get', 'post'], '/ops/cron', [OpsController::class, 'cron'])->name('ops.cron');

require __DIR__.'/auth.php';
