<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ManifestController;
use App\Http\Controllers\NotionWebhookController;
use App\Http\Controllers\ReleaseEmailController;
use App\Http\Controllers\ReleasePreviewController;
use App\Http\Middleware\ManifestDevAuth;
use App\Http\Middleware\VerifyCsrfToken;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// ── The Manifest — public knowledge base (no login) ───────────────────────────
// Home: Vessel = Sprint, Container = Card.
Route::get('/', [ManifestController::class, 'index'])->name('manifest.index');
Route::get('/vessel/{vessel}', [ManifestController::class, 'vesselShow'])->name('manifest.vessel');

// Central de Novidades — versão resumida em catálogo, acessível pelo menu.
Route::get('/novidades', [ManifestController::class, 'updates'])->name('manifest.novidades');

// Tela da rota da documentação — apenas para a apresentação do mockup
Route::view('/rota', 'manifest.rota')->name('manifest.rota');
Route::get('/doc/{pageId}', [ManifestController::class, 'show'])
    ->where('pageId', '[0-9a-f-]{32,36}')
    ->name('manifest.show');

// ── Admin gate — protects only the /releases tooling below. The site has
// no more "dev" documentation area: technical docs live directly in Notion.
Route::get('/dev/login', [ManifestController::class, 'loginForm'])->name('manifest.dev.login');
Route::post('/dev/login', [ManifestController::class, 'login'])
    ->middleware('throttle:5,1') // brute-force protection
    ->name('manifest.dev.login.post');
Route::post('/dev/logout', [ManifestController::class, 'logout'])->name('manifest.dev.logout');

// ── Notion Webhook ────────────────────────────────────────────────────────────
// Notion POSTs here when a task status changes. CSRF is skipped (external caller).
Route::post('/webhooks/notion', [NotionWebhookController::class, 'handle'])
    ->name('webhooks.notion')
    ->withoutMiddleware([VerifyCsrfToken::class]);

// ── Release Email (admin-only: triggers paid Claude calls + writes to Notion) ─
// Interactive Blade UI: preview → regenerate → approve & push to Notion.
Route::middleware([ManifestDevAuth::class, 'throttle:30,1'])->group(function () {
    Route::get('/releases/{sprint}/generate-email', [ReleaseEmailController::class, 'generate'])
        ->name('releases.generate-email');

    Route::get('/releases/{sprint}/preview', [ReleasePreviewController::class, 'show'])
        ->name('releases.preview');

    Route::post('/releases/{sprint}/regenerate', [ReleasePreviewController::class, 'regenerate'])
        ->name('releases.regenerate');

    Route::post('/releases/{sprint}/approve', [ReleasePreviewController::class, 'approve'])
        ->name('releases.approve');
});