package br.gov.sigefp.rh.api;

import br.gov.sigefp.rh.api.dto.*;
import br.gov.sigefp.rh.service.SalaryStructureService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/rh/salary-structure")
@RequiredArgsConstructor
public class SalaryStructureController {

    private final SalaryStructureService service;

    @GetMapping("/summary")
    public ResponseEntity<SalaryStructureFullDTO> getFullStructure() {
        return ResponseEntity.ok(service.getFullStructure());
    }

    // --- Categories ---
    @GetMapping("/categories")
    public ResponseEntity<List<SalaryCategoryDTO>> getAllCategories() {
        return ResponseEntity.ok(service.getAllCategories());
    }

    @PostMapping("/categories")
    public ResponseEntity<SalaryCategoryDTO> createCategory(@Valid @RequestBody SalaryCategoryDTO dto) {
        return ResponseEntity.ok(service.createCategory(dto));
    }

    @PutMapping("/categories/{id}")
    public ResponseEntity<SalaryCategoryDTO> updateCategory(@PathVariable("id") UUID id,
            @Valid @RequestBody SalaryCategoryDTO dto) {
        return ResponseEntity.ok(service.updateCategory(id, dto));
    }

    @DeleteMapping("/categories/{id}")
    public ResponseEntity<Void> deleteCategory(@PathVariable("id") UUID id) {
        service.deleteCategory(id);
        return ResponseEntity.noContent().build();
    }

    // --- Grades ---
    @GetMapping("/categories/{categoryId}/grades")
    public ResponseEntity<List<SalaryGradeDTO>> getGradesByCategory(@PathVariable("categoryId") UUID categoryId) {
        return ResponseEntity.ok(service.getGradesByCategory(categoryId));
    }

    @PostMapping("/grades")
    public ResponseEntity<SalaryGradeDTO> createGrade(@Valid @RequestBody SalaryGradeDTO dto) {
        return ResponseEntity.ok(service.createGrade(dto));
    }

    @PutMapping("/grades/{id}")
    public ResponseEntity<SalaryGradeDTO> updateGrade(@PathVariable("id") UUID id,
            @Valid @RequestBody SalaryGradeDTO dto) {
        return ResponseEntity.ok(service.updateGrade(id, dto));
    }

    @DeleteMapping("/grades/{id}")
    public ResponseEntity<Void> deleteGrade(@PathVariable("id") UUID id) {
        service.deleteGrade(id);
        return ResponseEntity.noContent().build();
    }

    // --- Steps ---
    @GetMapping("/grades/{gradeId}/steps")
    public ResponseEntity<List<SalaryStepDTO>> getStepsByGrade(@PathVariable("gradeId") UUID gradeId) {
        return ResponseEntity.ok(service.getStepsByGrade(gradeId));
    }

    @PostMapping("/steps")
    public ResponseEntity<SalaryStepDTO> createStep(@Valid @RequestBody SalaryStepDTO dto) {
        return ResponseEntity.ok(service.createStep(dto));
    }

    @DeleteMapping("/steps/{id}")
    public ResponseEntity<Void> deleteStep(@PathVariable("id") UUID id) {
        service.deleteStep(id);
        return ResponseEntity.noContent().build();
    }

    // --- Grid ---
    @GetMapping("/steps/{stepId}/grid")
    public ResponseEntity<List<SalaryGridDTO>> getGridByStep(@PathVariable("stepId") UUID stepId) {
        return ResponseEntity.ok(service.getGridByStep(stepId));
    }

    @PostMapping("/grid")
    public ResponseEntity<SalaryGridDTO> createGridEntry(@Valid @RequestBody SalaryGridDTO dto) {
        return ResponseEntity.ok(service.createGridEntry(dto));
    }
}
