package br.gov.sigefp.rh.api;

import br.gov.sigefp.rh.domain.GlobalDeductionRule;
import br.gov.sigefp.rh.domain.TaxBracket;
import br.gov.sigefp.rh.repository.TaxBracketRepository;
import br.gov.sigefp.rh.service.TaxService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * Controller para parametrização de impostos e regras tributárias.
 */
@RestController
@RequestMapping("/api/rh/taxes")
@RequiredArgsConstructor
public class TaxController {

    private final TaxService taxService;
    private final TaxBracketRepository taxBracketRepository;

    @GetMapping("/rules")
    public List<GlobalDeductionRule> findAllRules() {
        return taxService.findAllRules();
    }

    @PostMapping("/rules")
    public GlobalDeductionRule saveRule(@RequestBody GlobalDeductionRule rule) {
        return taxService.saveRule(rule);
    }

    @DeleteMapping("/rules/{id}")
    public ResponseEntity<Void> deleteRule(@PathVariable UUID id) {
        taxService.deleteRule(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/brackets")
    public List<TaxBracket> findAllBrackets() {
        return taxService.findAllBrackets();
    }

    @PostMapping("/brackets")
    public TaxBracket saveBracket(@RequestBody TaxBracket bracket) {
        return taxService.saveBracket(bracket);
    }

    @DeleteMapping("/brackets/{id}")
    public ResponseEntity<Void> deleteBracket(@PathVariable UUID id) {
        taxService.deleteBracket(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/brackets/active")
    public List<TaxBracket> getActiveBrackets() {
        return taxBracketRepository.findActiveBrackets(LocalDate.now());
    }

    @GetMapping("/types")
    public List<br.gov.sigefp.rh.domain.DeductionType> getDeductionTypes() {
        return taxService.findAllDeductionTypes();
    }

    @PostMapping("/types")
    public br.gov.sigefp.rh.domain.DeductionType saveDeductionType(
            @RequestBody br.gov.sigefp.rh.domain.DeductionType type) {
        return taxService.saveDeductionType(type);
    }

    @PutMapping("/types/{id}/toggle")
    public void toggleDeductionType(@PathVariable UUID id) {
        taxService.toggleDeductionTypeStatus(id);
    }
}
