package br.gov.sigefp.treasury.api;

import br.gov.sigefp.treasury.api.dto.ApprovePaymentDTO;
import br.gov.sigefp.treasury.api.dto.CreatePaymentAuthorizationDTO;
import br.gov.sigefp.treasury.api.dto.PaymentAuthorizationDTO;
import br.gov.sigefp.treasury.api.dto.RejectPaymentDTO;
import br.gov.sigefp.treasury.service.PaymentAuthorizationService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/treasury/authorizations")
@RequiredArgsConstructor
public class PaymentAuthorizationController {

    private final PaymentAuthorizationService authorizationService;

    @PostMapping
    public ResponseEntity<PaymentAuthorizationDTO> requestAuthorization(
            @Valid @RequestBody CreatePaymentAuthorizationDTO dto) {
        PaymentAuthorizationDTO created = authorizationService.requestAuthorization(dto);
        return ResponseEntity.status(HttpStatus.CREATED).body(created);
    }

    @PostMapping("/{id}/approve")
    public ResponseEntity<PaymentAuthorizationDTO> approve(
            @PathVariable UUID id,
            @Valid @RequestBody ApprovePaymentDTO dto) {
        PaymentAuthorizationDTO updated = authorizationService.approve(
                id, dto.getApproverId(), dto.getComments());
        return ResponseEntity.ok(updated);
    }

    @PostMapping("/{id}/reject")
    public ResponseEntity<PaymentAuthorizationDTO> reject(
            @PathVariable UUID id,
            @Valid @RequestBody RejectPaymentDTO dto) {
        PaymentAuthorizationDTO updated = authorizationService.reject(
                id, dto.getApproverId(), dto.getReason());
        return ResponseEntity.ok(updated);
    }

    @GetMapping("/{id}")
    public ResponseEntity<PaymentAuthorizationDTO> findById(@PathVariable UUID id) {
        PaymentAuthorizationDTO authorization = authorizationService.findById(id);
        return ResponseEntity.ok(authorization);
    }

    @GetMapping("/pending")
    public ResponseEntity<List<PaymentAuthorizationDTO>> findPendingApprovals(
            @RequestParam(name = "approverId") UUID approverId) {
        List<PaymentAuthorizationDTO> authorizations = authorizationService.findPendingApprovals(approverId);
        return ResponseEntity.ok(authorizations);
    }

    @GetMapping("/payment-order/{paymentOrderId}")
    public ResponseEntity<List<PaymentAuthorizationDTO>> findByPaymentOrderId(
            @PathVariable UUID paymentOrderId) {
        List<PaymentAuthorizationDTO> authorizations = authorizationService.findByPaymentOrderId(paymentOrderId);
        return ResponseEntity.ok(authorizations);
    }

    @GetMapping("/payment-batch/{paymentBatchId}")
    public ResponseEntity<List<PaymentAuthorizationDTO>> findByPaymentBatchId(
            @PathVariable UUID paymentBatchId) {
        List<PaymentAuthorizationDTO> authorizations = authorizationService.findByPaymentBatchId(paymentBatchId);
        return ResponseEntity.ok(authorizations);
    }
}
