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 { /// /// Account service implementation /// Contains business logic, validation, and orchestration for Account operations /// public class AccountService : IAccountService { private readonly IAccountDataService _accountDataService; private readonly ICreateAccountValidation _createValidator; private readonly IUpdateAccountValidation _updateValidator; public AccountService( IAccountDataService accountDataService, ICreateAccountValidation createValidator, IUpdateAccountValidation updateValidator) { _accountDataService = accountDataService; _createValidator = createValidator; _updateValidator = updateValidator; } public async Task GetAccountByIdAsync(int id) { var account = await _accountDataService.GetByIdAsync(id); return account == null ? null : MapToDTO(account); } public async Task GetAccountByIdWithDetailsAsync(int id) { var account = await _accountDataService.GetByIdWithDetailsAsync(id); return account == null ? null : MapToDTO(account); } public async Task> GetAllAccountsAsync() { var accounts = await _accountDataService.GetAllAsync(); return accounts.Select(MapToDTO); } public async Task> GetAccountsPagedAsync(int page, int pageSize, string? search = null) { var (items, totalCount) = await _accountDataService.GetPagedAsync(page, pageSize, search); return new PagedResult { Items = items.Select(MapToDTO).ToList(), TotalCount = totalCount, Page = page, PageSize = pageSize }; } public async Task CreateAccountAsync(CreateAccountDTO dto, string userId) { var validationResult = await _createValidator.ValidateAsync(dto); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var account = new Accounts { Name = dto.Name, Phone = dto.Phone, Email = dto.Email, Website = dto.Website, CreatedDate = DateTime.UtcNow, createdby = userId }; var created = await _accountDataService.AddAsync(account); return MapToDTO(created); } public async Task UpdateAccountAsync(int id, UpdateAccountDTO dto, string userId) { var validationResult = await _updateValidator.ValidateAsync(dto); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var account = await _accountDataService.GetByIdAsync(id); if (account == null) throw new InvalidOperationException($"Account with ID {id} not found"); if (dto.Name != null) account.Name = dto.Name; if (dto.Phone != null) account.Phone = dto.Phone; if (dto.Email != null) account.Email = dto.Email; if (dto.Website != null) account.Website = dto.Website; account.LastModificationTime = DateTime.UtcNow; if (int.TryParse(userId, out int userIdInt)) account.LastModifierUserId = userIdInt; await _accountDataService.UpdateAsync(account); return MapToDTO(account); } public async Task DeleteAccountAsync(int id, string userId) { var account = await _accountDataService.GetByIdAsync(id); if (account == null) throw new InvalidOperationException($"Account with ID {id} not found"); account.IsDeleted = true; account.DeleterUserId = userId; account.DeletionTime = DateTime.UtcNow; await _accountDataService.UpdateAsync(account); } public async Task AccountExistsAsync(int id) { return await _accountDataService.ExistsAsync(id); } public async Task GetTotalAccountCountAsync() { return await _accountDataService.CountAsync(); } private AccountDTO MapToDTO(Accounts account) { return new AccountDTO { Id = account.Id, Name = account.Name, Type = account.Type, Phone = account.Phone, Email = account.Email, Website = account.Website, CreatedDate = account.CreatedDate, CreatedBy = account.createdby }; } } }