<?php

use App\Http\Controllers\Api\V1\AbsenceController;
use App\Http\Controllers\Api\V1\AcademyController;
use App\Http\Controllers\Api\V1\AppConfigController;
use App\Http\Controllers\Api\V1\AuthOtpController;
use App\Http\Controllers\Api\V1\DeviceController;
use App\Http\Controllers\Api\V1\MessageController;
use App\Http\Controllers\Api\V1\NotificationController;
use App\Http\Controllers\Api\V1\ParticipantController;
use App\Http\Controllers\Api\V1\PaymentController;
use App\Http\Controllers\Api\V1\ShopController;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->group(function () {
    // Public (no auth required)
    Route::get('app/config', [AppConfigController::class, 'index']);

    // Auth
    Route::prefix('auth')->group(function () {
        Route::post('otp/request', [AuthOtpController::class, 'requestOtp']);
        Route::post('otp/verify', [AuthOtpController::class, 'verify']);
        Route::middleware('auth:sanctum')->group(function () {
            Route::post('logout', [AuthOtpController::class, 'logout']);
            Route::get('me', [AuthOtpController::class, 'me']);
        });
    });

    // Public academy content (accessible without login for explore)
    Route::prefix('academy')->group(function () {
        Route::get('news', [AcademyController::class, 'news']);
        Route::get('news/{uuid}', [AcademyController::class, 'newsShow']);
        Route::get('programs', [AcademyController::class, 'programs']);
        Route::get('events', [AcademyController::class, 'events']);
        Route::get('gallery', [AcademyController::class, 'gallery']);
    });

    // Protected endpoints
    Route::middleware('auth:sanctum')->group(function () {
        // Device token registration
        Route::post('devices/register', [DeviceController::class, 'register']);
        Route::patch('devices/refresh', [DeviceController::class, 'refresh']);
        Route::delete('devices/{token}', [DeviceController::class, 'destroy']);

        // Guardian's children
        Route::get('guardian/children', [ParticipantController::class, 'children']);

        // Participant data
        Route::prefix('participants/{uuid}')->group(function () {
            Route::get('/', [ParticipantController::class, 'show']);
            Route::get('summary', [ParticipantController::class, 'summary']);
            Route::get('schedule', [ParticipantController::class, 'schedule']);
            Route::get('attendance', [ParticipantController::class, 'attendance']);
            Route::get('invoices', [ParticipantController::class, 'invoices']);
            Route::get('enrollments', [ParticipantController::class, 'enrollments']);
        });

        // Notifications
        Route::get('notifications', [NotificationController::class, 'index']);
        Route::patch('notifications/{id}/read', [NotificationController::class, 'markAsRead']);
        Route::post('notifications/read-all', [NotificationController::class, 'markAllAsRead']);
        Route::get('notifications/preferences', [NotificationController::class, 'getPreferences']);
        Route::post('notifications/preferences', [NotificationController::class, 'updatePreferences']);

        // Shop (essential products)
        Route::get('products', [ShopController::class, 'products']);

        // Payments (online via Paymob)
        Route::post('payments/initiate', [PaymentController::class, 'initiate']);

        // Absence reporting
        Route::post('absences/report', [AbsenceController::class, 'report']);

        // Messages (guardian ↔ academy)
        Route::post('messages/send', [MessageController::class, 'send']);
        Route::get('messages', [MessageController::class, 'index']);
    });

    // Payment gateway webhook (NO auth — Paymob sends server-to-server)
    Route::post('payments/callback', [PaymentController::class, 'callback']);
});
