package com.smartoffice.attendance;

import com.smartoffice.auth.UserPrincipal;
import com.smartoffice.common.ApiResponse;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/attendance")
public class AttendanceController {
    private final AttendanceService attendanceService;

    public AttendanceController(AttendanceService attendanceService) {
        this.attendanceService = attendanceService;
    }

    @PostMapping("/check-in")
    public ApiResponse<AttendanceDto> checkIn(@AuthenticationPrincipal UserPrincipal principal,
                                              @Valid @RequestBody(required = false) CheckInRequest request) {
        if (request == null) {
            request = new CheckInRequest();
        }
        return ApiResponse.created(attendanceService.checkIn(principal.getUsername(), request));
    }

    @PostMapping("/check-out")
    public ApiResponse<AttendanceDto> checkOut(@AuthenticationPrincipal UserPrincipal principal) {
        return ApiResponse.ok(attendanceService.checkOut(principal.getUsername()));
    }

    @GetMapping("/me")
    public ApiResponse<List<AttendanceDto>> myRecords(@AuthenticationPrincipal UserPrincipal principal) {
        return ApiResponse.ok(attendanceService.myRecords(principal.getUsername()));
    }
}
