<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\BillingController;
use App\Http\Controllers\DeviceTokenController;
use App\Http\Controllers\FollowController;
use App\Http\Controllers\LegalDocumentController;
use App\Http\Controllers\MealImageAnalysisController;
use App\Http\Controllers\MealPostController;
use App\Http\Controllers\PostReviewsController;
use App\Http\Controllers\PublicUserProfileController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\StravaController;
use App\Http\Controllers\WorkoutSessionController;
use Illuminate\Support\Facades\Route;

// Auth
Route::prefix('auth')->group(function (): void {
    Route::post('register', [AuthController::class, 'register'])->middleware('throttle:auth');
    Route::post('login', [AuthController::class, 'login'])->middleware('throttle:auth');
    Route::post('mobile/register', [AuthController::class, 'mobileRegister'])->middleware('throttle:auth');
    Route::post('mobile/login', [AuthController::class, 'mobileLogin'])->middleware('throttle:auth');
    Route::post('forgot-password', [AuthController::class, 'forgotPassword'])
        ->middleware('throttle:6,1')
        ->name('password.email');
    Route::post('reset-password', [AuthController::class, 'resetPassword'])
        ->middleware('throttle:6,1')
        ->name('password.update');
    Route::get('email/verify/{id}/{hash}', [AuthController::class, 'verifyEmail'])
        ->middleware(['signed:relative', 'throttle:6,1'])
        ->name('verification.verify');
    Route::post('email/verification-notification', [AuthController::class, 'sendVerificationEmail'])
        ->middleware('throttle:6,1')
        ->name('verification.send');

    Route::middleware('auth:sanctum')->group(function (): void {
        Route::post('logout', [AuthController::class, 'logout']);
        Route::get('me', [AuthController::class, 'me']);
        Route::patch('me', [AuthController::class, 'update'])->middleware(['verified', 'throttle:content-write']);
        Route::patch('me/password', [AuthController::class, 'updatePassword'])->middleware('throttle:account-action');
        Route::delete('me', [AuthController::class, 'destroy'])->middleware('throttle:account-action');
    });

});

// Health
Route::get('/health', function () {
    return response()->json(['status' => 'ok']);
});

Route::prefix('legal-documents')->group(function (): void {
    Route::get('/', [LegalDocumentController::class, 'index'])->name('legal-documents.index');
    Route::get('{slug}', [LegalDocumentController::class, 'show'])->name('legal-documents.show');
});

Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void {
    Route::post('device-tokens', [DeviceTokenController::class, 'store'])->middleware('throttle:device-token');
    Route::delete('device-tokens', [DeviceTokenController::class, 'destroy'])->middleware('throttle:device-token');
});

// Billing
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->prefix('billing')->group(function (): void {
    Route::get('products', [BillingController::class, 'products'])->name('billing.products');
    Route::post('products/{creditProduct}/checkout', [BillingController::class, 'checkout'])->middleware('throttle:account-action')->name('billing.checkout');
    Route::post('portal', [BillingController::class, 'portal'])->middleware('throttle:account-action')->name('billing.portal');
});

// Workouts
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void {
    Route::get('workouts/calendar', [WorkoutSessionController::class, 'calendar'])->name('workouts.calendar');
    Route::get('workouts', [WorkoutSessionController::class, 'index'])->name('workouts.index');
    Route::post('workouts', [WorkoutSessionController::class, 'store'])->middleware('throttle:content-write')->name('workouts.store');
    Route::get('strava/status', [StravaController::class, 'status'])->name('strava.status');
    Route::get('strava/authorization-url', [StravaController::class, 'authorizationUrl'])->middleware('throttle:external-service')->name('strava.authorization-url');
    Route::post('strava/callback', [StravaController::class, 'callback'])->middleware('throttle:external-service')->name('strava.callback');
    Route::delete('strava/connection', [StravaController::class, 'disconnect'])->middleware('throttle:external-service')->name('strava.disconnect');
    Route::post('strava/sync', [StravaController::class, 'sync'])->middleware('throttle:external-service')->name('strava.sync');
});

// Meals
Route::middleware(['auth:sanctum', 'verified', 'not_suspended'])->group(function (): void {
    Route::get('users/{user}', [PublicUserProfileController::class, 'show'])->name('users.show');
    Route::post('users/{user}/follow', [FollowController::class, 'follow'])->middleware('throttle:engagement')->name('users.follow');
    Route::delete('users/{user}/follow', [FollowController::class, 'unfollow'])->middleware('throttle:engagement')->name('users.unfollow');
    Route::post('users/{user}/accept', [FollowController::class, 'accept'])->middleware('throttle:engagement')->name('users.accept');
    Route::post('users/{user}/reject', [FollowController::class, 'reject'])->middleware('throttle:engagement')->name('users.reject');
    Route::get('users/{user}/followers', [FollowController::class, 'followers'])->name('users.followers');
    Route::get('users/{user}/following', [FollowController::class, 'following'])->name('users.following');
    Route::post('users/{user}/reports', [ReportController::class, 'storeUser'])
        ->middleware('throttle:reports')
        ->name('users.reports.store');
    Route::get('me/meal-posts', [MealPostController::class, 'mine'])->name('meal-posts.mine');
    Route::get('meal-posts/stats', [MealPostController::class, 'stats']);
    Route::get('meal-posts/calendar', [MealPostController::class, 'calendar']);
    Route::post('meal-posts/analyze-image', [MealImageAnalysisController::class, 'store'])->middleware('throttle:ai-analysis');
    Route::post('meal-posts/{mealPost}/like', [MealPostController::class, 'like'])->middleware('throttle:engagement')->name('meal-posts.like');
    Route::delete('meal-posts/{mealPost}/like', [MealPostController::class, 'unlike'])->middleware('throttle:engagement')->name('meal-posts.unlike');
    Route::post('meal-posts/{mealPost}/reports', [ReportController::class, 'storeMealPost'])
        ->middleware('throttle:reports')
        ->name('meal-posts.reports.store');
    Route::get('meal-posts/{mealPost}/my-review', [PostReviewsController::class, 'mine'])->name('meal-posts.reviews.mine');
    Route::get('meal-posts/{mealPost}/reviews', [PostReviewsController::class, 'index'])->name('meal-posts.reviews.index');
    Route::post('meal-posts/{mealPost}/reviews', [PostReviewsController::class, 'store'])->middleware('throttle:content-write')->name('meal-posts.reviews.store');
    Route::get('post-reviews/{postReview}', [PostReviewsController::class, 'show'])->name('post-reviews.show');
    Route::put('post-reviews/{postReview}', [PostReviewsController::class, 'update'])->middleware('throttle:content-write')->name('post-reviews.update');
    Route::patch('post-reviews/{postReview}', [PostReviewsController::class, 'update'])->middleware('throttle:content-write')->name('post-reviews.patch');
    Route::delete('post-reviews/{postReview}', [PostReviewsController::class, 'destroy'])->middleware('throttle:content-write')->name('post-reviews.destroy');
    Route::post('post-reviews/{postReview}/reports', [ReportController::class, 'storePostReview'])
        ->middleware('throttle:reports')
        ->name('post-reviews.reports.store');
    Route::apiResource('meal-posts', MealPostController::class)
        ->except(['update'])
        ->middlewareFor(['store', 'destroy'], 'throttle:content-write');
    Route::put('meal-posts/{mealPost}', [MealPostController::class, 'update'])->middleware('throttle:content-write')->name('meal-posts.update');
    Route::patch('meal-posts/{mealPost}', [MealPostController::class, 'update'])->middleware('throttle:content-write')->name('meal-posts.patch');
});
