using Data.SeaHavenIndustries; using SeaHaven.DataServices.Interfaces; using SeaHaven.Services.DTOs; using SeaHaven.Services.Interfaces; namespace SeaHaven.Services.Implementation { public class QuotesService : IQuotesService { private readonly IQuotesDataService _quotesData; private readonly IWorkOrderDataService _workOrderData; public QuotesService(IQuotesDataService quotesData, IWorkOrderDataService workOrderData) { _quotesData = quotesData; _workOrderData = workOrderData; } public async Task CreateQuoteAsync(CreateQuoteInput input, CancellationToken cancellationToken) { var rnd = new Random(); var id = rnd.Next(1, 99999); var quote = new Quotes { QuotId = id.ToString(), ContactId = input.ContactId, PO = input.PO, TT = input.TT, ProjectName = input.ProjectName, Date = input.Date, WorkorderId = input.WorkorderId, SiteName = input.SiteName, SiteId = input.SiteId, BillingAddress = input.BillingAddress, LineItems = new List() }; if (input.LineItems != null && input.LineItems.Any()) { foreach (var li in input.LineItems) { quote.LineItems.Add(new QuotesLineItems { LineitemNumber = li.LineitemNumber, Lineitem = li.Lineitem, Descriptions = li.Descriptions }); } } await _quotesData.AddAsync(quote); } public async Task UpdateQuoteAsync(UpdateQuoteInput input, CancellationToken cancellationToken) { var quote = await _quotesData.GetByIdAsync(input.Id); if (quote == null) return false; quote.QuotId = input.QuotId; quote.ContactId = input.ContactId; quote.PO = input.PO; quote.TT = input.TT; quote.ProjectName = input.ProjectName; quote.Date = input.Date; quote.WorkorderId = input.WorkorderId; quote.SiteName = input.SiteName; quote.SiteId = input.SiteId; quote.BillingAddress = input.BillingAddress; await _quotesData.DeleteLineItemsByQuoteIdAsync(input.Id, cancellationToken); quote.LineItems ??= new List(); if (input.LineItems != null && input.LineItems.Any()) { foreach (var li in input.LineItems) { quote.LineItems.Add(new QuotesLineItems { LineitemNumber = li.LineitemNumber, Lineitem = li.Lineitem, Descriptions = li.Descriptions }); } } await _quotesData.UpdateAsync(quote); return true; } public async Task DeleteQuoteAsync(int id, CancellationToken cancellationToken) { var quote = await _quotesData.GetByIdAsync(id); if (quote == null) return false; await _quotesData.DeleteLineItemsByQuoteIdAsync(id, cancellationToken); await _quotesData.DeleteAsync(id); return true; } public async Task GetQuotesPagedAsync(int page, int pageSize, string search, CancellationToken cancellationToken) { var (items, totalCount) = await _quotesData.GetQuotesPagedAsync(page, pageSize, search, cancellationToken); return new PagedQuotesResult { Items = items.Select(item => new QuoteListItemDTO { Id = item.Id, QuoteId = item.QuoteId, WorkOrder = item.WorkOrder, ProjectName = item.ProjectName, TotalAmount = FormatAmount(item.TotalAmount), SiteName = item.SiteName, SiteId = item.SiteId, PO = item.PO, TT = item.TT, BillingAddress = item.BillingAddress, Status = item.Status, ContactId = item.ContactId, Date = item.Date, Lineitems = item.Lineitems.Select(MapLineItem).ToList() }).ToList(), TotalCount = totalCount }; } public async Task> GetWorkOrderQuoteDetailsAsync( int woId, CancellationToken cancellationToken) { var items = await _quotesData.GetWorkOrderQuoteDetailsAsync(woId, cancellationToken); return items.Select(item => new WorkOrderQuoteDetailDTO { Id = item.Id, PO = item.PO, TT = item.TT, Title = item.Title, Name = item.Name }).ToList(); } public async Task GetQuoteDetailsByIdAsync( int id, CancellationToken cancellationToken) { var item = await _quotesData.GetQuoteDetailsByIdAsync(id, cancellationToken); return item == null ? null : new QuoteDetailDTO { Id = item.Id, QuotId = item.QuotId, Date = item.Date, Name = item.Name, PO = item.PO, TT = item.TT, SiteName = item.SiteName, ProjectName = item.ProjectName, SiteId = item.SiteId, BillingAddress = item.BillingAddress, ShippingAddress = item.ShippingAddress, TotalAmount = FormatAmount(item.TotalAmount), Lineitems = item.Lineitems.Select(MapLineItem).ToList() }; } public async Task GetReportingAsync(CancellationToken cancellationToken) { return new QuoteReportingData { TotalCreatedWorkOrder = await _workOrderData.CountAsync(), TotalCompleted = await _workOrderData.CountByStatusAsync("Done"), TotalSev1 = await _workOrderData.CountByPriorityAsync("Sev 1"), TotalSev2 = await _workOrderData.CountByPriorityAsync("Sev 2"), TotalSev3 = await _workOrderData.CountByPriorityAsync("Sev 3"), TotalSev4 = await _workOrderData.CountByPriorityAsync("Sev 4"), TotalSev5 = await _workOrderData.CountByPriorityAsync("Sev 5") }; } private static QuoteLineItemDTO MapLineItem( SeaHaven.DataServices.Models.QuoteLineItemReadModel item) => new() { Descriptions = item.Descriptions, Lineitem = item.Lineitem, LineitemNumber = item.LineitemNumber }; private static string FormatAmount(decimal amount) => $"${amount}"; } }