using Microsoft.AspNetCore.Mvc; using vendorApproverBackend.Responses; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using System.Security.Claims; using vendorApproverBackend.Services; [ApiController] [Route("api/stats")] public class StatisticsController : ControllerBase { private readonly StatsService _statsService; private readonly UserService _userService; public StatisticsController(StatsService statsService, UserService userService) { _statsService = statsService; _userService = userService; } [HttpGet("admin-stats")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,RENWE_APPROVER,MANAGER,PERFERMANCE_MANAGER")] public async Task> GetAdminStats() { var stats = await _statsService.GetAdminStatsAsync(); return Ok(stats); } [HttpGet("vendor-stats/{vendorId}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,RENWE_APPROVER,MANAGER,PERFERMANCE_MANAGER")] public async Task> GetVendorStats(long vendorId) { 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 is not a vendor "); } var stats = await _statsService.GetVendorStatsAsync(user.Vendor.Id); return Ok(stats); } [HttpGet("employee-stats")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,RENWE_APPROVER,MANAGER,PERFERMANCE_MANAGER")] public async Task> GetEmployeeStats() { 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."); } var stats = await _statsService.GetEmployeeStatsAsync(user.Id); return Ok(stats); } [HttpGet("payment-financial-and-admin-stats")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,RENWE_APPROVER,MANAGER,PERFERMANCE_MANAGER")] public async Task> GetFinancialStats() { var stats = await _statsService.GetFinancialStatsAsync(); return Ok(stats); } }