<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\DoctorController;
use App\Http\Controllers\Api\PatientController;
use App\Http\Controllers\Api\QueueController;
use App\Http\Controllers\Api\AppointmentController;
use App\Http\Controllers\Api\MedicalRecordController;
use App\Http\Controllers\Api\NewsController;
use App\Http\Controllers\Api\AdvertisementController;

/*
|--------------------------------------------------------------------------
| API Routes — Doctor Queue System
|--------------------------------------------------------------------------
*/

// ===== مسارات عامة (بدون تسجيل دخول) =====
Route::prefix('v1')->group(function () {

    // المصادقة
    Route::post('auth/register/patient',  [AuthController::class, 'registerPatient']);
    Route::post('auth/register/doctor',   [AuthController::class, 'registerDoctor']);
    Route::post('auth/login',             [AuthController::class, 'login']);
    Route::post('auth/forgot-password',   [AuthController::class, 'forgotPassword']);
    Route::post('auth/reset-password',    [AuthController::class, 'resetPassword']);

    // البحث العام
    Route::get('doctors',                 [DoctorController::class, 'index']);
    Route::get('doctors/{doctor}',        [DoctorController::class, 'show']);
    Route::get('doctors/{doctor}/reviews',[DoctorController::class, 'reviews']);
    Route::get('doctors/nearby',          [DoctorController::class, 'nearby']);

    // الأخبار والإعلانات
    Route::get('news',                    [NewsController::class, 'index']);
    Route::get('news/{slug}',             [NewsController::class, 'show']);
    Route::get('advertisements',          [AdvertisementController::class, 'index']);
    Route::post('advertisements/{ad}/click', [AdvertisementController::class, 'trackClick']);

    // ===== مسارات المريض (يحتاج تسجيل دخول) =====
    Route::middleware('auth:sanctum')->prefix('patient')->group(function () {

        Route::get('profile',                 [PatientController::class, 'profile']);
        Route::put('profile',                 [PatientController::class, 'updateProfile']);

        // الطابور
        Route::get('queue/current',           [QueueController::class, 'myCurrentQueue']);
        Route::post('queue/join/{doctor}',    [QueueController::class, 'joinQueue']);
        Route::delete('queue/leave',          [QueueController::class, 'leaveQueue']);

        // المواعيد
        Route::get('appointments',            [AppointmentController::class, 'patientAppointments']);
        Route::post('appointments',           [AppointmentController::class, 'book']);
        Route::delete('appointments/{id}',    [AppointmentController::class, 'cancel']);

        // السجل الطبي
        Route::get('medical-records',         [MedicalRecordController::class, 'patientRecords']);
        Route::get('medical-records/{id}',    [MedicalRecordController::class, 'show']);

        // التقييمات
        Route::post('doctors/{doctor}/review',[DoctorController::class, 'submitReview']);
    });

    // ===== مسارات الطبيب (يحتاج تسجيل دخول) =====
    Route::middleware(['auth:sanctum', 'role:doctor'])->prefix('doctor')->group(function () {

        Route::get('profile',                 [DoctorController::class, 'myProfile']);
        Route::put('profile',                 [DoctorController::class, 'updateProfile']);

        // إدارة الطابور
        Route::get('queue/today',             [QueueController::class, 'todayQueue']);
        Route::post('queue/call-next',        [QueueController::class, 'callNext']);
        Route::patch('queue/{id}/status',     [QueueController::class, 'updateStatus']);

        // المواعيد
        Route::get('appointments',            [AppointmentController::class, 'doctorAppointments']);
        Route::patch('appointments/{id}',     [AppointmentController::class, 'updateStatus']);

        // السجلات الطبية
        Route::get('patients/{patient}/records',  [MedicalRecordController::class, 'patientHistory']);
        Route::post('patients/{patient}/records', [MedicalRecordController::class, 'create']);
        Route::put('records/{id}',                [MedicalRecordController::class, 'update']);

        // رفع الوسائط
        Route::post('media/upload',           [DoctorController::class, 'uploadMedia']);
        Route::delete('media/{id}',           [DoctorController::class, 'deleteMedia']);

        // الاشتراكات
        Route::get('subscription',            [DoctorController::class, 'subscription']);
        Route::post('subscription/upgrade',   [DoctorController::class, 'upgradeSubscription']);
    });

});
