using Data.SeaHavenIndustries; using SeaHaven.DataServices.Interfaces; using SeaHaven.Services.DTOs; using SeaHaven.Services.Interfaces; namespace SeaHaven.Services.Implementation { public class WorkOrderService : IWorkOrderService { private readonly IWorkOrderDataService _workOrderDataService; public WorkOrderService(IWorkOrderDataService workOrderDataService) { _workOrderDataService = workOrderDataService; } public async Task GetWorkOrderByIdAsync(int id) { var workOrder = await _workOrderDataService.GetByIdAsync(id); return workOrder == null ? null : MapToDTO(workOrder); } public async Task GetWorkOrderByIdWithDetailsAsync(int id) { var workOrder = await _workOrderDataService.GetByIdWithDetailsAsync(id); return workOrder == null ? null : MapToDTO(workOrder); } public async Task> GetAllWorkOrdersAsync() { var workOrders = await _workOrderDataService.GetAllAsync(); return workOrders.Select(MapToDTO); } public async Task> GetWorkOrdersPagedAsync(int page, int pageSize, string? search = null, string? status = null, string? priority = null) { var (items, totalCount) = await _workOrderDataService.GetPagedAsync(page, pageSize, search, status, priority); return new PagedResult { Items = items.Select(MapToDTO), TotalCount = totalCount, Page = page, PageSize = pageSize }; } public async Task CreateWorkOrderAsync(CreateWorkOrderDTO dto, string userId) { var workOrder = new WorkOrder { WorkerOrderTitle = dto.WorkerOrderTitle, Description = dto.Description, Status = dto.Status ?? "Open", Priority = dto.Priority, LocationId = dto.LocationId, AssignTo = dto.AssignTo, DueDate = dto.DueDate, createdby = userId, CreatedDate = DateTime.Now }; // Generate internal number workOrder.InternalWONumber = await _workOrderDataService.GenerateNextInternalWONumberAsync(); var savedWorkOrder = await _workOrderDataService.AddAsync(workOrder); return MapToDTO(savedWorkOrder); } public async Task UpdateWorkOrderAsync(int id, UpdateWorkOrderDTO dto, string userId) { var existingWorkOrder = await _workOrderDataService.GetByIdAsync(id); if (existingWorkOrder == null) throw new KeyNotFoundException($"WorkOrder with ID {id} not found"); if (!string.IsNullOrWhiteSpace(dto.WorkerOrderTitle)) existingWorkOrder.WorkerOrderTitle = dto.WorkerOrderTitle; if (dto.Description != null) existingWorkOrder.Description = dto.Description; if (dto.Status != null) existingWorkOrder.Status = dto.Status; if (dto.Priority != null) existingWorkOrder.Priority = dto.Priority; if (dto.LocationId.HasValue) existingWorkOrder.LocationId = dto.LocationId; if (dto.AssignTo != null) existingWorkOrder.AssignTo = dto.AssignTo; if (dto.DueDate.HasValue) existingWorkOrder.DueDate = dto.DueDate; if (dto.CompletedDate.HasValue) existingWorkOrder.CompletedDate = dto.CompletedDate; existingWorkOrder.LastModificationTime = DateTime.Now; await _workOrderDataService.UpdateAsync(existingWorkOrder); return MapToDTO(existingWorkOrder); } public async Task DeleteWorkOrderAsync(int id, string userId) { if (!await _workOrderDataService.ExistsAsync(id)) throw new KeyNotFoundException($"WorkOrder with ID {id} not found"); await _workOrderDataService.DeleteAsync(id); } public async Task WorkOrderExistsAsync(int id) { return await _workOrderDataService.ExistsAsync(id); } public async Task GenerateNextInternalWONumberAsync() { return await _workOrderDataService.GenerateNextInternalWONumberAsync(); } public async Task GetTotalWorkOrderCountAsync() { return await _workOrderDataService.CountAsync(); } private static WorkOrderDTO MapToDTO(WorkOrder workOrder) { return new WorkOrderDTO { Id = workOrder.Id, InternalWONumber = workOrder.InternalWONumber, WorkerOrderTitle = workOrder.WorkerOrderTitle, Description = workOrder.Description, Status = workOrder.Status, Priority = workOrder.Priority, LocationId = workOrder.LocationId, LocationName = workOrder.Locations?.Name, AssignTo = workOrder.AssignTo, DueDate = workOrder.DueDate, CompletedDate = workOrder.CompletedDate, CreatedDate = workOrder.CreatedDate, CreatedBy = workOrder.createdby, ModifiedDate = workOrder.LastModificationTime, ModifiedBy = null // Not tracked in FullAuditEntity }; } } }