<?php

use App\Http\Controllers\api\AttendanceController;
use App\Http\Controllers\api\ClassController;
use App\Http\Controllers\api\ScheduleController;
use App\Http\Controllers\api\UserController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\Api\AttendanceDailyController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/ping', function () {
    return response()->json(['message' => 'pong'], 200);
});

// auth routes
Route::post('/login', [UserController::class, 'login']);
Route::post('/register', [UserController::class, 'register']);

// global route for public access
Route::get('/classes', [ClassController::class, 'index']);

// get class by id, for public access because no data that is sensitive.
Route::get('/classes/{id}', [ClassController::class, 'show']);

// academic period fetch
Route::get('/academic-periods/active', [ScheduleController::class, 'fetchAcademicPeriodActive']);

// firebase notification test
Route::post('/fcm/send-token', [NotificationController::class, 'sendToToken']);
Route::post('/fcm/send-topic', [NotificationController::class, 'sendToTopic']);
Route::post('/fcm/send-topic-template', [NotificationController::class, 'sendTopicTemplateExample']);

// PUBLIC: attendance picture upload from Flask (no auth required for internal API)
Route::post('/attendance/upload-picture', [AttendanceDailyController::class, 'uploadPicture']);

// protected routes, need authentication
Route::middleware(['auth:sanctum'])->group(function () {
    Route::get('/attendance-daily/report', [AttendanceController::class, 'dailyAttendanceReport']);
    Route::post('/store-fcm', [UserController::class, 'storeFcmToken']);
    Route::post('/get-topic-from-class/{classId}', [UserController::class, 'getTopicFromClass']);
    // get schedule for a specific class
    Route::get('/classes/{id}/schedule', [ClassController::class, 'schedule']);
    Route::get('/classes/{id}/schedule/{dayindex}', [ClassController::class, 'scheduleByDay']);

    // update profile picture
    Route::post('/user/profile-picture', [UserController::class, 'updateProfilePhoto']);
});

// route login pakai Sanctum
Route::middleware(['auth:sanctum', 'role:student'])->group(function () {

    // get user
    Route::get('/user', function (Request $request) {
        return $request->user()->load('teacher', 'student');
    });

    // attendance record, permission, report, etc...
    Route::get('/testfeedback', [UserController::class, 'feedback']);
    Route::post('/qrattendance', [AttendanceController::class, 'qrAttendance']);
    Route::get('/attendance/report', [AttendanceController::class, 'report']);
    Route::get('/attendance/report/class', [AttendanceController::class, 'reportClass']);
    Route::get('/attendance/permission/report', [AttendanceController::class, 'permissionReport']);
    Route::post('/attendance/permission', [AttendanceController::class, 'createPermission']);

    // notification
    Route::get('/student/notifications', [UserController::class, 'getStudentNotifications']);
});

// role admin only
Route::middleware(['auth:sanctum', 'role:admin'])->group(function () {
    // announcement
    Route::post('/fcm/send-all', [NotificationController::class, 'sendToAll']);
});

Route::middleware(['auth:sanctum', 'role:teacher'])->group(function () {
    Route::get('/user', function (Request $request) {
        return $request->user()->load('teacher', 'student');
    });

    // schedule teacher personal
    Route::get('/teacher/schedules', [ScheduleController::class, 'scheduleTeacherPersonal']);
    Route::get('/teacher/schedules/day', [ScheduleController::class, 'scheduleTeacherPersonalByDay']);
    Route::get('/schedules/{classId}', [ScheduleController::class, 'scheduleByClass']);

    // get class information
    Route::get('/info/class/{id}', [ClassController::class, 'studentClassInformation']);

    // attendance record, permission...
    // attendance by schedule.
    Route::get('/teacher/attendance/class/{classId}/{date}', [AttendanceController::class, 'reportClassById']);
    // attendance daily
    Route::get('/teacher/attendance/daily/class', [AttendanceController::class, 'reportDailyAttendance']);

    // report disrepancy
    Route::post('/teacher/attendance/disrepancy/report', [AttendanceController::class, 'reportDisrepancyStudentAttendanceByHistoryId']);

    //* permission zone //
    Route::post('/teacher/attendance/permission/accept', [AttendanceController::class, 'permissionAccept']);
    Route::post('/teacher/attendance/permission/reject', [AttendanceController::class, 'permissionReject']);
    Route::get('/teacher/attendance/permission/report/{classId}', [AttendanceController::class, 'permissionReportByClass']);
    //* permission zone END //

    // announcement
    Route::post('/fcm/send-announcement', [ClassController::class, 'sendAnnouncementToClass']);

    // activity of teacher
    Route::get('/teacher/activity', [UserController::class, 'getTeacherActivity']);
    
    // copy url for public access qr (5 minutes valid)
    Route::get('/teacher/attendance/qr/generate/{idSchedule}', [AttendanceController::class, 'generateLinkQrAttendance']);
});

