using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using _0_Framework.Application; using CompanyManagment.App.Contracts.CameraBugReport; using Company.Domain.CameraBugReportAgg; namespace CompanyManagment.Application { public class CameraBugReportApplication : ICameraBugReportApplication { private readonly ICameraBugReportRepository _repository; public CameraBugReportApplication(ICameraBugReportRepository repository) { _repository = repository; } // ============ Async Methods (MongoDB) ============ public async Task CreateAsync(CreateCameraBugReportCommand command) { var op = new OperationResult(); try { var bugReport = new CameraBugReport( command.Title, command.Description, command.UserEmail, command.DeviceModel, command.OsVersion, command.Manufacturer, command.BuildNumber, command.AppVersion, command.ScreenResolution, command.IsCharging, command.BatteryLevel, command.StorageInMB, command.MemoryInMB, command.NetworkType, command.Platform, command.DeviceId, command.PackageName, command.InstallTime, command.LastUpdateTime, command.Flavor, command.Type, command.Priority, command.AccountId, command.StackTrace ); // اضافه کردن لاگ‌ها if (command.Logs != null && command.Logs.Any()) { foreach (var log in command.Logs) { bugReport.AddLog(log); } } // اضافه کردن تصاویر if (command.Screenshots != null && command.Screenshots.Any()) { foreach (var screenshot in command.Screenshots) { bugReport.AddScreenshot(screenshot, $"screenshot_{Guid.NewGuid()}.jpg"); } } await _repository.CreateAsync(bugReport); return op.Succcedded(); } catch (Exception ex) { return op.Failed($"خطا در ثبت گزارش خرابی: {ex.Message}"); } } public async Task EditAsync(EditCameraBugReportCommand command) { var op = new OperationResult(); try { var bugReport = await _repository.GetByIdAsync(command.Id); if (bugReport == null) return op.Failed("گزارش خرابی یافت نشد."); bugReport.ChangePriority(command.Priority); bugReport.ChangeStatus(command.Status); await _repository.UpdateAsync(bugReport); return op.Succcedded(); } catch (Exception ex) { return op.Failed($"خطا در ویرایش گزارش خرابی: {ex.Message}"); } } public async Task DeleteAsync(Guid id) { var op = new OperationResult(); try { var exists = await _repository.IsExistAsync(id); if (!exists) return op.Failed("گزارش خرابی یافت نشد."); await _repository.DeleteAsync(id); return op.Succcedded(); } catch (Exception ex) { return op.Failed($"خطا در حذف گزارش خرابی: {ex.Message}"); } } public async Task> GetAllAsync(CameraBugReportSearchModel searchModel) { try { var skip = (searchModel.PageNumber - 1) * searchModel.PageSize; var bugReports = await _repository.FilterAsync( searchModel.Type, searchModel.Priority, searchModel.Status, searchModel.SearchTerm, skip, searchModel.PageSize ); return bugReports.Select(x => new CameraBugReportViewModel { Id = x.Id, Title = x.Title, Description = x.Description, UserEmail = x.UserEmail, AccountId = x.AccountId, DeviceModel = x.DeviceModel, AppVersion = x.AppVersion, Type = x.Type, Priority = x.Priority, Status = x.Status, CreationDate = x.CreationDate, UpdateDate = x.UpdateDate, LogsCount = x.Logs?.Count ?? 0, ScreenshotsCount = x.Screenshots?.Count ?? 0 }).ToList(); } catch (Exception ex) { throw new Exception($"خطا در دریافت لیست گزارش‌ها: {ex.Message}", ex); } } public async Task GetDetailsAsync(Guid id) { try { var bugReport = await _repository.GetByIdAsync(id); if (bugReport == null) return null; return new CameraBugReportDetailViewModel { Id = bugReport.Id, Title = bugReport.Title, Description = bugReport.Description, UserEmail = bugReport.UserEmail, AccountId = bugReport.AccountId, DeviceModel = bugReport.DeviceModel, OsVersion = bugReport.OsVersion, Platform = bugReport.Platform, Manufacturer = bugReport.Manufacturer, DeviceId = bugReport.DeviceId, ScreenResolution = bugReport.ScreenResolution, MemoryInMB = bugReport.MemoryInMB, StorageInMB = bugReport.StorageInMB, BatteryLevel = bugReport.BatteryLevel, IsCharging = bugReport.IsCharging, NetworkType = bugReport.NetworkType, AppVersion = bugReport.AppVersion, BuildNumber = bugReport.BuildNumber, PackageName = bugReport.PackageName, InstallTime = bugReport.InstallTime, LastUpdateTime = bugReport.LastUpdateTime, Flavor = bugReport.Flavor, Type = bugReport.Type, Priority = bugReport.Priority, Status = bugReport.Status, StackTrace = bugReport.StackTrace, CreationDate = bugReport.CreationDate, UpdateDate = bugReport.UpdateDate, Logs = bugReport.Logs?.Select(x => x.Message).ToList() ?? new List(), Screenshots = bugReport.Screenshots?.Select(x => new CameraBugReportScreenshotViewModel { FileName = x.FileName, UploadDate = x.UploadDate, Base64Data = x.Base64Data }).ToList() ?? new List() }; } catch (Exception ex) { throw new Exception($"خطا در دریافت جزئیات گزارش: {ex.Message}", ex); } } public async Task IsExistAsync(Guid id) { return await _repository.IsExistAsync(id); } // ============ Sync Methods (Backward Compatibility) ============ public OperationResult Create(CreateCameraBugReportCommand command) { try { return CreateAsync(command).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { return new OperationResult().Failed($"خطا: {ex.Message}"); } } public OperationResult Edit(EditCameraBugReportCommand command) { try { return EditAsync(command).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { return new OperationResult().Failed($"خطا: {ex.Message}"); } } public OperationResult Delete(Guid id) { try { return DeleteAsync(id).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { return new OperationResult().Failed($"خطا: {ex.Message}"); } } public List GetAll(CameraBugReportSearchModel searchModel) { try { return GetAllAsync(searchModel).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { throw new Exception($"خطا: {ex.Message}", ex); } } public CameraBugReportDetailViewModel GetDetails(Guid id) { try { return GetDetailsAsync(id).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { throw new Exception($"خطا: {ex.Message}", ex); } } public bool IsExist(Guid id) { try { return IsExistAsync(id).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { throw new Exception($"خطا: {ex.Message}", ex); } } } }