currentService->listCurrent(); return response()->json([ 'ok' => true, 'incidents' => IncidentResource::collection($data['incidents']), 'grades' => IncidentGradeResource::collection($data['grades']), ]); } public function history(IncidentListRequest $request): JsonResponse { $payload = $request->validated(); $incidents = $this->historyService->history($payload['school_year'] ?? null, $payload['semester'] ?? null); return response()->json([ 'ok' => true, 'incidents' => IncidentResource::collection($incidents), ]); } public function processed(IncidentListRequest $request): JsonResponse { $payload = $request->validated(); $incidents = $this->historyService->processed($payload['school_year'] ?? null, $payload['semester'] ?? null); return response()->json([ 'ok' => true, 'incidents' => IncidentResource::collection($incidents), ]); } public function analysis(IncidentListRequest $request): JsonResponse { $payload = $request->validated(); $students = $this->analysisService->analyze($payload['school_year'] ?? null, $payload['semester'] ?? null); return response()->json([ 'ok' => true, 'students' => IncidentAnalysisStudentResource::collection($students), 'school_year' => $payload['school_year'] ?? null, 'semester' => $payload['semester'] ?? null, ]); } public function studentsByGrade(int $gradeId): JsonResponse { $students = $this->currentService->studentsByGrade($gradeId); return response()->json([ 'ok' => true, 'students' => IncidentStudentOptionResource::collection($students), ]); } public function store(Request $request): JsonResponse { $guard = $this->authenticatedUserIdOrUnauthorized(); if ($guard instanceof JsonResponse) { return $guard; } $data = array_merge($request->query->all(), $request->all(), $request->json()->all()); $validator = Validator::make($data, [ 'student_id' => ['required', 'integer', 'min:1'], 'grade' => ['required', 'integer', 'min:1'], 'incident' => ['required', 'string', 'max:255'], 'description' => ['required', 'string', 'max:2000'], 'school_year' => ['nullable', 'string', 'max:20'], 'semester' => ['nullable', 'string', 'max:20'], ]); if ($validator->fails()) { return response()->json([ 'message' => 'Validation failed.', 'errors' => $validator->errors(), ], 422); } $payload = $validator->validated(); $result = $this->currentService->addIncident($payload, $guard); if (empty($result['ok'])) { return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to add incident.'], 422); } return response()->json([ 'ok' => true, 'created' => $result['created'], 'incident_id' => $result['incident_id'], ]); } public function updateState(IncidentStateRequest $request, int $incidentId): JsonResponse { $payload = $request->validated(); $updated = $this->currentService->updateState($incidentId, (string) $payload['incident_state']); return response()->json([ 'ok' => $updated, ]); } public function close(Request $request, int $incidentId): JsonResponse { $guard = $this->authenticatedUserIdOrUnauthorized(); if ($guard instanceof JsonResponse) { return $guard; } $data = array_merge($request->query->all(), $request->all(), $request->json()->all()); $validator = Validator::make($data, [ 'state_description' => ['required', 'string', 'max:2000'], 'action_taken' => ['nullable', 'string', 'max:2000'], ]); if ($validator->fails()) { return response()->json([ 'message' => 'Validation failed.', 'errors' => $validator->errors(), ], 422); } $payload = $validator->validated(); $result = $this->currentService->closeIncident( $incidentId, (string) $payload['state_description'], $payload['action_taken'] ?? null, $guard ); if (empty($result['ok'])) { return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to close incident.'], 422); } return response()->json([ 'ok' => true, 'incident_id' => $result['incident_id'], ]); } public function cancel(IncidentCancelRequest $request, int $incidentId): JsonResponse { $guard = $this->authenticatedUserIdOrUnauthorized(); if ($guard instanceof JsonResponse) { return $guard; } $payload = $request->validated(); $result = $this->currentService->cancelIncident( $incidentId, $payload['state_description'] ?? null, $payload['action_taken'] ?? null, $guard ); if (empty($result['ok'])) { return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to cancel incident.'], 422); } return response()->json([ 'ok' => true, 'incident_id' => $result['incident_id'], ]); } /** * @return int|JsonResponse */ private function authenticatedUserIdOrUnauthorized(): int|JsonResponse { $userId = (int) (auth()->id() ?? 0); if ($userId <= 0) { return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401); } return $userId; } }