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 { /// /// PM Schedule service implementation /// Contains business logic, validation, and orchestration for PM Schedule operations /// public class PMScheduleService : IPMScheduleService { private readonly IPMScheduleDataService _pmScheduleDataService; private readonly ICreatePMScheduleValidation _createValidator; private readonly IUpdatePMScheduleValidation _updateValidator; public PMScheduleService( IPMScheduleDataService pmScheduleDataService, ICreatePMScheduleValidation createValidator, IUpdatePMScheduleValidation updateValidator) { _pmScheduleDataService = pmScheduleDataService; _createValidator = createValidator; _updateValidator = updateValidator; } public async Task GetPMScheduleByIdAsync(int id) { var pmSchedule = await _pmScheduleDataService.GetByIdAsync(id); return pmSchedule == null ? null : MapToDTO(pmSchedule); } public async Task GetPMScheduleByIdWithDetailsAsync(int id) { var pmSchedule = await _pmScheduleDataService.GetByIdWithDetailsAsync(id); return pmSchedule == null ? null : MapToDTO(pmSchedule); } public async Task> GetAllPMSchedulesAsync() { var pmSchedules = await _pmScheduleDataService.GetAllWithDetailsAsync(); return pmSchedules.Select(MapToDTO); } public async Task> GetPMSchedulesByLocationIdAsync(int locationId) { var pmSchedules = await _pmScheduleDataService.GetAllWithDetailsAsync(); return pmSchedules.Where(p => p.LocationId == locationId).Select(MapToDTO); } public async Task> GetPMSchedulesPagedAsync(int page, int pageSize, string? search = null) { var (items, totalCount) = await _pmScheduleDataService.GetPagedAsync(page, pageSize, search); return new PagedResult { Items = items.Select(MapToDTO).ToList(), TotalCount = totalCount, Page = page, PageSize = pageSize }; } public async Task CreatePMScheduleAsync(CreatePMScheduleDTO dto, string userId) { var validationResult = await _createValidator.ValidateAsync(dto); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var pmSchedule = new PMSchedules { Name = dto.Name, Frequency = dto.Frequency, StartDate = dto.StartDate, IsActive = dto.IsActive, AssetId = dto.AssetId, LocationId = dto.LocationId, CreatedDate = DateTime.UtcNow, createdby = userId }; var created = await _pmScheduleDataService.AddAsync(pmSchedule); return MapToDTO(created); } public async Task UpdatePMScheduleAsync(int id, UpdatePMScheduleDTO dto, string userId) { var validationResult = await _updateValidator.ValidateAsync(dto); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var pmSchedule = await _pmScheduleDataService.GetByIdAsync(id); if (pmSchedule == null) throw new InvalidOperationException($"PM Schedule with ID {id} not found"); if (dto.Name != null) pmSchedule.Name = dto.Name; if (dto.Frequency != null) pmSchedule.Frequency = dto.Frequency; if (dto.StartDate.HasValue) pmSchedule.StartDate = dto.StartDate.Value; if (dto.IsActive.HasValue) pmSchedule.IsActive = dto.IsActive.Value; if (dto.AssetId.HasValue) pmSchedule.AssetId = dto.AssetId; if (dto.LocationId.HasValue) pmSchedule.LocationId = dto.LocationId; pmSchedule.LastModificationTime = DateTime.UtcNow; if (int.TryParse(userId, out int userIdInt)) pmSchedule.LastModifierUserId = userIdInt; await _pmScheduleDataService.UpdateAsync(pmSchedule); return MapToDTO(pmSchedule); } public async Task DeletePMScheduleAsync(int id, string userId) { var pmSchedule = await _pmScheduleDataService.GetByIdAsync(id); if (pmSchedule == null) throw new InvalidOperationException($"PM Schedule with ID {id} not found"); pmSchedule.IsDeleted = true; pmSchedule.DeleterUserId = userId; pmSchedule.DeletionTime = DateTime.UtcNow; await _pmScheduleDataService.UpdateAsync(pmSchedule); } public async Task PMScheduleExistsAsync(int id) { return await _pmScheduleDataService.ExistsAsync(id); } public async Task GetTotalPMScheduleCountAsync() { return await _pmScheduleDataService.CountAsync(); } private PMScheduleDTO MapToDTO(PMSchedules pmSchedule) { return new PMScheduleDTO { Id = pmSchedule.Id, Name = pmSchedule.Name, Frequency = pmSchedule.Frequency, StartDate = pmSchedule.StartDate, IsActive = pmSchedule.IsActive, AssetId = pmSchedule.AssetId, AssetName = pmSchedule.Asset?.Name, LocationId = pmSchedule.LocationId, LocationName = pmSchedule.Location?.Name, CreatedDate = pmSchedule.CreatedDate }; } } }