<?php

use App\Http\Controllers\Dashboard\AdminDashboardController;
use App\Http\Controllers\Dashboard\AdminEmojiCatalogController;
use App\Http\Controllers\Dashboard\UserDashboardController;
use Illuminate\Support\Facades\Route;

Route::middleware('auth')->prefix('dashboard')->name('dashboard.')->group(function () {
    Route::middleware('verified')->group(function () {
        Route::get('/', [UserDashboardController::class, 'overview'])->name('overview');
        Route::get('/keywords', [UserDashboardController::class, 'keywords'])->name('keywords');
        Route::get('/keywords/search', [UserDashboardController::class, 'keywordSearch'])->name('keywords.search');
        Route::post('/keywords', [UserDashboardController::class, 'storeKeyword'])->name('keywords.store');
        Route::put('/keywords/{keyword}', [UserDashboardController::class, 'updateKeyword'])->name('keywords.update');
        Route::put('/keywords/{keyword}/active', [UserDashboardController::class, 'toggleKeywordActive'])->name('keywords.toggle_active');
        Route::delete('/keywords/{keyword}', [UserDashboardController::class, 'deleteKeyword'])->name('keywords.delete');
        Route::post('/keywords/import', [UserDashboardController::class, 'importKeywords'])->name('keywords.import');
        Route::get('/keywords/export', [UserDashboardController::class, 'exportKeywords'])->name('keywords.export');

        Route::get('/api-keys', [UserDashboardController::class, 'apiKeys'])->name('api-keys');
        Route::post('/api-keys', [UserDashboardController::class, 'createApiKey'])->name('api-keys.create');
        Route::post('/api-keys/{key}/revoke', [UserDashboardController::class, 'revokeApiKey'])->name('api-keys.revoke');

        Route::get('/billing', [UserDashboardController::class, 'billing'])->name('billing');
        Route::get('/preferences', [UserDashboardController::class, 'preferences'])->name('preferences');

        Route::middleware('can:admin')->prefix('admin')->name('admin.')->group(function () {
            Route::get('/users', [AdminDashboardController::class, 'users'])->name('users');
            Route::get('/users/{user}', [AdminDashboardController::class, 'userDetail'])->name('users.show');
            Route::post('/users/tier', [AdminDashboardController::class, 'updateUserTier'])->name('users.tier');
            Route::post('/users/create', [AdminDashboardController::class, 'createUser'])->name('users.create');
            Route::delete('/users/{user}', [AdminDashboardController::class, 'deleteUser'])->name('users.delete');

            Route::get('/subscriptions', [AdminDashboardController::class, 'subscriptions'])->name('subscriptions');
            Route::get('/subscriptions/{subscription}', [AdminDashboardController::class, 'subscriptionDetail'])->name('subscriptions.show');
            Route::post('/subscriptions/grant', [AdminDashboardController::class, 'grantSubscription'])->name('subscriptions.grant');
            Route::post('/subscriptions/revoke', [AdminDashboardController::class, 'revokeSubscription'])->name('subscriptions.revoke');

            Route::get('/pricing', [AdminDashboardController::class, 'pricing'])->name('pricing');
            Route::post('/pricing/update', [AdminDashboardController::class, 'updatePricing'])->name('pricing.update');
            Route::post('/pricing/reset', [AdminDashboardController::class, 'resetPricing'])->name('pricing.reset');
            Route::post('/pricing/snapshot', [AdminDashboardController::class, 'createPricingSnapshot'])->name('pricing.snapshot');
            Route::post('/pricing/paypal-sync', [AdminDashboardController::class, 'syncPaypalPlans'])->name('pricing.paypal_sync');

            Route::get('/catalog', [AdminEmojiCatalogController::class, 'index'])->name('catalog');
            Route::get('/catalog/create', [AdminEmojiCatalogController::class, 'create'])->name('catalog.create');
            Route::get('/catalog/{emojiId}/edit', [AdminEmojiCatalogController::class, 'edit'])->whereNumber('emojiId')->name('catalog.edit');
            Route::post('/catalog', [AdminEmojiCatalogController::class, 'store'])->name('catalog.store');
            Route::put('/catalog/{emojiId}', [AdminEmojiCatalogController::class, 'update'])->whereNumber('emojiId')->name('catalog.update');
            Route::delete('/catalog/{emojiId}', [AdminEmojiCatalogController::class, 'destroy'])->whereNumber('emojiId')->name('catalog.delete');
            Route::post('/catalog/import-json', [AdminEmojiCatalogController::class, 'importCurrentJson'])->name('catalog.import_json');
            Route::post('/catalog/publish', [AdminEmojiCatalogController::class, 'publish'])->name('catalog.publish');
            Route::post('/catalog/snapshots/activate', [AdminEmojiCatalogController::class, 'activateSnapshot'])->name('catalog.snapshot.activate');

            Route::get('/webhooks', [AdminDashboardController::class, 'webhooks'])->name('webhooks');
            Route::post('/webhooks/{id}/replay', [AdminDashboardController::class, 'replayWebhook'])->name('webhooks.replay');
            Route::post('/webhooks/replay-failed', [AdminDashboardController::class, 'replayFailedWebhooks'])->name('webhooks.replay_failed');
            Route::get('/webhooks/{event}', [AdminDashboardController::class, 'webhookDetail'])->name('webhooks.show');

            Route::get('/settings', [AdminDashboardController::class, 'settings'])->name('settings');
            Route::post('/settings/update', [AdminDashboardController::class, 'updateSettings'])->name('settings.update');

            Route::get('/export/{type}', [AdminDashboardController::class, 'exportCsv'])->name('export');
            Route::get('/audit-logs', [AdminDashboardController::class, 'auditLogs'])->name('audit_logs');
        });
    });
});
