<?php

use App\Http\Controllers\Coach\AssessmentBodyFatController;
use App\Http\Controllers\Coach\AssessmentController as CoachAssessmentController;
use App\Http\Controllers\Coach\AssessmentViewController;
use App\Http\Controllers\Coach\AthleteListController;
use App\Http\Controllers\Coach\BankAccountController;
use App\Http\Controllers\Coach\DocumentController;
use App\Http\Controllers\Coach\ExerciseLookupController;
use App\Http\Controllers\Coach\NutritionExtrasController;
use App\Http\Controllers\Coach\NutritionPlanController;
use App\Http\Controllers\Coach\PaymentController as CoachPaymentController;
use App\Http\Controllers\Coach\ProfileController as CoachProfileController;
use App\Http\Controllers\Coach\ProgressController as CoachProgressController;
use App\Http\Controllers\Coach\RequestController;
use App\Http\Controllers\Coach\ReviewModerationController;
use App\Http\Controllers\Coach\WorkoutPlanController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Coach Routes — تجمیع نهایی همه‌ی فازها (Phase 3, 5, 7, 8, 9, 10, 15)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'active.account', 'role:coach'])
    ->prefix('coach')
    ->name('coach.')
    ->group(function () {
        // Phase 3 — پروفایل و مدارک (حتی وقتی pending هم قابل ویرایش)
        Route::get('/profile', [CoachProfileController::class, 'edit'])->name('profile.edit');
        Route::put('/profile', [CoachProfileController::class, 'update'])->name('profile.update');
        Route::post('/profile/photo', [CoachProfileController::class, 'updatePhoto'])->name('profile.photo');
        Route::post('/documents', [DocumentController::class, 'store'])->name('documents.store');
        Route::delete('/documents/{document}', [DocumentController::class, 'destroy'])->name('documents.destroy');

        // Phase 5 — رابطه با ورزشکار
        Route::get('/requests', [RequestController::class, 'index'])->name('requests.index');
        Route::post('/requests/{coachRequest}/accept', [RequestController::class, 'accept'])->name('requests.accept');
        Route::post('/requests/{coachRequest}/reject', [RequestController::class, 'reject'])->name('requests.reject');
        Route::post('/requests/{coachRequest}/price', [RequestController::class, 'setPrice'])->name('requests.price');
        Route::get('/athletes', [AthleteListController::class, 'index'])->name('athletes.index');
        Route::get('/bank-account', [BankAccountController::class, 'edit'])->name('bank-account.edit');
        Route::put('/bank-account', [BankAccountController::class, 'update'])->name('bank-account.update');
        Route::put('/assessments/{assessment}/body-fat', [AssessmentBodyFatController::class, 'update'])->name('assessments.body-fat');

        // Phase 9 — ارزیابی (مربی ثبت می‌کند) + مشاهده + پیشرفت
        Route::get('/assessments/{assessment}', [AssessmentViewController::class, 'show'])->name('assessments.show');
        Route::get('/athletes/{athlete}/assessments/create', [CoachAssessmentController::class, 'create'])->name('athletes.assessments.create');
        Route::post('/athletes/{athlete}/assessments', [CoachAssessmentController::class, 'store'])->name('athletes.assessments.store');
        Route::get('/athletes/{athlete}/progress', [CoachProgressController::class, 'show'])->name('athletes.progress.show');

        // Phase 7 — بانک حرکات (فقط جستجو، بدون مدیریت) + Workout Builder
        Route::get('/exercises/search', [ExerciseLookupController::class, 'search'])->name('exercises.search');
        Route::get('/workout-plans', [WorkoutPlanController::class, 'index'])->name('workout-plans.index');
        Route::get('/athletes/{athlete}/workout-plans/create', [WorkoutPlanController::class, 'create'])->name('athletes.workout-plans.create');
        Route::post('/athletes/{athlete}/workout-plans', [WorkoutPlanController::class, 'store'])->name('athletes.workout-plans.store');
        Route::get('/workout-plans/{plan}', [WorkoutPlanController::class, 'show'])->name('workout-plans.show');
        Route::get('/workout-plans/{plan}/edit', [WorkoutPlanController::class, 'edit'])->name('workout-plans.edit');
        Route::put('/workout-plans/{plan}', [WorkoutPlanController::class, 'update'])->name('workout-plans.update');
        Route::delete('/workout-plans/{plan}', [WorkoutPlanController::class, 'destroy'])->name('workout-plans.destroy');
        Route::post('/workout-plans/{plan}/sessions', [\App\Http\Controllers\Coach\WorkoutSessionController::class, 'store'])->name('workout-plans.sessions.store');
        Route::post('/workout-plans/{plan}/sessions/reorder', [\App\Http\Controllers\Coach\WorkoutSessionController::class, 'reorder'])->name('workout-plans.sessions.reorder');

        // Phase 8 — Nutrition Builder
        Route::get('/nutrition-plans', [NutritionPlanController::class, 'index'])->name('nutrition-plans.index');
        Route::get('/athletes/{athlete}/nutrition-plans/create', [NutritionPlanController::class, 'create'])->name('athletes.nutrition-plans.create');
        Route::post('/athletes/{athlete}/nutrition-plans', [NutritionPlanController::class, 'store'])->name('athletes.nutrition-plans.store');
        Route::get('/nutrition-plans/{plan}', [NutritionPlanController::class, 'show'])->name('nutrition-plans.show');
        Route::get('/nutrition-plans/{plan}/edit', [NutritionPlanController::class, 'edit'])->name('nutrition-plans.edit');
        Route::put('/nutrition-plans/{plan}', [NutritionPlanController::class, 'update'])->name('nutrition-plans.update');
        Route::delete('/nutrition-plans/{plan}', [NutritionPlanController::class, 'destroy'])->name('nutrition-plans.destroy');
        Route::post('/nutrition-plans/{plan}/days', [\App\Http\Controllers\Coach\NutritionDayController::class, 'store'])->name('nutrition-plans.days.store');
        Route::post('/nutrition-plans/{plan}/forbidden-foods', [NutritionExtrasController::class, 'storeForbiddenFood'])->name('nutrition-plans.forbidden-foods.store');
        Route::post('/nutrition-plans/{plan}/supplements', [NutritionExtrasController::class, 'storeSupplement'])->name('nutrition-plans.supplements.store');
        Route::post('/nutrition-days/{day}/meals', [\App\Http\Controllers\Coach\NutritionMealController::class, 'store'])->name('nutrition-days.meals.store');
        Route::post('/nutrition-meals/{meal}/items', [\App\Http\Controllers\Coach\NutritionItemController::class, 'store'])->name('nutrition-meals.items.store');

        // Phase 10 — پرداخت‌ها
        Route::get('/payments', [CoachPaymentController::class, 'index'])->name('payments.index');
        Route::post('/payments/{payment}/approve', [CoachPaymentController::class, 'approve'])->name('payments.approve');
        Route::post('/payments/{payment}/reject', [CoachPaymentController::class, 'reject'])->name('payments.reject');

        // Phase 15 — Moderation نظرات (مرحله ۱)
        Route::get('/reviews', [ReviewModerationController::class, 'index'])->name('reviews.index');
        Route::post('/reviews/{review}/approve', [ReviewModerationController::class, 'approve'])->name('reviews.approve');
        Route::post('/reviews/{review}/reject', [ReviewModerationController::class, 'reject'])->name('reviews.reject');
    });

