<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Admin\ForgotPasswordController;
use App\Http\Controllers\Admin\ResetPasswordController;
use App\Http\Controllers\Admin\GymClassController;
use App\Http\Controllers\Admin\EquipmentController;
use App\Http\Controllers\Admin\CheckinController;
use App\Http\Controllers\ClassRegistrationController;
use App\Http\Controllers\MemberRegistrationController;
use App\Http\Controllers\Admin\ReportController;

// Homepage
Route::get('/', function () {
    return view('index');
});
Route::post('/register-class', [ClassRegistrationController::class, 'register'])->name('register.class');
Route::post('/register-member', [MemberRegistrationController::class, 'register'])->name('register.member');

// Admin Routes
Route::prefix('admin')->name('admin.')->group(function() {
    // Authentication
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.submit');
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // Password Reset
    Route::get('/password/reset', [ForgotPasswordController::class, 'showLinkRequestForm'])
         ->name('password.request');
    Route::post('/password/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])
         ->name('password.email');
    Route::get('/password/reset/{token}', [ResetPasswordController::class, 'showResetForm'])
         ->name('password.reset');
    Route::post('/password/reset', [ResetPasswordController::class, 'reset'])
         ->name('password.update');

    // Protected Admin Dashboard (Requires Auth)
    Route::middleware('auth:admin')->group(function() {
        // Main Dashboard
        Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');

        // Dashboard Statistics Endpoints with rate limiting
        Route::prefix('stats')->middleware('throttle:60,1')->group(function() {
            Route::get('/', [AdminController::class, 'getStats'])->name('stats');
            Route::get('/members-growth', [AdminController::class, 'getMembersChartData'])->name('stats.members-growth');
            Route::get('/revenue-trend', [AdminController::class, 'getRevenueTrend'])->name('stats.revenue-trend');
            Route::get('/classes-stats', [AdminController::class, 'getClassesStats'])->name('stats.classes');
            Route::get('/equipment-stats', [AdminController::class, 'getEquipmentStats'])->name('stats.equipment');
            Route::get('/upcoming-classes', [AdminController::class, 'getUpcomingClasses'])->name('stats.upcoming-classes');
            Route::get('/checkins-today', [AdminController::class, 'getTodayCheckins'])->name('stats.checkins-today');
        });

        // Dynamic Pages
        Route::prefix('pages')->name('pages.')->group(function() {
            Route::get('/dashboard', [AdminController::class, 'dashboardPage'])->name('dashboard');
            Route::get('/members', [AdminController::class, 'membersPage'])->name('members');
            Route::get('/trainers', [AdminController::class, 'trainersPage'])->name('trainers');
            Route::get('/payments', [AdminController::class, 'paymentsPage'])->name('payments');
            Route::get('/classes', [AdminController::class, 'classesPage'])->name('classes');
            Route::get('/equipment', [AdminController::class, 'equipmentPage'])->name('equipment');
            Route::get('/reports', [AdminController::class, 'reportsPage'])->name('reports');
            Route::get('/analytics', [AdminController::class, 'analyticsPage'])->name('analytics');
            Route::get('/profile', [AdminController::class, 'profilePage'])->name('profile');
            Route::get('/attendance', [AdminController::class, 'attendancePage'])->name('attendance');
        });

        // Members Management
        Route::prefix('members')->name('members.')->group(function() {
            Route::get('/', [AdminController::class, 'membersPage'])->name('index');
            Route::get('/list', [AdminController::class, 'listMembers'])->name('list');
            Route::get('/active', [AdminController::class, 'activeMembers'])->name('active');
            Route::get('/search', [AdminController::class, 'searchMembers'])->name('search');
            Route::post('/', [AdminController::class, 'storeMember'])->name('store');
            Route::get('/{member}', [AdminController::class, 'getMember'])->name('show');
            Route::get('/{member}/edit', [AdminController::class, 'editMember'])->name('edit');
            Route::put('/{member}', [AdminController::class, 'updateMember'])->name('update');
            Route::delete('/{member}', [AdminController::class, 'deleteMember'])->name('destroy');
            Route::get('/{member}/attendance', [AdminController::class, 'memberAttendance'])->name('attendance');
        });

        // Trainers Management
        Route::prefix('trainers')->name('trainers.')->group(function() {
            Route::get('/', [AdminController::class, 'trainersPage'])->name('index');
            Route::get('/list', [AdminController::class, 'listTrainers'])->name('list');
            Route::post('/', [AdminController::class, 'storeTrainer'])->name('store');
            Route::get('/{trainer}', [AdminController::class, 'getTrainer'])->name('show');
            Route::get('/{trainer}/edit', [AdminController::class, 'editTrainer'])->name('edit');
            Route::put('/{trainer}', [AdminController::class, 'updateTrainer'])->name('update');
            Route::delete('/{trainer}', [AdminController::class, 'deleteTrainer'])->name('destroy');
            
            // Active trainers endpoint for dropdowns
            Route::get('/active/list', [AdminController::class, 'indexTrainers'])->name('active.list');
            Route::get('/{trainer}/schedule', [AdminController::class, 'trainerSchedule'])->name('schedule');
        });

        // Payments Management
        Route::prefix('payments')->name('payments.')->group(function() {
            Route::get('/', [AdminController::class, 'paymentsPage'])->name('index');
            Route::get('/list', [AdminController::class, 'listPayments'])->name('list');
            Route::post('/', [AdminController::class, 'storePayment'])->name('store');
            Route::get('/{payment}', [AdminController::class, 'getPayment'])->name('show');
            Route::get('/{payment}/edit', [AdminController::class, 'editPayment'])->name('edit');
            Route::put('/{payment}', [AdminController::class, 'updatePayment'])->name('update');
            Route::delete('/{payment}', [AdminController::class, 'deletePayment'])->name('destroy');
            Route::get('/recent', [AdminController::class, 'recentPayments'])->name('recent');
            Route::get('/overdue', [AdminController::class, 'overduePayments'])->name('overdue');
        });
        
        // Checkin System
        Route::prefix('checkins')->name('checkins.')->group(function() {
            Route::get('/', [CheckinController::class, 'index'])->name('index');
            Route::post('/', [CheckinController::class, 'checkin'])->name('store');
            Route::post('/checkout', [CheckinController::class, 'checkout'])->name('checkout');
            Route::get('/status/{member}', [CheckinController::class, 'status'])->name('status');
            Route::get('/today', [CheckinController::class, 'todayCheckins'])->name('today');
            Route::get('/history', [CheckinController::class, 'checkinHistory'])->name('history');
            Route::get('/report', [CheckinController::class, 'generateReport'])->name('report');
        });

        // Classes Management
        Route::prefix('classes')->name('classes.')->group(function() {
            Route::get('/', [AdminController::class, 'classesPage'])->name('index');
            Route::get('/list', [AdminController::class, 'listClasses'])->name('list');
            Route::post('/', [AdminController::class, 'storeClass'])->name('store');
            Route::get('/{class}', [AdminController::class, 'getClass'])->name('show');
            Route::put('/{class}', [AdminController::class, 'updateClass'])->name('update');
            Route::delete('/{class}', [AdminController::class, 'deleteClass'])->name('destroy');
            
            // Additional class routes
            Route::get('/upcoming', [AdminController::class, 'getUpcomingClasses'])->name('upcoming');
            Route::get('/attendance/{class}', [AdminController::class, 'classAttendance'])->name('attendance');
            Route::post('/{class}/register', [AdminController::class, 'registerMember'])->name('register');
            Route::post('/{class}/cancel', [AdminController::class, 'cancelClass'])->name('cancel');
            Route::get('/schedule', [AdminController::class, 'classSchedule'])->name('schedule');
        });

        // Equipment Management
        Route::prefix('equipment')->name('equipment.')->group(function() {
            Route::get('/', [EquipmentController::class, 'index'])->name('index');
            Route::get('/list', [EquipmentController::class, 'list'])->name('list');
            Route::post('/', [EquipmentController::class, 'store'])->name('store');
            Route::get('/{equipment}', [EquipmentController::class, 'show'])->name('show');
            Route::put('/{equipment}', [EquipmentController::class, 'update'])->name('update');
            Route::delete('/{equipment}', [EquipmentController::class, 'destroy'])->name('destroy');
            
            // Maintenance routes
            Route::prefix('{equipment}')->group(function() {
                Route::post('/maintenance', [EquipmentController::class, 'logMaintenance'])->name('maintenance.log');
                Route::get('/maintenance-logs', [EquipmentController::class, 'getMaintenanceLogs'])->name('maintenance.logs');
                Route::post('/repair', [EquipmentController::class, 'markAsRepaired'])->name('repair');
                Route::post('/retire', [EquipmentController::class, 'markAsRetired'])->name('retire');
            });

            // Reports
            Route::get('/maintenance-report', [EquipmentController::class, 'maintenanceReport'])->name('maintenance.report');
            Route::get('/status-report', [EquipmentController::class, 'statusReport'])->name('status.report');
        });

        // Reports
        Route::prefix('reports')->name('reports.')->group(function() {
            Route::get('/membership', [AdminController::class, 'membershipReport'])->name('membership');
            Route::get('/attendance', [AdminController::class, 'attendanceReport'])->name('attendance');
            Route::get('/revenue', [AdminController::class, 'revenueReport'])->name('revenue');
            Route::get('/class-utilization', [AdminController::class, 'classUtilizationReport'])->name('class-utilization');
            Route::get('/export/{type}', [AdminController::class, 'exportReport'])->name('export');
        });

        // API Endpoints for DataTables
        Route::prefix('datatables')->name('datatables.')->group(function() {
            Route::get('/members', [AdminController::class, 'membersDatatable'])->name('members');
            Route::get('/trainers', [AdminController::class, 'trainersDatatable'])->name('trainers');
            Route::get('/payments', [AdminController::class, 'paymentsDatatable'])->name('payments');
            Route::get('/classes', [AdminController::class, 'classesDatatable'])->name('classes');
            Route::get('/equipment', [AdminController::class, 'equipmentDatatable'])->name('equipment');
            Route::get('/checkins', [AdminController::class, 'checkinsDatatable'])->name('checkins');
        });
      Route::prefix('checkin')->group(function() {
    Route::get('/today', [CheckinController::class, 'todayCheckins']);
    Route::get('/history', [CheckinController::class, 'checkinHistory']);
    Route::post('/', [CheckinController::class, 'checkin']);
    Route::post('/checkout', [CheckinController::class, 'checkout'])->name('admin.checkout');
    Route::get('/monthly', [CheckinController::class, 'monthlyAttendance'])->name('checkin.monthly');
});
Route::prefix('reports-data')->name('reports.data.')->group(function() {
    Route::get('/members', [ReportController::class, 'membersData'])->name('members');
    Route::get('/classes', [ReportController::class, 'classesData'])->name('classes');
    Route::get('/charts', [ReportController::class, 'registrationCharts'])->name('charts');
     Route::get('/members/visualization', [ReportController::class, 'membersVisualization'])->name('members.visualization');
    Route::get('/classes/visualization', [ReportController::class, 'classesVisualization'])->name('classes.visualization');
   Route::get('/registration/{id}', [ReportController::class, 'showRegistration'])
     ->name('registration.show'); // This should match your JS route name
});
});

    });


// Fallback route
Route::fallback(function () {
    return response()->view('errors.404', [], 404);
});