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2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
import {
  Controller,
  Get,
  Post,
  Put,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseIntPipe,
  Headers,
  UploadedFile,
  UseInterceptors,
  Res,
  StreamableFile,
  ForbiddenException,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
  ApiConsumes,
  ApiBody,
  ApiHeader,
} from '@nestjs/swagger';
import type { Response } from 'express';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles } from '../../../common/decorators/roles.decorator';
import { CurrentUser } from '../../../common/decorators/current-user.decorator';
import { RoleName } from '../../auth/entities/role.entity';
import {
  AttendanceService,
  AttendanceExcelService,
  AttendanceAiService,
  StudentFaceReferenceService,
} from '../services';
import {
  CreateSessionDto,
  UpdateSessionDto,
  MarkAttendanceDto,
  BatchAttendanceDto,
  AttendanceQueryDto,
  UpdateRecordDto,
  ImportAttendanceDto,
} from '../dto';
import { AttendanceSession, AttendanceRecord } from '../entities';

@ApiTags('πŸ“‹ Attendance')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard, RolesGuard)
@Controller('attendance')
@ApiHeader({
  name: 'Accept-Language',
  description: 'Language preference (en, ar)',
  required: false,
  example: 'en',
})
export class AttendanceController {
  constructor(
    private readonly attendanceService: AttendanceService,
    private readonly excelService: AttendanceExcelService,
    private readonly aiService: AttendanceAiService,
    private readonly studentFaceReferenceService: StudentFaceReferenceService,
  ) {}

  // ==================== SESSION ENDPOINTS ====================