/*
|--------------------------------------------------------------------------
| مسیرهای زیرمجموعه‌ی مربی که روی id جلسه/وعده/حرکت متمرکزند (نه id مربی) —
| هنوز role:coach، اما بیرون از prefix('coach') بالا (Phase 7/8):
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'active.account', 'role:coach'])->group(function () {
    Route::put('/workout-sessions/{session}', [\App\Http\Controllers\Coach\WorkoutSessionController::class, 'update'])->name('coach.workout-sessions.update');
    Route::delete('/workout-sessions/{session}', [\App\Http\Controllers\Coach\WorkoutSessionController::class, 'destroy'])->name('coach.workout-sessions.destroy');
    Route::post('/workout-sessions/{session}/exercises', [\App\Http\Controllers\Coach\WorkoutExerciseController::class, 'store'])->name('coach.workout-sessions.exercises.store');
    Route::post('/workout-sessions/{session}/exercises/reorder', [\App\Http\Controllers\Coach\WorkoutExerciseController::class, 'reorder'])->name('coach.workout-sessions.exercises.reorder');
    Route::put('/workout-exercises/{exercise}', [\App\Http\Controllers\Coach\WorkoutExerciseController::class, 'update'])->name('coach.workout-exercises.update');
    Route::post('/workout-exercises/{exercise}/duplicate', [\App\Http\Controllers\Coach\WorkoutExerciseController::class, 'duplicate'])->name('coach.workout-exercises.duplicate');
    Route::delete('/workout-exercises/{exercise}', [\App\Http\Controllers\Coach\WorkoutExerciseController::class, 'destroy'])->name('coach.workout-exercises.destroy');

    Route::delete('/nutrition-days/{day}', [\App\Http\Controllers\Coach\NutritionDayController::class, 'destroy'])->name('coach.nutrition-days.destroy');
    Route::post('/nutrition-days/{day}/move-up', [\App\Http\Controllers\Coach\NutritionDayController::class, 'moveUp'])->name('coach.nutrition-days.move-up');
    Route::post('/nutrition-days/{day}/move-down', [\App\Http\Controllers\Coach\NutritionDayController::class, 'moveDown'])->name('coach.nutrition-days.move-down');
    Route::put('/nutrition-meals/{meal}', [\App\Http\Controllers\Coach\NutritionMealController::class, 'update'])->name('coach.nutrition-meals.update');
    Route::delete('/nutrition-meals/{meal}', [\App\Http\Controllers\Coach\NutritionMealController::class, 'destroy'])->name('coach.nutrition-meals.destroy');
    Route::put('/nutrition-items/{item}', [\App\Http\Controllers\Coach\NutritionItemController::class, 'update'])->name('coach.nutrition-items.update');
    Route::delete('/nutrition-items/{item}', [\App\Http\Controllers\Coach\NutritionItemController::class, 'destroy'])->name('coach.nutrition-items.destroy');
    Route::delete('/forbidden-foods/{food}', [NutritionExtrasController::class, 'destroyForbiddenFood'])->name('coach.forbidden-foods.destroy');
    Route::delete('/supplements/{supplement}', [NutritionExtrasController::class, 'destroySupplement'])->name('coach.supplements.destroy');
});

/*
|--------------------------------------------------------------------------
| مسیرهای مشترک (هم مربی هم Admin به آن‌ها نیاز دارند) — بیرون از role:coach:
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'active.account'])->group(function () {
    Route::get('/documents/{document}', [DocumentController::class, 'show'])->name('coach.documents.show');
    Route::get('/payments/{payment}/receipt', [\App\Http\Controllers\Coach\PaymentReceiptController::class, 'show'])->name('coach.payments.receipt');
});
