using vendorApproverBackend.Data; using vendorApproverBackend.Entities; using vendorApproverBackend.Enums; using vendorApproverBackend.Responses; using Microsoft.EntityFrameworkCore; using vendorApproverBackend.Dtos; public class RenewClarificationService { private readonly DataContext _context; public RenewClarificationService(DataContext context) { _context = context; } public async Task AddClarificationAsync(long renewId, string message, long senderId) { var clarification = new RequestRenewClarification { RequestRenewId = renewId, SenderId = senderId, Message = message, Timestamp = DateTime.UtcNow }; _context.RequestRenewClarifications.Add(clarification); await _context.SaveChangesAsync(); // Reload with Sender to populate Transient fields (Name/Role) return await _context.RequestRenewClarifications .Include(c => c.Sender) .FirstOrDefaultAsync(c => c.Id == clarification.Id); } public async Task> GetClarificationsPaginatedAsync( int page, int size, long renewId) { // Ensure page and size have valid values if (page < 0) page = 0; if (size <= 0) size = 5; var query = _context.RequestRenewClarifications .Include(c => c.Sender) .Where(c => c.RequestRenewId == renewId); // 1. Get total count for the PagedResult metadata var totalCount = await query.CountAsync(); // 2. Sort by most recent (Descending) and apply pagination var items = await query .OrderByDescending(c => c.Timestamp) .Skip(page * size) .Take(size) .ToListAsync(); return new PagedResult(items, totalCount, page, size); } public async Task UpdateClarificationAsync(long id, string newMessage) { var clarification = await _context.RequestRenewClarifications .Include(c => c.Sender) .FirstOrDefaultAsync(c => c.Id == id); if (clarification != null) { clarification.Message = newMessage; await _context.SaveChangesAsync(); } return clarification; } public async Task> GetClarificationsListByRenewIdAsync(long renewId) { return await _context.RequestRenewClarifications .Include(c => c.Sender) .Where(c => c.RequestRenewId == renewId) .OrderBy(c => c.Timestamp) // Oldest to Newest .ToListAsync(); } // --- PQ REQUEST METHODS --- public async Task AddPqClarificationAsync(long pqId, string message, long senderId) { var clarification = new RequestRenewClarification { PrequalificationRequestId = pqId, SenderId = senderId, Message = message, Timestamp = DateTime.UtcNow }; _context.RequestRenewClarifications.Add(clarification); await _context.SaveChangesAsync(); return await _context.RequestRenewClarifications .Include(c => c.Sender) .FirstOrDefaultAsync(c => c.Id == clarification.Id); } public async Task> GetClarificationsListByPqIdAsync(long pqId) { return await _context.RequestRenewClarifications .Include(c => c.Sender) .Where(c => c.PrequalificationRequestId == pqId) .OrderBy(c => c.Timestamp) .ToListAsync(); } // --- VENDOR LETTER METHODS --- public async Task AddLetterClarificationAsync(long letterId, string message, long senderId) { var clarification = new RequestRenewClarification { VendorRequestLetterId = letterId, SenderId = senderId, Message = message, Timestamp = DateTime.UtcNow }; _context.RequestRenewClarifications.Add(clarification); await _context.SaveChangesAsync(); return await _context.RequestRenewClarifications .Include(c => c.Sender) .FirstOrDefaultAsync(c => c.Id == clarification.Id); } public async Task> GetClarificationsListByLetterIdAsync(long letterId) { return await _context.RequestRenewClarifications .Include(c => c.Sender) .Where(c => c.VendorRequestLetterId == letterId) .OrderBy(c => c.Timestamp) .ToListAsync(); } }