<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\DoctorController;
use App\Http\Controllers\Admin\PatientController;
use App\Http\Controllers\Admin\AppointmentController;
use App\Http\Controllers\Admin\SubscriptionController;
use App\Http\Controllers\Admin\AdvertisementController;
use App\Http\Controllers\Admin\NewsController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\MaintenanceController;

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

// تسجيل الدخول (بدون middleware)
Route::prefix('admin')->name('admin.')->group(function () {

    Route::get('login',  [AuthController::class, 'showLogin'])->name('login');
    Route::post('login', [AuthController::class, 'login'])->name('login.post');

    // باقي المسارات محمية
    Route::middleware('admin.auth')->group(function () {

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

        // الداشبورد
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard');

        // الأطباء
        Route::prefix('doctors')->name('doctors.')->group(function () {
            Route::get('/',              [DoctorController::class, 'index'])->name('index');
            Route::get('create',         [DoctorController::class, 'create'])->name('create');
            Route::post('/',             [DoctorController::class, 'store'])->name('store');
            Route::get('{id}',           [DoctorController::class, 'show'])->name('show');
            Route::patch('{id}/approve', [DoctorController::class, 'approve'])->name('approve');
            Route::patch('{id}/suspend', [DoctorController::class, 'suspend'])->name('suspend');
            Route::delete('{id}',        [DoctorController::class, 'destroy'])->name('destroy');
        });

        // المرضى
        Route::prefix('patients')->name('patients.')->group(function () {
            Route::get('/',       [PatientController::class, 'index'])->name('index');
            Route::patch('{user}/toggle-active', [PatientController::class, 'toggleActive'])->name('toggle-active');
            Route::get('{id}',    [PatientController::class, 'show'])->name('show');
            Route::delete('{id}', [PatientController::class, 'destroy'])->name('destroy');
        });

        // المواعيد
        Route::prefix('appointments')->name('appointments.')->group(function () {
            Route::get('/',    [AppointmentController::class, 'index'])->name('index');
            Route::get('{id}', [AppointmentController::class, 'show'])->name('show');
        });

        // الاشتراكات
        Route::prefix('subscriptions')->name('subscriptions.')->group(function () {
            Route::get('/',                        [SubscriptionController::class, 'index'])->name('index');
            Route::get('plans',                    [SubscriptionController::class, 'plans'])->name('plans');
            Route::post('plans',                   [SubscriptionController::class, 'storePlan'])->name('plans.store');
            Route::put('plans/{id}',               [SubscriptionController::class, 'updatePlan'])->name('plans.update');
            Route::delete('plans/{id}',            [SubscriptionController::class, 'destroyPlan'])->name('plans.destroy');
        });

        // الإعلانات
        Route::prefix('ads')->name('ads.')->group(function () {
            Route::get('/',           [AdvertisementController::class, 'index'])->name('index');
            Route::get('create',      [AdvertisementController::class, 'create'])->name('create');
            Route::post('/',          [AdvertisementController::class, 'store'])->name('store');
            Route::get('{id}/edit',   [AdvertisementController::class, 'edit'])->name('edit');
            Route::put('{id}',        [AdvertisementController::class, 'update'])->name('update');
            Route::delete('{id}',     [AdvertisementController::class, 'destroy'])->name('destroy');
            Route::patch('{id}/toggle',[AdvertisementController::class, 'toggle'])->name('toggle');
        });

        // الأخبار
        Route::prefix('news')->name('news.')->group(function () {
            Route::get('/',         [NewsController::class, 'index'])->name('index');
            Route::get('create',    [NewsController::class, 'create'])->name('create');
            Route::post('/',        [NewsController::class, 'store'])->name('store');
            Route::get('{id}/edit', [NewsController::class, 'edit'])->name('edit');
            Route::put('{id}',      [NewsController::class, 'update'])->name('update');
            Route::delete('{id}',   [NewsController::class, 'destroy'])->name('destroy');
            Route::patch('{id}/publish', [NewsController::class, 'publish'])->name('publish');
        });

        // الإعدادات
        Route::get('settings',  [SettingsController::class, 'index'])->name('settings');
        Route::put('settings',  [SettingsController::class, 'update'])->name('settings.update');

        // صيانة: كاش، جلسات، ربط التخزين
        Route::post('maintenance/clear-cache',    [MaintenanceController::class, 'clearCache'])->name('maintenance.clear-cache');
        Route::post('maintenance/clear-sessions', [MaintenanceController::class, 'clearSessions'])->name('maintenance.clear-sessions');
        Route::post('maintenance/storage-link',   [MaintenanceController::class, 'storageLink'])->name('maintenance.storage-link');
    });
});
