<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{AuthController,DashboardController,ProjectsController,TicketsController,FilesController,BillingController,ClubController,AdminController};
use App\Models\Project;
use App\Services\Access;
Route::middleware('guest')->group(function(){
 Route::view('/login','auth.login')->name('login'); Route::post('/login',[AuthController::class,'login'])->middleware('throttle:15,1');
 Route::view('/register','auth.register');Route::post('/register',[AuthController::class,'register'])->middleware('throttle:10,1');
 Route::post('/otp',[AuthController::class,'sendOtp'])->middleware('throttle:10,1');Route::view('/verify','auth.verify');Route::post('/verify',[AuthController::class,'verify'])->middleware('throttle:15,1');
});
Route::get('/payments/callback',[BillingController::class,'callback'])->middleware('throttle:30,1');
Route::get('/review-link/{token}',[ClubController::class,'link'])->middleware('throttle:30,1');
Route::get('/review-public/{review}',[ClubController::class,'review']);Route::post('/review-public/{review}',[ClubController::class,'submit'])->middleware('throttle:5,1');
Route::middleware(['auth','active'])->group(function(){
 Route::get('/',[DashboardController::class,'index'])->name('dashboard'); Route::post('/logout',[AuthController::class,'logout']);
 Route::get('/account',[DashboardController::class,'account']);Route::post('/account',[DashboardController::class,'updateAccount']);Route::post('/account/sessions',[DashboardController::class,'revokeSession']);
 Route::get('/notifications',[DashboardController::class,'notifications']);Route::post('/notifications/{notification}',[DashboardController::class,'readNotification']);
 Route::get('/periods/{period}',function(\App\Models\ServicePeriod $period){$project=Project::findOrFail($period->project_id);Access::project(auth()->user(),$project);abort_unless($period->order_id&&$period->starts_at->lte(now()),403);$addendum=\App\Models\Addendum::where('service_period_id',$period->id)->first();return view('projects.period',compact('project','period','addendum'));});
 Route::get('/projects',[ProjectsController::class,'index']);Route::post('/projects',[ProjectsController::class,'create'])->middleware('throttle:10,1');
 Route::get('/projects/{project}',[ProjectsController::class,'show']);
 Route::get('/projects/{project}/intake',function(Project $project){Access::project(auth()->user(),$project);return view('projects.intake',compact('project'));});
 Route::get('/projects/{project}/history',[ProjectsController::class,'history']);
 foreach(['action'=>'action','assign'=>'assign','credentials'=>'credentials','credentials/reveal'=>'reveal','tasks'=>'task','corrections'=>'correction','approve-form'=>'approveForm','activate'=>'activate','gift'=>'gift','dates'=>'dates'] as $path=>$action)Route::post('/projects/{project}/'.$path,[ProjectsController::class,$action])->middleware('throttle:20,1');
 Route::post('/projects/{project}/tasks/{task}/complete',[ProjectsController::class,'completeTask']);
 Route::get('/tickets',[TicketsController::class,'index']);Route::post('/tickets',[TicketsController::class,'create'])->middleware('throttle:10,1');Route::get('/tickets/{ticket}',[TicketsController::class,'show']);Route::post('/tickets/{ticket}/reply',[TicketsController::class,'reply'])->middleware('throttle:20,1');Route::post('/tickets/{ticket}/close',[TicketsController::class,'close']);
 Route::post('/files',[FilesController::class,'upload'])->middleware('throttle:30,1');Route::get('/files/{attachment}',[FilesController::class,'download']);
 Route::get('/plans',[BillingController::class,'plans']);Route::get('/orders',[BillingController::class,'index']);Route::post('/orders',[BillingController::class,'order'])->middleware('throttle:10,1');Route::get('/orders/{order}',[BillingController::class,'show']);
 foreach(['pay','reconcile','logo','resolve'] as $action)Route::post('/orders/{order}/'.$action,[BillingController::class,$action])->middleware('throttle:10,1');
 Route::get('/club',[ClubController::class,'index']);Route::get('/reviews/{review}',[ClubController::class,'review']);Route::post('/missions/{mission}',[ClubController::class,'mission'])->middleware('throttle:5,1');
 Route::get('/admin/lookup',[AdminController::class,'lookup']);Route::get('/admin/export',[AdminController::class,'export']);Route::post('/admin/guide',[AdminController::class,'guide']);Route::post('/admin/settings',[AdminController::class,'settings']);
 Route::post('/admin/sms/{template}/test',[AdminController::class,'testSms'])->middleware('throttle:2,1');Route::post('/admin/outbox/{box}/action',[AdminController::class,'outboxAction']);Route::post('/admin/points/adjust',[AdminController::class,'adjustPoints']);
 Route::get('/admin/{section?}',[AdminController::class,'index']);Route::post('/admin/{section}/{id}/moderate',[AdminController::class,'moderate']);Route::post('/admin/{section}/{id?}',[AdminController::class,'save']);
});
