using FrameworkDAL.DTO.MessageHub.MessageHubGenerator; using GB5Shared.DTO.Framework.Login; using Microsoft.Extensions.Options; using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; namespace FrameworkBLL.MessageHub.MessageHubPlatforms { public class TeamsPlatform : IMessageHubPlatform.ITeamsPlatform { private readonly IHttpClientFactory _httpClientFactory; private readonly MessageHubSettings _settings; public TeamsPlatform( IHttpClientFactory httpClientFactory, IOptions settings) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); } // ========================================================== // ✅ TEMPLATE - Teams MessageHubEngine // ========================================================== public async Task SendTeamsTemplate( MessageHubDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) return Fail("Template DTO cannot be null"); if (login == null) return Fail("Login info missing"); var payload = new { text = $"📢 TEMPLATE MESSAGE\n\n{dto.MessageText}\n\nSent By: {login.UserName}" }; try { var client = _httpClientFactory.CreateClient(); var response = await client.PostAsync( _settings.Teams.WebhookUrl, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"), ct); var responseContent = await response.Content.ReadAsStringAsync(ct); return new MessageHubResponseDTO { Success = response.IsSuccessStatusCode, Message = response.IsSuccessStatusCode ? "Teams Template message sent successfully" : "Teams Template message failed", ResponseData = responseContent }; } catch (Exception ex) { return Fail(ex.Message); } } // ========================================================== // ✅ CONVERSATION - Teams MessageHubConversation // ========================================================== public async Task SendTeamsConversation( MessageHubDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) return Fail("Conversation DTO cannot be null"); if (login == null) return Fail("Login info missing"); var payload = new { text = dto.MessageText // Pure conversation text }; try { var client = _httpClientFactory.CreateClient(); var response = await client.PostAsync( _settings.Teams.WebhookUrl, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"), ct); var responseContent = await response.Content.ReadAsStringAsync(ct); return new MessageHubResponseDTO { Success = response.IsSuccessStatusCode, Message = response.IsSuccessStatusCode ? "Teams Conversation message sent successfully" : "Teams Conversation message failed", ResponseData = responseContent }; } catch (Exception ex) { return Fail(ex.Message); } } // ========================================================== // PRIVATE HELPER // ========================================================== private static MessageHubResponseDTO Fail(string reason) => new() { Success = false, Message = reason }; } }