<?php

use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\BatchController;
use App\Http\Controllers\CourseController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\StudentController;
use Illuminate\Support\Facades\Route;

// Guest routes
Route::middleware('guest')->group(function () {
    Route::get('/',     [LoginController::class, 'showLogin'])->name('login');
    Route::get('/login',[LoginController::class, 'showLogin']);
    Route::post('/login',[LoginController::class, 'login'])->name('login.post');
});

// Authenticated routes
Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Profile
    Route::patch('/profile',             [ProfileController::class, 'update'])->name('profile.update');
    Route::patch('/profile/password',    [ProfileController::class, 'updatePassword'])->name('profile.password');
    Route::patch('/profile/access-code', [ProfileController::class, 'updateAccessCode'])->name('profile.access-code');

    // Courses (API — JSON)
    Route::get('/api/courses',            [CourseController::class, 'index'])->name('courses.index');
    Route::post('/api/courses',           [CourseController::class, 'store'])->name('courses.store');
    Route::put('/api/courses/{course}',   [CourseController::class, 'update'])->name('courses.update');
    Route::delete('/api/courses/{course}',[CourseController::class, 'destroy'])->name('courses.destroy');

    // Batches (API — JSON)
    Route::get('/api/batches',            [BatchController::class, 'index'])->name('batches.index');
    Route::post('/api/batches',           [BatchController::class, 'store'])->name('batches.store');
    Route::put('/api/batches/{batch}',    [BatchController::class, 'update'])->name('batches.update');
    Route::delete('/api/batches/{batch}', [BatchController::class, 'destroy'])->name('batches.destroy');

    // Students (API — JSON)
    Route::get('/api/students',                [StudentController::class, 'index'])->name('students.index');
    Route::post('/api/students',               [StudentController::class, 'store'])->name('students.store');
    Route::put('/api/students/{student}',      [StudentController::class, 'update'])->name('students.update');
    Route::delete('/api/students/{student}',   [StudentController::class, 'destroy'])->name('students.destroy');
    Route::post('/api/students/bulk',          [StudentController::class, 'bulkStore'])->name('students.bulk');
    Route::get('/api/students/form-data',      [StudentController::class, 'formData'])->name('students.formData');

    // Attendance (API — JSON)
    Route::get('/api/attendance/batches',      [AttendanceController::class, 'batches'])->name('attendance.batches');
    Route::get('/api/attendance/students',     [AttendanceController::class, 'students'])->name('attendance.students');
    Route::get('/api/attendance/history',      [AttendanceController::class, 'history'])->name('attendance.history');
    Route::get('/api/attendance/student',      [AttendanceController::class, 'studentHistory'])->name('attendance.studentHistory');
    Route::get('/api/attendance/student/export',[AttendanceController::class, 'exportStudentCsv'])->name('attendance.studentExport');
    Route::get('/api/attendance/export',       [AttendanceController::class, 'exportCsv'])->name('attendance.export');
    Route::get('/api/attendance/transfers',    [AttendanceController::class, 'transfers'])->name('attendance.transfers');
    Route::post('/api/attendance/save',        [AttendanceController::class, 'save'])->name('attendance.save');
    Route::patch('/api/attendance/update',     [AttendanceController::class, 'updateRecord'])->name('attendance.update');
    Route::delete('/api/attendance/delete-day',[AttendanceController::class, 'deleteDay'])->name('attendance.deleteDay');

    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
});
