<?php

use Illuminate\Support\Facades\Route;
use App\Models\SubscriptionRequest;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\PlanController;
use App\Http\Controllers\Admin\PaymentMethodController;
use App\Http\Controllers\Admin\AdminSubscriptionRequestController;
use App\Http\Controllers\Admin\AdminSubscriptionController;
use App\Http\Controllers\Admin\AdminExpenseController;
use App\Http\Controllers\Admin\AdminProfileController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\SeoSettingController;

/*
|--------------------------------------------------------------------------
| Admin Routes
|--------------------------------------------------------------------------
*/

Route::prefix('admin')->group(function () {

    /*
    |--------------------------------------------------------------------------
    | Admin Authentication Routes
    |--------------------------------------------------------------------------
    */
    Route::get('/login', [AuthController::class, 'showLogin'])
        ->name('admin.login');

    Route::post('/login', [AuthController::class, 'login'])
        ->name('admin.login.submit');

    /*
    |--------------------------------------------------------------------------
    | Protected Admin Routes
    |--------------------------------------------------------------------------
    */
    Route::middleware(['admin.auth', 'user.activity'])->group(function () {

        /*
        |--------------------------------------------------------------------------
        | Dashboard
        |--------------------------------------------------------------------------
        */
        Route::get('/dashboard', [DashboardController::class, 'index'])
            ->name('admin.dashboard');

        Route::post('/logout', [AuthController::class, 'logout'])
            ->name('admin.logout');

        /*
        |--------------------------------------------------------------------------
        | Admin Profile
        |--------------------------------------------------------------------------
        */
        Route::get('/profile', [AdminProfileController::class, 'show'])
            ->name('admin.profile');

        Route::get('/profile/edit', [AdminProfileController::class, 'edit'])
            ->name('admin.profile.edit');

        Route::post('/profile/update', [AdminProfileController::class, 'updateProfile'])
            ->name('admin.profile.update');

        Route::post('/profile/password', [AdminProfileController::class, 'updatePassword'])
            ->name('admin.profile.password');

        /*
        |--------------------------------------------------------------------------
        | Live Notification Polling Route
        |--------------------------------------------------------------------------
        */
        Route::get('/notifications/pending-requests', function () {
            $count = SubscriptionRequest::where('status', 'pending')->count();

            return response()->json([
                'count' => $count,
            ]);
        })->name('admin.notifications.pending');

        /*
        |--------------------------------------------------------------------------
        | SEO Settings
        |--------------------------------------------------------------------------
        */
        Route::get('/seo-settings', [SeoSettingController::class, 'edit'])
            ->name('admin.seo.edit');

        Route::post('/seo-settings', [SeoSettingController::class, 'update'])
            ->name('admin.seo.update');

        /*
        |--------------------------------------------------------------------------
        | Plan Management
        |--------------------------------------------------------------------------
        */
        Route::resource('plans', PlanController::class);

        Route::patch('/plans/{plan}/toggle-status', [PlanController::class, 'toggleStatus'])
            ->name('plans.toggle-status');

        /*
        |--------------------------------------------------------------------------
        | Payment Methods
        |--------------------------------------------------------------------------
        */
        Route::resource('payment-methods', PaymentMethodController::class);

        Route::patch('/payment-methods/{payment_method}/toggle', [PaymentMethodController::class, 'toggleStatus'])
            ->name('payment-methods.toggle');

        /*
        |--------------------------------------------------------------------------
        | User Management
        |--------------------------------------------------------------------------
        */
        Route::get('/users', [UserController::class, 'index'])
            ->name('admin.users.index');

        Route::get('/users/{user}', [UserController::class, 'show'])
            ->name('admin.users.show');

        Route::get('/users/{user}/edit', [UserController::class, 'edit'])
            ->name('admin.users.edit');

        Route::put('/users/{user}', [UserController::class, 'update'])
            ->name('admin.users.update');

        Route::patch('/users/{user}/toggle', [UserController::class, 'toggleStatus'])
            ->name('admin.users.toggle');

        Route::patch('/users/{user}/toggle-status', [UserController::class, 'toggleStatus'])
            ->name('admin.users.toggle-status');

        Route::post('/users/bulk-action', [UserController::class, 'bulkAction'])
            ->name('admin.users.bulk');

        Route::post('/users/bulk-block', [UserController::class, 'bulkBlock'])
            ->name('admin.users.bulk-block');

        Route::post('/users/bulk-unblock', [UserController::class, 'bulkUnblock'])
            ->name('admin.users.bulk-unblock');

        /*
        |--------------------------------------------------------------------------
        | Subscription Requests
        |--------------------------------------------------------------------------
        */
        Route::prefix('subscription-requests')->name('admin.subscription-requests.')->group(function () {
            Route::get('/', [AdminSubscriptionRequestController::class, 'index'])->name('index');
            Route::get('/{subscriptionRequest}', [AdminSubscriptionRequestController::class, 'show'])->name('show');
            Route::post('/{subscriptionRequest}/approve', [AdminSubscriptionRequestController::class, 'approve'])->name('approve');
            Route::post('/{subscriptionRequest}/cancel', [AdminSubscriptionRequestController::class, 'cancel'])->name('cancel');
        });

        /*
        |--------------------------------------------------------------------------
        | Subscriptions
        |--------------------------------------------------------------------------
        */
        Route::prefix('subscriptions')->name('admin.subscriptions.')->group(function () {
            Route::get('/history', [AdminSubscriptionController::class, 'history'])->name('history');
            Route::get('/active', [AdminSubscriptionController::class, 'active'])->name('active');
            Route::get('/expired', [AdminSubscriptionController::class, 'expired'])->name('expired');
            Route::patch('/{subscription}/force-expire', [AdminSubscriptionController::class, 'forceExpire'])->name('force_expire');
        });

        /*
        |--------------------------------------------------------------------------
        | Admin Expenses
        |--------------------------------------------------------------------------
        */
        Route::resource('admin-expenses', AdminExpenseController::class);
    });
});