using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using vendorApproverBackend.Enums; using vendorApproverBackend.Requests; using vendorApproverBackend.Services; namespace vendorApproverBackend.Controllers { [ApiController] [Route("api/[controller]")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL")] public class PaymentDetailsController : ControllerBase { private readonly PaymentDetailService _paymentDetailService; public PaymentDetailsController(PaymentDetailService paymentDetailService) { _paymentDetailService = paymentDetailService; } [HttpGet("{id}")] public async Task FindById(long id) { var paymentDetail = await _paymentDetailService.FindById(id); if (paymentDetail == null) { return NotFound($"PaymentDetail with ID {id} not found."); } return Ok(paymentDetail); } [HttpGet("request/{requestId}")] public async Task FindByRequestId(long requestId) { var paymentDetail = await _paymentDetailService.FindByPrequalificationRequestId(requestId); if (paymentDetail == null) { return NotFound($"No PaymentDetail found for PrequalificationRequest with ID {requestId}."); } return Ok(paymentDetail); } [HttpPost("add-payment-details/{requestId}")] public async Task AddPaymentDetails(long requestId, [FromBody] AddPaymentDetailRequest dto) { try { var newPaymentDetail = await _paymentDetailService.AddPaymentDetails(requestId, dto); return CreatedAtAction(nameof(FindById), new { id = newPaymentDetail.Id }, newPaymentDetail); } catch (Exception ex) { return BadRequest(ex.Message); } } [HttpPost("add-letter-product-payment/{productId}/{requestLetterId}")] public async Task AddProductPayment(long productId, long requestLetterId, [FromBody] AddPaymentDetailRequest dto) { try { // This will now target the LetterProducts table and set the PAYMENT_PENDING status var newPaymentDetail = await _paymentDetailService.AddProductPaymentDetails(productId, requestLetterId, dto); return CreatedAtAction(nameof(FindById), new { id = newPaymentDetail.Id }, newPaymentDetail); } catch (Exception ex) { return BadRequest(ex.Message); } } [HttpPatch("update-payment-details/{requestId}")] public async Task UpdatePaymentDetails(long requestId, [FromBody] AddPaymentDetailRequest dto) { try { var updatedPaymentDetail = await _paymentDetailService.UpdatePaymentDetails(requestId, dto); return Ok(updatedPaymentDetail); } catch (Exception ex) { return NotFound(ex.Message); } } [HttpPatch("update-payment-status/{requestId}")] public async Task UpdatePaymentStatus(long requestId, [FromBody] UpdatePaymentDetailStatus dto) { var success = await _paymentDetailService.UpdatePaymentStatus(requestId, dto); if (!success) { return NotFound($"PaymentDetail for PrequalificationRequest ID {requestId} not found."); } return NoContent(); } [HttpPatch("update-product-payment-status/{letterProductId}")] public async Task UpdateProductPaymentStatus(long letterProductId, [FromBody] UpdatePaymentDetailStatus dto) { try { var success = await _paymentDetailService.UpdateProductPaymentStatus(letterProductId, dto); if (!success) { return NotFound($"PaymentDetail for Product ID {letterProductId} not found."); } return NoContent(); } catch (Exception ex) { return BadRequest(ex.Message); } } [HttpDelete("delete-payment-details/{id}")] public async Task Delete(long id) { var success = await _paymentDetailService.Delete(id); if (!success) { return NotFound($"PaymentDetail with ID {id} not found."); } return NoContent(); } [HttpGet("all_requests_paginated")] public async Task FetchPaymentsDetailsPaginator( [FromQuery] int page = 0, [FromQuery] int size = 5, [FromQuery] long? id = null, [FromQuery] PaymentStatus? status = null, [FromQuery] PaymentMethode? paymentMethode = null, [FromQuery] DateTime? paymentDate = null, [FromQuery] double? amount = null, [FromQuery] string vendorRepresentativeName = null, [FromQuery] RequestStatus? requestStatus = null) { try { var pagedResult = await _paymentDetailService.FindByCriteria( page, size, id, status, paymentMethode, paymentDate, amount, vendorRepresentativeName, requestStatus); return Ok(pagedResult); } catch (Exception ex) { return StatusCode(406, $"Somthing went wrong: {ex.Message}"); } } } }