<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\PageController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RedirectController;
use App\Http\Controllers\ShortLinkController;
use App\Http\Controllers\StripeWebhookController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
})->name('home');

Route::post('/links', [ShortLinkController::class, 'store'])
    ->middleware('throttle:10,1')
    ->name('links.store');

Route::get('/u/{code}', [RedirectController::class, 'show'])
    ->middleware('throttle:120,1')
    ->name('short-links.redirect');

Route::get('/go/{token}', [RedirectController::class, 'interstitial'])
    ->name('interstitial.show');

Route::get('/go/{token}/continue', [RedirectController::class, 'continue'])
    ->name('interstitial.continue');

Route::post('/webhooks/stripe', StripeWebhookController::class)
    ->name('webhooks.stripe');

Route::get('/blog', [BlogController::class, 'index'])
    ->name('blog.index');

Route::get('/blog/{post:slug}', [BlogController::class, 'show'])
    ->name('blog.show');

Route::get('/{slug}', [PageController::class, 'showSlug'])
    ->whereIn('slug', ['about', 'faq', 'terms', 'privacy', 'contact'])
    ->name('pages.public');

Route::get('/dashboard', DashboardController::class)
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::get('/dashboard/analytics', [DashboardController::class, 'analytics'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard.analytics');

Route::get('/dashboard/subscription', [DashboardController::class, 'subscription'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard.subscription');

Route::post('/dashboard/subscription/free-plan/{plan}', [DashboardController::class, 'activateFreePlan'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard.subscription.free-plan');

Route::post('/dashboard/subscription/stripe-checkout/{plan}', [DashboardController::class, 'startStripeCheckout'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard.subscription.stripe-checkout');

Route::get('/dashboard/analytics/realtime', [DashboardController::class, 'realtime'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard.analytics.realtime');

Route::middleware('auth')->group(function () {
    Route::get('/links', [ShortLinkController::class, 'index'])->name('links.index');
    Route::get('/links/create', [ShortLinkController::class, 'create'])->name('links.create');
    Route::get('/links/{link}/qr-code', [ShortLinkController::class, 'showQrCode'])->name('links.qr-code.show');
    Route::get('/links/{link}/qr-code/download', [ShortLinkController::class, 'downloadQrCode'])->name('links.qr-code.download');

    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');
});

require __DIR__.'/auth.php';

Route::get('/p/{page:slug}', [PageController::class, 'show'])
    ->name('pages.show');
