package br.gov.sigefp.org.api;

import br.gov.sigefp.org.api.dto.PositionDTO;
import br.gov.sigefp.org.service.PositionService;
import jakarta.validation.Valid;
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.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * Controller REST para gestão de posições/cargos.
 */
@RestController
@RequestMapping("/api/org/positions")
@RequiredArgsConstructor
public class PositionController {

    private final PositionService positionService;

    @GetMapping
    public ResponseEntity<?> findAll(
            @RequestParam(value = "orgUnitId", required = false) UUID orgUnitId,
            @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 = "ASC") String sortDirection) {
        
        if (orgUnitId != null) {
            // Retornar lista quando filtrado por orgUnitId
            List<PositionDTO> positions = positionService.findByOrgUnitId(orgUnitId);
            return ResponseEntity.ok(positions);
        }
        
        Sort sort = sortBy != null 
            ? Sort.by(Sort.Direction.fromString(sortDirection), sortBy)
            : Sort.by(Sort.Direction.ASC, "code");
        
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<PositionDTO> result = positionService.findAll(pageable);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{id}")
    public ResponseEntity<PositionDTO> findById(@PathVariable("id") UUID id) {
        try {
            PositionDTO dto = positionService.findById(id);
            return ResponseEntity.ok(dto);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping
    public ResponseEntity<PositionDTO> create(@Valid @RequestBody PositionDTO dto) {
        try {
            PositionDTO created = positionService.create(dto);
            return ResponseEntity.status(HttpStatus.CREATED).body(created);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<PositionDTO> update(
            @PathVariable("id") UUID id,
            @Valid @RequestBody PositionDTO dto) {
        try {
            PositionDTO updated = positionService.update(id, dto);
            return ResponseEntity.ok(updated);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }
}

