using Data; using Mailjet.Client; using Mailjet.Client.Resources; using Microsoft.AspNetCore.Mvc; using Model; using Services.Implemnetation; using Newtonsoft.Json.Linq; using Services.Interaces; using Services.EmailSend; using Microsoft.EntityFrameworkCore; namespace Web.Controllers { public class SubscriptionController : Controller { private readonly SurveyContext _context; private readonly IEmailServices _mailSerivces; private readonly IConfiguration _configuration; public SubscriptionController(SurveyContext context,IEmailServices mailSerivces,IConfiguration configuration) { _context = context; _mailSerivces = mailSerivces; _configuration = configuration; } // GET: /Subscription/Subscribe public IActionResult Subscribe() { return View(); } // POST: /Subscription/Subscribe [HttpPost] [ValidateAntiForgeryToken] public async Task Subscribe(Subscription subscription) { if(ModelState.IsValid) { try { string email = subscription.Email; string senderName = email.Substring(0, email.IndexOf('@')).ToUpper(); // Check if the email already exists var existingSubscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (existingSubscription != null) { TempData["error"] = "Email already subscribed."; return RedirectToAction("", "home"); } string subject = "Subscription Confirmation"; string confirmationPath = _configuration["Email:ConfirmEmailPath"]; // Retrieve the confirmation path from appsettings string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={email}"; // Construct the confirmation URL string body = $@" Email Confirmation

Dear {senderName},

Thank you for subscribing. We're thrilled to have you on board!

To confirm your subscription, please click the following button:

Confirm Subscription

If you have any questions or need assistance, feel free to contact us at help@seosoft.dk

Søren Eggert Lundsteen Olsen
SeoSoft ApS

Hovedgaden 3 Jordrup
Kolding 6064
Denmark
"; //string body = $@"Dear {senderName},

//Thank you for subscribing. We're thrilled to have you on board!

//To confirm your subscription, please click the following button:

//Confirm Subscription

//If you have any questions or need assistance, feel free to contact us at help@seosoft.dk

//
// Søren Eggert Lundsteen Olsen
// Seosoft ApS //
//
//
// Hovedgaden 3 // Jordrup
// Kolding 6064
// Denmark // // "; var newEmail = new EmailToSend(email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(newEmail); var subscriber = new Subscription { Name = senderName, Email = subscription.Email, IsSubscribed = false }; _context.Subscriptions.Add(subscriber); await _context.SaveChangesAsync(); TempData["success"] = "Subscription successful. Please confirm your email."; return RedirectToAction("", "home"); } catch (Exception ex) { TempData["error"] = "Failed to subscribe."; return RedirectToAction("", "home"); } } return RedirectToAction("", "home"); } [HttpGet] public async Task Confirmation(string email) { try { // Find the subscription with the provided email var subscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (subscription != null) { if (subscription.IsSubscribed) { // If IsSubscribed is already true, inform the user that the email is already confirmed ViewBag.Message = "Your email is already confirmed. Thank you!"; } else { // Update the IsSubscribed property to true subscription.IsSubscribed = true; _context.Subscriptions.Update(subscription); await _context.SaveChangesAsync(); // Send a "thank you" email to the user string subject = "Thank You for Confirming Your Subscription"; string body = $@" Email Confirmation

Dear {subscription.Name},

Thank you for confirming your subscription. You are now subscribed to our newsletter.

Søren Eggert Lundsteen Olsen
SeoSoft ApS

Hovedgaden 3 Jordrup
Kolding 6064
Denmark
"; var thankYouEmail = new EmailToSend(subscription.Email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(thankYouEmail); // Inform the user that the email has been confirmed ViewBag.Message = "Thank you for confirming your email. You are now subscribed!"; } return View(subscription); // You can return a view to show a confirmation message } else { ViewBag.Message = "You have been unsubscribed from our newsletter. Thank you!"; return View(subscription); } } catch (Exception ex) { // Log or handle the exception as needed return View("Error"); // You can return a view to show an error message } } [HttpGet] public async Task UnsubscribeConfirmation(string email) { try { // Find the subscription with the provided email var subscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (subscription != null) { if (subscription.IsSubscribed) { // Update the IsSubscribed property to false subscription.IsSubscribed = false; _context.Subscriptions.Remove(subscription); await _context.SaveChangesAsync(); // Inform the user that the email has been unsubscribed ViewBag.Message = "You have successfully unsubscribed from our newsletter. We're sorry to see you go"; // Optionally, send an email confirmation to the user string subject = "Unsubscription Confirmation"; string body = $@" Unsubscribe Confirmation

Unsubscribe Confirmation

You have successfully unsubscribed from our newsletter. We're sorry to see you go.


Søren Eggert Lundsteen Olsen
SeoSoft ApS

Hovedgaden 3
Jordrup
Kolding 6064
Denmark
"; var thankYouEmail = new EmailToSend(subscription.Email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(thankYouEmail); return View(subscription); // You can return a view to show a confirmation message } else { // If IsSubscribed is already false, inform the user that the email is already unsubscribed ViewBag.Message = "Your email is already unsubscribed. Thank you!"; return View(subscription); // You can return a view to show a message } } else { // Inform the user that the unsubscription process couldn't be completed ViewBag.Message = "You have been unsubscribed from our newsletter. subscribe first."; return View(subscription); // You can return a view to show an error message } } catch (Exception ex) { // Log or handle the exception as needed return View("Error"); // You can return a view to show an error message } } } }