<?php

use App\Http\Controllers\Api\V1\AdminAnalyticsController;
use App\Http\Controllers\Api\V1\AdminPricingController;
use App\Http\Controllers\Api\V1\AdminSettingsController;
use App\Http\Controllers\Api\V1\AdminSubscriptionController;
use App\Http\Controllers\Api\V1\AdminUserController;
use App\Http\Controllers\Api\V1\AdminWebhookController;
use App\Http\Controllers\Api\V1\EmojiApiController;
use App\Http\Controllers\Api\V1\ExtensionController;
use App\Http\Controllers\Api\V1\PricingController;
use App\Http\Controllers\Api\V1\SystemController;
use App\Http\Controllers\Api\V1\UserController;
use App\Http\Controllers\Api\V1\UserKeywordController;
use App\Http\Controllers\Billing\PakasirController;
use App\Http\Controllers\Billing\PayPalController;
use Illuminate\Support\Facades\Route;

Route::options('/v1/{any}', function () {
    $headers = [
        'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Api-Key, X-Admin-Token, X-Account-Id, X-Dewemoji-Frontend, X-Extension-Token',
        'Vary' => 'Origin',
    ];
    $origin = request()->headers->get('Origin', '');
    $allowedOrigins = config('dewemoji.cors.allowed_origins', []);
    if (is_array($allowedOrigins) && in_array($origin, $allowedOrigins, true)) {
        $headers['Access-Control-Allow-Origin'] = $origin;
    } elseif ((bool) config('dewemoji.cors.allow_all_public', true)) {
        $headers['Access-Control-Allow-Origin'] = '*';
    }

    return response('', 204, $headers);
})->where('any', '.*');

Route::post('/paypal/webhook', [PayPalController::class, 'webhook']);
Route::post('/webhooks/pakasir', [PakasirController::class, 'webhook']);

Route::prefix('v1')->group(function () {
    Route::get('/categories', [EmojiApiController::class, 'categories']);
    Route::get('/emojis', [EmojiApiController::class, 'emojis']);
    Route::get('/search', [EmojiApiController::class, 'search']);
    Route::get('/emoji', [EmojiApiController::class, 'emoji']);
    Route::get('/emoji/{slug}', [EmojiApiController::class, 'emoji']);
    Route::get('/pricing', [PricingController::class, 'index']);
    Route::post('/extension/verify', [ExtensionController::class, 'verify']);
    Route::get('/extension/search', [EmojiApiController::class, 'emojis']);

    Route::post('/user/register', [UserController::class, 'register']);
    Route::post('/user/login', [UserController::class, 'login']);
    Route::post('/user/logout', [UserController::class, 'logout']);
    Route::get('/user/apikeys', [UserController::class, 'listApiKeys']);
    Route::post('/user/apikeys', [UserController::class, 'createApiKey']);
    Route::delete('/user/apikeys/{key}', [UserController::class, 'revokeApiKey']);

    Route::get('/keywords', [UserKeywordController::class, 'index']);
    Route::post('/keywords', [UserKeywordController::class, 'store']);
    Route::put('/keywords/{id}', [UserKeywordController::class, 'update']);
    Route::put('/keywords/{id}/active', [UserKeywordController::class, 'toggleActive']);
    Route::delete('/keywords/{id}', [UserKeywordController::class, 'destroy']);
    Route::post('/keywords/import', [UserKeywordController::class, 'import']);
    Route::get('/keywords/export', [UserKeywordController::class, 'export']);

    Route::post('/admin/user/tier', [AdminUserController::class, 'setTier']);
    Route::get('/admin/users', [AdminUserController::class, 'index']);
    Route::get('/admin/user', [AdminUserController::class, 'show']);
    Route::get('/admin/pricing', [AdminPricingController::class, 'index']);
    Route::post('/admin/pricing', [AdminPricingController::class, 'update']);
    Route::get('/admin/pricing/changes', [AdminPricingController::class, 'changes']);
    Route::post('/admin/pricing/reset', [AdminPricingController::class, 'reset']);
    Route::get('/admin/settings', [AdminSettingsController::class, 'index']);
    Route::post('/admin/settings', [AdminSettingsController::class, 'update']);
    Route::get('/admin/subscriptions', [AdminSubscriptionController::class, 'index']);
    Route::post('/admin/subscription/grant', [AdminSubscriptionController::class, 'grant']);
    Route::post('/admin/subscription/revoke', [AdminSubscriptionController::class, 'revoke']);
    Route::get('/admin/analytics', [AdminAnalyticsController::class, 'overview']);
    Route::get('/admin/webhooks', [AdminWebhookController::class, 'index']);
    Route::get('/admin/webhooks/{id}', [AdminWebhookController::class, 'show']);
    Route::post('/admin/webhooks/{id}/replay', [AdminWebhookController::class, 'replay']);

    // Keep /v1 alias for backward compatibility, but use the same canonical webhook handler.
    Route::post('/paypal/webhook', [PayPalController::class, 'webhook']);

    Route::get('/health', [SystemController::class, 'health']);
    Route::get('/metrics-lite', [SystemController::class, 'metricsLite']);
    Route::get('/metrics', [SystemController::class, 'metrics']);
});
