<?php

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\DeviceTokenController;
use App\Http\Controllers\Api\HealthController;
use App\Http\Controllers\Api\MobileConfigController;
use App\Http\Controllers\Api\MobileLogController;
use App\Http\Controllers\Api\OtpController;
use Illuminate\Support\Facades\Route;

// Health check (no auth, no versioning)
Route::get('/health', [HealthController::class, 'check']);

// Fallback/Legacy routes for mobile apps not yet updated to v1
Route::post('/login', [AuthController::class, 'login'])->middleware('throttle:10,1');
Route::get('/app-config', [AuthController::class, 'getAppConfig'])->middleware('throttle:60,1');

Route::prefix('v1')->middleware('mobile-guard')->group(function () {
    // Public endpoints
    Route::post('/login', [AuthController::class, 'login'])->middleware('throttle:10,1');
    Route::post('/register', [AuthController::class, 'register'])->middleware('throttle:5,1');
    Route::post('/forgot-password', [AuthController::class, 'forgotPassword'])->middleware('throttle:5,1');
    Route::get('/app-config', [AuthController::class, 'getAppConfig']);
    Route::get('/mobile/sync', [MobileConfigController::class, 'sync']);
    Route::post('/mobile/log', [MobileLogController::class, 'store'])->middleware('throttle:60,1');

    // OTP
    Route::post('/otp/send', [OtpController::class, 'send'])->middleware('throttle:5,1');
    Route::post('/otp/verify', [OtpController::class, 'verify'])->middleware('throttle:5,1');

    // Authenticated endpoints
    Route::middleware('auth:sanctum')->group(function () {
        Route::get('/user', [AuthController::class, 'user']);
        Route::post('/logout', [AuthController::class, 'logout']);
        Route::post('/profile/update', [AuthController::class, 'updateProfile']);
        Route::post('/profile/avatar', [AuthController::class, 'updateAvatar']);
        Route::post('/profile/password', [AuthController::class, 'updatePassword']);
        Route::delete('/profile/delete', [AuthController::class, 'deleteAccount']);
        Route::get('/dashboard', [AuthController::class, 'getDashboardData']);

        // Device tokens for push notifications
        Route::post('/devices/register', [DeviceTokenController::class, 'register']);
        Route::delete('/devices/unregister', [DeviceTokenController::class, 'unregister']);
    });
});
