using Microsoft.AspNetCore.Mvc; using vendorApproverBackend.Services; using vendorApproverBackend.Entities; using vendorApproverBackend.Responses; using vendorApproverBackend.Dtos; using Microsoft.AspNetCore.Authorization; using System.Security.Claims; [ApiController] [Route("api/certifications")] public class CertificationController : ControllerBase { private readonly CertificationService _certificationService; private readonly UserService _userService; public CertificationController(CertificationService certificationService, UserService userService) { _certificationService = certificationService; _userService = userService; } [HttpGet("get-by-id/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,PERFERMANCE_MANAGER")] public async Task> GetCertificationById(long id) { var certification = await _certificationService.GetCertificationByIdAsync(id); if (certification == null) { return NotFound($"Certification with ID {id} not found."); } return Ok(certification); } [HttpGet("get_all_certifications")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,PERFERMANCE_MANAGER")] public async Task>> GetAllCertifications( [FromQuery] int page = 0, [FromQuery] int size = 5, [FromQuery] long? requestId = null, [FromQuery] CertificationStatus? status = null, [FromQuery] string? supplierName = null, [FromQuery] long? supplierId = null) { try { var result = await _certificationService.GetCertificationsByCriteria( page, size, requestId, status, supplierName, supplierId); return Ok(result); } catch (Exception ex) { return StatusCode(500, $"Internal server error: {ex.Message}"); } } [HttpGet("getAllMyCertifications")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task>> getAllMyCertifications( [FromQuery] int page = 0, [FromQuery] int size = 5, [FromQuery] long? requestId = null, [FromQuery] CertificationStatus? status = null, [FromQuery] string? supplierName = null) { var username = User.FindFirstValue(ClaimTypes.Name); if (string.IsNullOrEmpty(username)) { return Unauthorized("Username claim not found in token."); } var user = await _userService.FindByUsernameAsync(username); if (user == null) { return NotFound("User not found."); } if (user.Vendor == null) { return NotFound("User us not a vendor"); } try { var result = await _certificationService.GetCertificationsByCriteria( page, size, requestId, status, supplierName, user.Vendor.Id); return Ok(result); } catch (Exception ex) { return StatusCode(500, $"Internal server error: {ex.Message}"); } } [HttpPatch("update-certification-status/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,PERFERMANCE_MANAGER")] public async Task> UpdateStatus(long id, [FromQuery] CertificationStatus status) { try { var updatedCertification = await _certificationService.UpdateCertificationStatusAsync(id, status); if (updatedCertification == null) { return NotFound($"Certification with ID {id} not found."); } return Ok(updatedCertification); } catch (Exception ex) { return StatusCode(500, $"Internal server error: {ex.Message}"); } } }