import { Controller, Get, Post, Body, Param, ParseIntPipe } from '@nestjs/common';
import { IotService } from './iot.service';

@Controller()
export class IotController {
  constructor(private readonly iotService: IotService) {}

  @Get()
  getHello(): string {
    return this.iotService.getHello();
  }

  @Get('time')
  getTime() {
    return this.iotService.getTime();
  }

  @Post('scan')
  receiveScan(@Body() body: { uid: string; timestamp: number }) {
    return this.iotService.receiveScan(body.uid, body.timestamp);
  }

  // Student management endpoints
  @Post('students')
  registerStudent(@Body() body: { firstName: string; lastName: string; className: string; rfidUid: string }) {
    return this.iotService.registerStudent(body.firstName, body.lastName, body.className, body.rfidUid);
  }

  @Get('students')
  getAllStudents() {
    return this.iotService.getAllStudents();
  }

  @Get('students/:id')
  getStudent(@Param('id', ParseIntPipe) id: number) {
    return this.iotService.getStudent(id);
  }

  @Post('students/:id/assign-card')
  assignCard(@Param('id', ParseIntPipe) id: number, @Body() body: { rfidUid: string }) {
    return this.iotService.assignCard(id, body.rfidUid);
  }

  @Post('students/:id/update')
  updateStudent(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { firstName?: string; lastName?: string; className?: string; rfidUid?: string }
  ) {
    return this.iotService.updateStudent(id, body.firstName, body.lastName, body.className, body.rfidUid);
  }

  @Post('students/:id/card-active')
  setCardActive(@Param('id', ParseIntPipe) id: number, @Body() body: { isActive: boolean }) {
    return this.iotService.setCardActive(id, body.isActive);
  }

  @Post('students/:id/delete')
  deleteStudent(@Param('id', ParseIntPipe) id: number) {
    return this.iotService.deleteStudent(id);
  }

  // Attendance endpoints
  @Get('attendance/present')
  getCurrentlyPresent() {
    return this.iotService.getCurrentlyPresent();
  }

  @Get('attendance/date/:date')
  getScansForDate(@Param('date') date: string) {
    return this.iotService.getScansForDate(date);
  }

  @Get('attendance/student/:id')
  getStudentAttendance(@Param('id', ParseIntPipe) id: number) {
    return this.iotService.getStudentAttendance(id);
  }

  @Get('attendance/all')
  getAllAttendanceLogs() {
    return this.iotService.getAllAttendanceLogs();
  }

  // Admin endpoints
  @Post('admin/attendance/manual-entry')
  addManualEntry(@Body() body: { studentId: number; scannedAt: string; eventType: string; reason: string }) {
    return this.iotService.addManualEntry(body.studentId, new Date(body.scannedAt), body.eventType, body.reason);
  }

  @Post('admin/attendance/log/:logId/delete')
  deleteAttendanceLog(@Param('logId', ParseIntPipe) logId: number) {
    return this.iotService.deleteAttendanceLog(logId);
  }
}
