package br.gov.sigefp.rh.api;

import br.gov.sigefp.rh.api.dto.AttendanceRecordDTO;
import br.gov.sigefp.rh.api.dto.MonthlyAttendanceSheetDTO;
import br.gov.sigefp.rh.domain.AttendanceRecord;
import br.gov.sigefp.rh.domain.MonthlyAttendanceSheet;
import br.gov.sigefp.rh.repository.AttendanceRecordRepository;
import br.gov.sigefp.rh.service.AttendanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/rh/attendance")
@RequiredArgsConstructor
public class AttendanceController {

    private final AttendanceService attendanceService;
    private final AttendanceRecordRepository recordRepository;

    @GetMapping("/sheet")
    public ResponseEntity<MonthlyAttendanceSheetDTO> getSheet(
            @RequestParam UUID orgUnitId,
            @RequestParam int month,
            @RequestParam int year) {

        MonthlyAttendanceSheet sheet = attendanceService.getOrCreateSheet(orgUnitId, month, year);
        return ResponseEntity.ok(toSummaryDTO(sheet));
    }

    @GetMapping("/sheet/{sheetId}/records")
    public ResponseEntity<List<AttendanceRecordDTO>> getRecords(@PathVariable UUID sheetId) {
        List<AttendanceRecord> records = recordRepository.findBySheetId(sheetId);
        return ResponseEntity.ok(records.stream().map(this::toRecordDTO).collect(Collectors.toList()));
    }

    @PostMapping("/sheet/{sheetId}/approve")
    public ResponseEntity<Void> approveSheet(@PathVariable UUID sheetId) {
        // TODO: Extract user from Security Context
        attendanceService.approveSheet(sheetId, "Admin User");
        return ResponseEntity.ok().build();
    }

    @PostMapping("/sheet/{sheetId}/reopen")
    public ResponseEntity<Void> reopenSheet(@PathVariable UUID sheetId) {
        attendanceService.reopenSheet(sheetId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/sheet/{sheetId}/upload")
    public ResponseEntity<Void> uploadExcel(@PathVariable UUID sheetId, @RequestParam("file") MultipartFile file) {
        attendanceService.importExcel(sheetId, file);
        return ResponseEntity.ok().build();
    }

    private MonthlyAttendanceSheetDTO toSummaryDTO(MonthlyAttendanceSheet sheet) {
        return MonthlyAttendanceSheetDTO.builder()
                .id(sheet.getId())
                .orgUnitId(sheet.getOrgUnitId())
                .month(sheet.getMonth())
                .year(sheet.getYear())
                .status(sheet.getStatus())
                .approvedBy(sheet.getApprovedBy())
                .build();
    }

    private AttendanceRecordDTO toRecordDTO(AttendanceRecord record) {
        return AttendanceRecordDTO.builder()
                .id(record.getId())
                .agentId(record.getAgent().getId())
                .agentName(record.getAgent().getFullName())
                .date(record.getDate())
                .type(record.getType())
                .observation(record.getObservation())
                .build();
    }
}
