<?php

use App\Http\Controllers\Admin\AiRequestController;
use App\Http\Controllers\Admin\AiStudioDashboardController;
use App\Http\Controllers\Admin\AnalyticsDashboardController;
use App\Http\Controllers\Admin\MonetizationDashboardController;
use App\Http\Controllers\Admin\PaymentReviewController;
use App\Http\Controllers\Admin\PaymentSettingsController;
use App\Http\Controllers\Admin\Auth\LoginController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\LandingScreenshotController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\SubscriptionPlanController;
use App\Http\Controllers\Admin\TransactionController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\UserSubscriptionAdminController;
use App\Http\Controllers\Web\Auth\ForgotPasswordController;
use App\Http\Controllers\Web\Auth\LoginController as WebLoginController;
use App\Http\Controllers\Web\Auth\RegisterController as WebRegisterController;
use App\Http\Controllers\Web\BillingController;
use App\Http\Controllers\Web\ChatController;
use App\Http\Controllers\Web\DashboardController as WebDashboardController;
use App\Http\Controllers\Web\FileController;
use App\Http\Controllers\Web\ImageController;
use App\Http\Controllers\Web\PricingController;
use App\Http\Controllers\Web\RewardsController;
use App\Http\Controllers\Web\SettingsController as WebSettingsController;
use App\Http\Controllers\Web\SiteController;
use App\Http\Controllers\Web\TextAnalysisController;
use App\Http\Controllers\Web\ToolsController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| الموقع العام — Landing + قانوني (usemadar.org)
|--------------------------------------------------------------------------
*/
Route::get('/', [SiteController::class, 'landing'])->name('site.landing');
Route::get('/privacy', [SiteController::class, 'privacy'])->name('site.privacy');
Route::get('/terms', [SiteController::class, 'terms'])->name('site.terms');
Route::get('/pricing', [PricingController::class, 'index'])->name('web.pricing');

/*
|--------------------------------------------------------------------------
| منصة مدار — مصادقة الويب (جلسة + Sanctum للـ API)
|--------------------------------------------------------------------------
*/
Route::prefix('')->name('web.')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('login', [WebLoginController::class, 'show'])->name('login');
        Route::post('login', [WebLoginController::class, 'login'])->middleware('throttle:10,1')->name('login.attempt');
        Route::get('register', [WebRegisterController::class, 'show'])->name('register');
        Route::post('register', [WebRegisterController::class, 'register'])->middleware('throttle:10,1')->name('register.attempt');
        Route::get('forgot-password', [ForgotPasswordController::class, 'show'])->name('password.request');
        Route::post('forgot-password', [ForgotPasswordController::class, 'send'])->middleware('throttle:5,1')->name('password.email');
    });

    Route::middleware(['auth', 'web', 'api.token'])->group(function () {
        Route::post('logout', [WebLoginController::class, 'logout'])->name('logout');

        Route::prefix('dashboard')->group(function () {
            Route::get('/', [WebDashboardController::class, 'index'])->name('dashboard');
            Route::get('/chat', [ChatController::class, 'index'])->name('chat');
            Route::get('/images', [ImageController::class, 'index'])->name('images');
            Route::get('/files', [FileController::class, 'index'])->name('files');
            Route::get('/tools', [ToolsController::class, 'index'])->name('tools');
            Route::get('/tools/text-analysis', [TextAnalysisController::class, 'index'])->name('tools.text-analysis');
            Route::get('/billing', [BillingController::class, 'index'])->name('billing');
            Route::post('/billing', [BillingController::class, 'store'])->name('billing.store');
            Route::get('/rewards', [RewardsController::class, 'index'])->name('rewards');
            Route::get('/settings', [WebSettingsController::class, 'index'])->name('settings');
            Route::patch('/settings', [WebSettingsController::class, 'update'])->name('settings.update');
        });
    });
});

/*
|--------------------------------------------------------------------------
| لوحة تحكم مدار (ويب — جلسات + صلاحية is_admin)
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->name('admin.')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
        Route::post('login', [LoginController::class, 'login'])
            ->middleware('throttle:10,1')
            ->name('login.attempt');
    });

    Route::middleware(['auth', 'admin'])->group(function () {
        Route::get('/', function () {
            return redirect()->route('admin.dashboard');
        });

        Route::post('logout', [LoginController::class, 'logout'])->name('logout');
        Route::get('dashboard', DashboardController::class)->name('dashboard');
        Route::get('monetization', MonetizationDashboardController::class)->name('monetization');
        Route::get('ai-studio', AiStudioDashboardController::class)->name('ai-studio');
        Route::get('analytics', AnalyticsDashboardController::class)->name('analytics');

        Route::get('users', [UserController::class, 'index'])->name('users.index');
        Route::get('users/{user}', [UserController::class, 'show'])->name('users.show');
        Route::patch('users/{user}/status', [UserController::class, 'updateStatus'])->name('users.status');
        Route::post('users/{user}/wallet', [UserController::class, 'adjustWallet'])->name('users.wallet');
        Route::post('users/{user}/points', [UserController::class, 'addPoints'])->name('users.points');
        Route::post('users/{user}/subscription', [UserController::class, 'assignSubscription'])->name('users.subscription.assign');

        Route::get('subscription-plans', [SubscriptionPlanController::class, 'index'])->name('subscription-plans.index');
        Route::get('subscription-plans/{subscription_plan}/edit', [SubscriptionPlanController::class, 'edit'])->name('subscription-plans.edit');
        Route::put('subscription-plans/{subscription_plan}', [SubscriptionPlanController::class, 'update'])->name('subscription-plans.update');

        Route::get('user-subscriptions', [UserSubscriptionAdminController::class, 'index'])->name('user-subscriptions.index');

        Route::get('transactions', [TransactionController::class, 'index'])->name('transactions.index');

        Route::prefix('payments')->name('payments.')->group(function () {
            Route::get('settings', [PaymentSettingsController::class, 'index'])->name('settings');
            Route::put('settings', [PaymentSettingsController::class, 'update'])->name('settings.update');
            Route::get('reviews', [PaymentReviewController::class, 'index'])->name('reviews');
            Route::get('reviews/{submission}', [PaymentReviewController::class, 'show'])->name('reviews.show');
            Route::get('reviews/{submission}/proof', [PaymentReviewController::class, 'proof'])->name('proof');
            Route::post('reviews/{submission}/approve', [PaymentReviewController::class, 'approve'])->name('reviews.approve');
            Route::post('reviews/{submission}/reject', [PaymentReviewController::class, 'reject'])->name('reviews.reject');
        });

        Route::get('ai-requests', [AiRequestController::class, 'index'])->name('ai-requests.index');
        Route::get('ai-requests/{ai_request}', [AiRequestController::class, 'show'])->name('ai-requests.show');

        Route::get('settings', [SettingsController::class, 'index'])->name('settings.index');
        Route::put('settings', [SettingsController::class, 'update'])->name('settings.update');
        Route::post('settings/prepare-deploy', [SettingsController::class, 'prepareDeploy'])->name('settings.prepare-deploy');
        Route::post('settings/screenshots', [LandingScreenshotController::class, 'store'])->name('settings.screenshots.store');
        Route::put('settings/screenshots/{landing_screenshot}', [LandingScreenshotController::class, 'update'])->name('settings.screenshots.update');
        Route::delete('settings/screenshots/{landing_screenshot}', [LandingScreenshotController::class, 'destroy'])->name('settings.screenshots.destroy');
        Route::post('settings/screenshots-reorder', [LandingScreenshotController::class, 'reorder'])->name('settings.screenshots.reorder');
    });
});