  @Post('sessions')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Create a new attendance session',
    description: `
## Create Attendance Session

Creates a new attendance session for a course section. When a session is created, attendance records are automatically initialized for all enrolled students with a default status of "absent".

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Required Fields
- \`sectionId\`: Course section ID
- \`sessionDate\`: Date of the session (YYYY-MM-DD)
- \`sessionType\`: Type of session (lecture, lab, tutorial, exam, other)

### Optional Fields
- \`startTime\` / \`endTime\`: Session time window
- \`location\`: Room/location
- \`notes\`: Additional notes

### Behavior
- Session is created with status \`scheduled\`
- All enrolled students get an initial \`absent\` record
- The instructor ID is automatically set from the authenticated user
    `,
  })
  @ApiResponse({
    status: 201,
    description: 'Session created successfully',
    type: AttendanceSession,
  })
  @ApiResponse({ status: 400, description: 'Invalid input data' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden - insufficient role' })
  async createSession(
    @Body() dto: CreateSessionDto,
    @CurrentUser() user: any,
  ): Promise<AttendanceSession> {
    return this.attendanceService.createSession(dto, user.userId);
  }

  @Get('sessions')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'List attendance sessions with filters',
    description: `
## List Attendance Sessions

Retrieves a paginated list of attendance sessions with optional filters.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Filtering Options
- \`sectionId\`: Filter by course section
- \`startDate\` / \`endDate\`: Filter by date range
- \`status\`: Filter by session status (scheduled, in_progress, completed, cancelled)
- \`sessionType\`: Filter by type (lecture, lab, tutorial, exam, other)

### Pagination
- \`page\`: Page number (default: 1)
- \`limit\`: Items per page (default: 20, max: 100)

### Response
Returns paginated list with \`data\` array and \`total\` count.
    `,
  })
  @ApiResponse({
    status: 200,
    description: 'List of sessions with pagination',
  })
  async findAllSessions(@Query() query: AttendanceQueryDto) {
    return this.attendanceService.findAllSessions(query);
  }

  @Get('sessions/:id')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get attendance session by ID with records',
    description: `
## Get Session Details

Retrieves detailed information about a specific attendance session including all attendance records.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
- Session information (date, type, status, location)
- Section details with course info
- Instructor details
- All attendance records with student info
- Attendance statistics (present/absent/late counts)
    `,
  })
  @ApiParam({ name: 'id', description: 'Session ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Session details with attendance records',
    type: AttendanceSession,
  })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async findSessionById(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<AttendanceSession> {
    return this.attendanceService.findSessionById(id);
  }

  @Put('sessions/:id')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Update an attendance session',
    description: `
## Update Attendance Session

Updates an existing attendance session. Cannot update closed/completed sessions.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Updatable Fields
- \`sessionDate\`: Date of the session
- \`sessionType\`: Type of session
- \`startTime\` / \`endTime\`: Session time window
- \`location\`: Room/location
- \`notes\`: Additional notes
- \`status\`: Session status

### Restrictions
- Cannot update sessions with status \`completed\` or \`cancelled\`
    `,
  })
  @ApiParam({ name: 'id', description: 'Session ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Session updated successfully',
    type: AttendanceSession,
  })
  @ApiResponse({ status: 400, description: 'Session is closed' })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async updateSession(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateSessionDto,
  ): Promise<AttendanceSession> {
    return this.attendanceService.updateSession(id, dto);
  }

  @Delete('sessions/:id')
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Delete an attendance session',
    description: `
## Delete Attendance Session

Deletes an attendance session and all associated records.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, ADMIN

### Notes
- This is a hard delete operation
- All attendance records for this session will be deleted
- TAs cannot delete sessions (only Instructors and Admins)
    `,
  })
  @ApiParam({ name: 'id', description: 'Session ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Session deleted successfully' })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async deleteSession(@Param('id', ParseIntPipe) id: number): Promise<void> {
    return this.attendanceService.deleteSession(id);
  }

  @Patch('sessions/:id/close')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Close an attendance session',
    description: `
## Close Attendance Session

Closes an attendance session, preventing further modifications to attendance records.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Behavior
- Sets session status to \`completed\`
- Calculates and updates final attendance statistics
- Prevents any further changes to attendance records
- Cannot close an already closed session
    `,
  })
  @ApiParam({ name: 'id', description: 'Session ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Session closed successfully',
    type: AttendanceSession,
  })
  @ApiResponse({ status: 400, description: 'Session already closed' })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async closeSession(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<AttendanceSession> {
    return this.attendanceService.closeSession(id);
  }

  @Get('sessions/:id/records')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get all attendance records for a session',
    description: `
## Get Session Attendance Records

Retrieves all attendance records for a specific session with student details.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
For each record:
- Student information (ID, name, email)
- Attendance status (present, absent, late, excused)
- Check-in time (if present)
- How it was marked (manual, ai, self)
- Notes
    `,
  })
  @ApiParam({ name: 'id', description: 'Session ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'List of attendance records',
    type: [AttendanceRecord],
  })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async getSessionRecords(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<AttendanceRecord[]> {
    return this.attendanceService.getSessionRecords(id);
  }

  // ==================== RECORD ENDPOINTS ====================

  @Post('records')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Mark attendance for a single student',
    description: `
## Mark Single Student Attendance

Marks attendance for a single student in a session.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Required Fields
- \`sessionId\`: Attendance session ID
- \`userId\`: Student user ID
- \`attendanceStatus\`: Status (present, absent, late, excused)

### Optional Fields
- \`notes\`: Additional notes about the attendance
- \`checkInTime\`: Manual check-in time override

### Behavior
- Updates existing record if student already has one for this session
- Cannot mark attendance for closed sessions
    `,
  })
  @ApiResponse({
    status: 201,
    description: 'Attendance marked successfully',
    type: AttendanceRecord,
  })
  @ApiResponse({ status: 400, description: 'Session is closed' })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async markAttendance(
    @Body() dto: MarkAttendanceDto,
  ): Promise<AttendanceRecord> {
    return this.attendanceService.markAttendance(dto);
  }

  @Post('records/batch')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Mark attendance for multiple students at once',
    description: `
## Batch Mark Attendance

Marks attendance for multiple students in a single request. Useful for quickly marking entire class.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Required Fields
- \`sessionId\`: Attendance session ID
- \`records\`: Array of { userId, attendanceStatus, notes? }

### Behavior
- Updates existing records or creates new ones
- All operations in a single transaction
- Cannot mark attendance for closed sessions
- Partial success not allowed - all or nothing
    `,
  })
  @ApiResponse({
    status: 201,
    description: 'Batch attendance marked successfully',
    type: [AttendanceRecord],
  })
  @ApiResponse({ status: 400, description: 'Session is closed' })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async markBatchAttendance(
    @Body() dto: BatchAttendanceDto,
  ): Promise<AttendanceRecord[]> {
    return this.attendanceService.markBatchAttendance(dto);
  }

  @Put('records/:id')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Update an individual attendance record',
    description: `
## Update Attendance Record

Updates an existing attendance record for a student.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Updatable Fields
- \`attendanceStatus\`: New status (present, absent, late, excused)
- \`notes\`: Additional notes
- \`checkInTime\`: Check-in time

### Restrictions
- Cannot update records for closed sessions
    `,
  })
  @ApiParam({ name: 'id', description: 'Record ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Record updated successfully',
    type: AttendanceRecord,
  })
  @ApiResponse({ status: 400, description: 'Session is closed' })
  @ApiResponse({ status: 404, description: 'Record not found' })
  async updateRecord(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateRecordDto,
  ): Promise<AttendanceRecord> {
    return this.attendanceService.updateRecord(id, dto);
  }

  // ==================== SUMMARY ENDPOINTS ====================

  @Get('my')
  @Roles(RoleName.STUDENT)
  @ApiOperation({
    summary: "Get current student's own attendance summary",
    description: `
## My Attendance Summary

Retrieves the authenticated student's own attendance summary across all enrolled courses.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: STUDENT

### Response Includes
For each enrolled course:
- Course and section information
- Total sessions
- Present/Absent/Late/Excused counts
- Attendance percentage
- At-risk flag (if below 75%)
    `,
  })
  @ApiResponse({
    status: 200,
    description: 'Student attendance summary',
  })
  async getMyAttendance(@CurrentUser() user: any) {
    return this.attendanceService.getStudentAttendance(user.userId);
  }

  @Get('by-student/:studentId')
  @Roles(RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get attendance summary for a specific student',
    description: `
## Student Attendance Summary

Retrieves attendance summary for a specific student. Students can only view their own data unless they are instructors/TAs/admins.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: STUDENT (own data only), INSTRUCTOR, TA, ADMIN

### Query Parameters
- \`courseId\`: (Optional) Filter by specific course

### Response Includes
- Course-wise attendance breakdown
- Overall attendance percentage
- At-risk indicator
    `,
  })
  @ApiParam({ name: 'studentId', description: 'Student user ID', example: 21 })
  @ApiResponse({
    status: 200,
    description: 'Student attendance summary',
  })
  @ApiResponse({ status: 403, description: 'Forbidden - cannot access other student data' })
  async getStudentAttendance(
    @Param('studentId', ParseIntPipe) studentId: number,
    @Query('courseId') courseId?: number,
    @CurrentUser() user?: any,
  ) {
    // Authorization check
    if (user) {
      const userRoles = user.roles?.map((r: any) => r.roleName) || [];
      const isAdmin = userRoles.includes(RoleName.ADMIN) || userRoles.includes(RoleName.IT_ADMIN);
      const isInstructorOrTA = userRoles.includes(RoleName.INSTRUCTOR) || userRoles.includes(RoleName.TA);
      const isStudent = userRoles.includes(RoleName.STUDENT);

      // Admin has full access
      if (isAdmin) {
        return this.attendanceService.getStudentAttendance(studentId, courseId);
      }

      // Instructors/TAs can view attendance for courses they teach
      if (isInstructorOrTA) {
        // For simplicity, allow instructors/TAs to view any student data if a courseId is provided
        // A more complete implementation would verify the instructor teaches that specific course
        if (courseId) {
          return this.attendanceService.getStudentAttendance(studentId, courseId);
        }
        // Without courseId filter, instructors can still view student data
        // (assuming they have valid course relationships)
        return this.attendanceService.getStudentAttendance(studentId, courseId);
      }

      // Students can only view their own data
      if (isStudent && studentId !== user.userId) {
        throw new ForbiddenException('You can only view your own attendance data');
      }
    }

    return this.attendanceService.getStudentAttendance(studentId, courseId);
  }

  @Get('by-course/:courseId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get attendance summary for a course (all sections)',
    description: `
## Course Attendance Summary

Retrieves attendance summary for all sections of a course.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
For each section:
- Section information
- Total sessions conducted
- Average attendance percentage
- Number of at-risk students (below 75%)
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Course attendance summary by section',
  })
  async getCourseAttendance(@Param('courseId', ParseIntPipe) courseId: number) {
    return this.attendanceService.getCourseAttendanceSummary(courseId);
  }

  @Get('summary/:sectionId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get attendance summary for a section',
    description: `
## Section Attendance Summary

Retrieves detailed attendance summary for a specific course section.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
- Section and course information
- Total sessions conducted
- Average attendance rate
- Number of at-risk students
- Per-student attendance breakdown
    `,
  })
  @ApiParam({ name: 'sectionId', description: 'Section ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Section attendance summary',
  })
  async getSectionSummary(@Param('sectionId', ParseIntPipe) sectionId: number) {
    return this.attendanceService.getSectionSummary(sectionId);
  }

  @Get('trends/:sectionId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get weekly attendance trends for a section',
    description: `
## Weekly Attendance Trends

Retrieves weekly attendance trends for a section to identify patterns.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
Array of weekly data:
- Week number and date range
- Sessions held that week
- Average attendance percentage
- Present/Absent/Late counts

### Use Cases
- Identify declining attendance patterns
- Correlate attendance with academic calendar
- Plan interventions for at-risk periods
    `,
  })
  @ApiParam({ name: 'sectionId', description: 'Section ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Weekly attendance trends',
  })
  async getWeeklyTrends(@Param('sectionId', ParseIntPipe) sectionId: number) {
    return this.attendanceService.getWeeklyTrends(sectionId);
  }

  @Get('report/:sectionId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get detailed attendance report for a section',
    description: `
## Section Attendance Report

Generates a comprehensive attendance report for a section, suitable for administrative purposes.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
- Complete attendance matrix (students Γ— sessions)
- Individual student statistics
- At-risk student list
- Session-wise breakdown
    `,
  })
  @ApiParam({ name: 'sectionId', description: 'Section ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Detailed section attendance report',
  })
  async getSectionReport(@Param('sectionId', ParseIntPipe) sectionId: number) {
    return this.attendanceService.getSectionSummary(sectionId);
  }

  // ==================== EXCEL IMPORT/EXPORT ENDPOINTS ====================

  @Post('import-excel')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({
    summary: 'Import attendance from Excel file',
    description: `
## Import Attendance from Excel

Imports attendance data from an Excel file for a specific session.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Excel File Format
Required columns:
- \`student_id\` or \`email\`: Student identifier
- \`status\`: Attendance status (PRESENT/P, ABSENT/A, LATE/L, EXCUSED/E)

Optional columns:
- \`notes\`: Additional notes

### Behavior
- Validates student IDs against enrolled students
- Skips invalid/unknown students
- Returns detailed import result with success/failure counts

### File Requirements
- Format: .xlsx (Excel 2007+)
- Max size: 5MB
- First row must be headers
    `,
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Excel file with attendance data',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: 'Excel file (.xlsx) with columns: student_id, status',
        },
        sessionId: {
          type: 'number',
          description: 'Session ID to import attendance for',
        },
      },
      required: ['file', 'sessionId'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Import result with success/failure counts',
  })
  @ApiResponse({ status: 400, description: 'Invalid Excel format' })
  async importFromExcel(
    @UploadedFile() file: Express.Multer.File,
    @Body() dto: ImportAttendanceDto,
  ) {
    return this.excelService.importFromExcel(dto.sessionId, file.buffer);
  }

  @Get('export-excel/:sessionId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Export attendance session to Excel file',
    description: `
## Export Session to Excel

Exports attendance data for a single session to an Excel file.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Excel Output
- Session information header
- Student list with:
  - Student ID, Name, Email
  - Attendance status
  - Check-in time
  - Notes
- Summary statistics
    `,
  })
  @ApiParam({ name: 'sessionId', description: 'Session ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Excel file download',
    content: {
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': {},
    },
  })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async exportToExcel(
    @Param('sessionId', ParseIntPipe) sessionId: number,
    @Res({ passthrough: true }) res: Response,
  ): Promise<StreamableFile> {
    const buffer = await this.excelService.exportToExcel(sessionId);
    res.set({
      'Content-Type':
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Content-Disposition': `attachment; filename="attendance_session_${sessionId}.xlsx"`,
    });
    return new StreamableFile(buffer);
  }

  @Get('export-excel/section/:sectionId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Export all attendance for a section to Excel',
    description: `
## Export Section Attendance to Excel

Exports complete attendance data for all sessions in a section.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Excel Output
- Section and course information
- Attendance matrix (students as rows, sessions as columns)
- Individual student attendance percentages
- At-risk students highlighted
    `,
  })
  @ApiParam({ name: 'sectionId', description: 'Section ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Excel file download with all sessions',
    content: {
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': {},
    },
  })
  async exportSectionToExcel(
    @Param('sectionId', ParseIntPipe) sectionId: number,
    @Res({ passthrough: true }) res: Response,
  ): Promise<StreamableFile> {
    const buffer = await this.excelService.exportSectionToExcel(sectionId);
    res.set({
      'Content-Type':
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Content-Disposition': `attachment; filename="attendance_section_${sectionId}.xlsx"`,
    });
    return new StreamableFile(buffer);
  }

  // ==================== AI PHOTO ENDPOINTS ====================

  @Post('face-references/me')
  @Roles(RoleName.STUDENT)
  @UseInterceptors(FileInterceptor('image'))
  @ApiOperation({
    summary: 'Upload my face reference image',
    description:
      'Uploads a student face reference image to Supabase private storage and marks it as the primary image for AI attendance.',
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        image: {
          type: 'string',
          format: 'binary',
          description: 'Face image (jpeg/png/webp)',
        },
      },
      required: ['image'],
    },
  })
  @ApiResponse({ status: 201, description: 'Face reference uploaded' })
  async uploadMyFaceReference(
    @UploadedFile() image: Express.Multer.File,
    @CurrentUser() user: any,
  ) {
    return this.studentFaceReferenceService.uploadMyReference(user.userId, image);
  }

  @Get('face-references/me')
  @Roles(RoleName.STUDENT)
  @ApiOperation({
    summary: 'List my face reference images',
    description:
      'Returns all active face reference images for the current student, including signed URLs.',
  })
  @ApiResponse({ status: 200, description: 'List of student face references' })
  async getMyFaceReferences(@CurrentUser() user: any) {
    return this.studentFaceReferenceService.listMyReferences(user.userId);
  }

  @Delete('face-references/me/:referenceId')
  @Roles(RoleName.STUDENT)
  @ApiOperation({
    summary: 'Delete my face reference image',
    description:
      'Deletes a student face reference image from Supabase storage and marks it inactive.',
  })
  @ApiParam({ name: 'referenceId', description: 'Face reference ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Face reference deleted' })
  async deleteMyFaceReference(
    @Param('referenceId', ParseIntPipe) referenceId: number,
    @CurrentUser() user: any,
  ) {
    await this.studentFaceReferenceService.deleteMyReference(user.userId, referenceId);
    return { success: true };
  }

  @Get('face-references/section/:sectionId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get section face references for AI matching',
    description:
      'Returns signed URLs for active primary student reference images restricted to enrolled students in the section.',
  })
  @ApiParam({ name: 'sectionId', description: 'Section ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Section-scoped face references' })
  async getSectionFaceReferences(
    @Param('sectionId', ParseIntPipe) sectionId: number,
  ) {
    return this.studentFaceReferenceService.getSectionReferencesForAi(sectionId);
  }

  @Post('ai-photo')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @UseInterceptors(FileInterceptor('photo'))
  @ApiOperation({
    summary: 'Upload photo for AI-based attendance recognition',
    description: `
## AI Photo Attendance

Uploads a classroom photo for AI-based face recognition to automatically mark attendance.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Process Flow
1. Upload photo with session ID
2. System returns a \`processingId\`
3. AI service processes the image asynchronously
4. Poll \`GET /attendance/ai-photo/{processingId}\` for results
5. Once complete, attendance records are automatically updated

### Requirements
- Photo format: JPEG, PNG
- Recommended resolution: 1920x1080 or higher
- Good lighting for accurate recognition
- Clear view of student faces

### Notes
- Processing time: 30-60 seconds typically
- Requires pre-registered student face data
- Manual review recommended for low-confidence matches
    `,
  })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'Class photo for AI processing',
    schema: {
      type: 'object',
      properties: {
        photo: {
          type: 'string',
          format: 'binary',
          description: 'Class photo for face recognition',
        },
        sessionId: {
          type: 'number',
          description: 'Session ID for attendance',
        },
      },
      required: ['photo', 'sessionId'],
    },
  })
  @ApiResponse({
    status: 200,
    description: 'Processing started, returns processing ID for polling',
  })
  @ApiResponse({ status: 404, description: 'Session not found' })
  async uploadPhotoForAi(
    @UploadedFile() photo: Express.Multer.File,
    @Body('sessionId', ParseIntPipe) sessionId: number,
    @CurrentUser() user: any,
  ) {
    // When the Files module stores the class photo, pass its id here. Otherwise NULL (no FK to files).
    return this.aiService.uploadPhotoForProcessing(
      sessionId,
      null,
      user.userId,
      photo,
    );
  }

  @Get('ai-photo/:processingId')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
  @ApiOperation({
    summary: 'Get AI processing result',
    description: `
## Get AI Processing Result

Polls for the result of an AI photo processing request.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: INSTRUCTOR, TA, ADMIN

### Response Includes
- Processing status (pending, processing, completed, failed)
- If completed:
  - Detected faces count
  - Matched students count
  - Unmatched faces count
  - List of matched students with confidence scores
  - Attendance records that were updated

### Polling Strategy
- Initial wait: 5 seconds
- Poll interval: 5-10 seconds
- Timeout: 2 minutes (processing failed if not complete)
    `,
  })
  @ApiParam({ name: 'processingId', description: 'Processing ID', example: 1 })
  @ApiResponse({
    status: 200,
    description: 'Processing result with matched students count',
  })
  async getAiProcessingResult(
    @Param('processingId', ParseIntPipe) processingId: number,
  ) {
    return this.aiService.getProcessingResult(processingId);
  }
}