using Data.SeaHavenIndustries; using FluentValidation; using SeaHaven.DataServices.Interfaces; using SeaHaven.Services.DTOs; using SeaHaven.Services.Interfaces; using SeaHaven.Services.Validation; namespace SeaHaven.Services.Implementation { /// /// Dispatch service implementation /// Contains business logic, validation, and orchestration for Dispatch operations /// public class DispatchService : IDispatchService { private readonly IDispatchDataService _dispatchDataService; private readonly ICreateDispatchValidation _createValidator; private readonly IUpdateDispatchValidation _updateValidator; public DispatchService( IDispatchDataService dispatchDataService, ICreateDispatchValidation createValidator, IUpdateDispatchValidation updateValidator) { _dispatchDataService = dispatchDataService; _createValidator = createValidator; _updateValidator = updateValidator; } public async Task GetDispatchByIdAsync(int id) { var dispatch = await _dispatchDataService.GetByIdAsync(id); return dispatch == null ? null : MapToDTO(dispatch); } public async Task GetDispatchByIdWithDetailsAsync(int id) { var dispatch = await _dispatchDataService.GetByIdWithDetailsAsync(id); return dispatch == null ? null : MapToDTO(dispatch); } public async Task> GetAllDispatchesAsync() { var dispatches = await _dispatchDataService.GetAllAsync(); return dispatches.Select(MapToDTO); } public async Task> GetDispatchesByVendorIdAsync(int vendorId) { var dispatches = await _dispatchDataService.GetByVendorIdAsync(vendorId); return dispatches.Select(MapToDTO); } public async Task> GetDispatchesPagedAsync(int page, int pageSize, string? search = null, string? status = null) { var (items, totalCount) = await _dispatchDataService.GetPagedAsync(page, pageSize, search, status); return new PagedResult { Items = items.Select(MapToDTO).ToList(), TotalCount = totalCount, Page = page, PageSize = pageSize }; } public async Task CreateDispatchAsync(CreateDispatchDTO dto, string userId) { var validationResult = await _createValidator.ValidateAsync(dto); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var dispatchNumber = await GenerateNextDispatchNumberAsync(); var dispatch = new Dispatch { VendorId = dto.VendorId, ScheduledDate = dto.ScheduledDate, Status = dto.Status ?? "Pending", DispatchNumber = dispatchNumber, CreatedDate = DateTime.UtcNow, createdby = userId }; var created = await _dispatchDataService.AddAsync(dispatch); return MapToDTO(created); } public async Task UpdateDispatchAsync(int id, UpdateDispatchDTO dto, string userId) { var validationResult = await _updateValidator.ValidateAsync(dto); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var dispatch = await _dispatchDataService.GetByIdAsync(id); if (dispatch == null) throw new InvalidOperationException($"Dispatch with ID {id} not found"); if (dto.VendorId.HasValue) dispatch.VendorId = dto.VendorId.Value; if (dto.ScheduledDate.HasValue) dispatch.ScheduledDate = dto.ScheduledDate; if (dto.CompletedDate.HasValue) dispatch.CompletedDate = dto.CompletedDate; if (dto.Status != null) dispatch.Status = dto.Status; dispatch.LastModificationTime = DateTime.UtcNow; if (int.TryParse(userId, out int userIdInt)) dispatch.LastModifierUserId = userIdInt; await _dispatchDataService.UpdateAsync(dispatch); return MapToDTO(dispatch); } public async Task DeleteDispatchAsync(int id, string userId) { var dispatch = await _dispatchDataService.GetByIdAsync(id); if (dispatch == null) throw new InvalidOperationException($"Dispatch with ID {id} not found"); dispatch.IsDeleted = true; dispatch.DeleterUserId = userId; dispatch.DeletionTime = DateTime.UtcNow; await _dispatchDataService.UpdateAsync(dispatch); } public async Task DispatchExistsAsync(int id) { return await _dispatchDataService.ExistsAsync(id); } public async Task GenerateNextDispatchNumberAsync() { var count = await _dispatchDataService.CountAsync(); return $"DISP-{(count + 1):D6}"; } public async Task GetTotalDispatchCountAsync() { return await _dispatchDataService.CountAsync(); } private DispatchDTO MapToDTO(Dispatch dispatch) { return new DispatchDTO { Id = dispatch.Id, DispatchNumber = dispatch.DispatchNumber, Status = dispatch.Status, VendorId = dispatch.VendorId, VendorName = dispatch.Vendor?.CompanyName, ScheduledDate = dispatch.ScheduledDate, CompletedDate = dispatch.CompletedDate, CreatedDate = dispatch.CreatedDate, CreatedBy = dispatch.createdby }; } } }