using FrameworkDAL.DTO.MessageHub.MessageHubGenerator; using GB5Shared.DTO.Framework.Login; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; namespace FrameworkBLL.MessageHub.MessageHubPlatforms { public class SMSPlatform : IMessageHubPlatform.ISMSPlatform { private readonly ILogger _logger; private readonly HttpClient _httpClient; private readonly SMSSettings _settings; public SMSPlatform( ILogger logger, HttpClient httpClient, IOptions settings) { _logger = logger; _httpClient = httpClient; _settings = settings.Value; } public async Task SendSmsConversation( MessageHubDTO dto, LoginDTO loginContext, CancellationToken ct) { if (ct.IsCancellationRequested) throw new OperationCanceledException(); try { var payload = new { apiKey = _settings.ApiKey, sender = _settings.SenderId, to = dto.Recipient, message = dto.MessageText }; var json = JsonSerializer.Serialize(payload); var response = await _httpClient.PostAsync( _settings.ApiUrl, new StringContent(json, Encoding.UTF8, "application/json"), ct); if (!response.IsSuccessStatusCode) { var error = await response.Content.ReadAsStringAsync(); throw new Exception($"SMS API failed: {error}"); } _logger.LogInformation( "SMS sent successfully | To={Recipient} | User={UserId}", dto.Recipient, loginContext?.UserId); } catch (Exception ex) { _logger.LogError(ex, "SMS sending failed | To={Recipient}", dto.Recipient); throw; } } } #region Settings Class public class SMSSettings { public string ApiUrl { get; set; } public string ApiKey { get; set; } public string SenderId { get; set; } public string FromNumber { get; set; } = ""; } #endregion }