package br.gov.sigefp.rh.api;

import br.gov.sigefp.rh.api.dto.PerformanceEvaluationDTO;
import br.gov.sigefp.rh.service.PerformanceEvaluationService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * Controller REST para gestão de avaliações de desempenho.
 */
@RestController
@RequestMapping("/api/rh/evaluations")
@RequiredArgsConstructor
public class PerformanceEvaluationController {

    private final PerformanceEvaluationService evaluationService;

    @GetMapping
    public ResponseEntity<Page<PerformanceEvaluationDTO>> findAll(
            @RequestParam(value = "page", defaultValue = "0") int page,
            @RequestParam(value = "size", defaultValue = "20") int size,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "sortDirection", required = false, defaultValue = "DESC") String sortDirection) {

        Sort sort = sortBy != null
                ? Sort.by(Sort.Direction.fromString(sortDirection), sortBy)
                : Sort.by(Sort.Direction.DESC, "evaluationDate");

        Pageable pageable = PageRequest.of(page, size, sort);
        Page<PerformanceEvaluationDTO> result = evaluationService.findAll(pageable);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/{id}/finalize")
    public ResponseEntity<Void> finalizeEvaluation(@PathVariable UUID id) {
        evaluationService.finalizeEvaluation(id);
        return ResponseEntity.ok().build();
    }
}
