const EmailClient = require("../core/EmailClient");
const TemplateManager = require("../core/TemplateManager");

/**
 * FeedbackEmailService handles all feedback-related email notifications
 * This service is responsible for:
 * - Sending feedback confirmation to users
 * - Sending feedback notifications to administrators
 */
class FeedbackEmailService {
  constructor() {
    this.emailClient = new EmailClient();
    this.templateManager = new TemplateManager();
    this.initialized = false;
  }

  /**
   * Initialize the feedback email service
   * @returns {Promise<void>}
   */
  async initialize() {
    if (this.initialized) return;

    await Promise.all([
      this.emailClient.initialize(),
      this.templateManager.initialize(),
    ]);

    this.initialized = true;
    console.log("Feedback Email Service initialized successfully");
  }

  /**
   * Send feedback confirmation email to user
   * @param {Object} user - User object
   * @param {string} user.firstName - User's first name
   * @param {string} user.email - User's email address
   * @param {Object} feedback - Feedback object
   * @param {string} feedback.feedbackText - The feedback content
   * @param {Date} feedback.createdAt - Feedback submission timestamp
   * @returns {Promise<{success: boolean, messageId?: string, error?: string}>}
   */
  async sendFeedbackConfirmation(user, feedback) {
    if (!this.initialized) {
      await this.initialize();
    }

    const submittedAt = new Date(feedback.createdAt).toLocaleString("en-US", {
      dateStyle: "long",
      timeStyle: "short",
    });

    const variables = {
      userName: user.firstName || "there",
      userEmail: user.email,
      feedbackText: feedback.feedbackText,
      submittedAt: submittedAt,
      year: new Date().getFullYear(),
    };

    const htmlContent = await this.templateManager.renderTemplate(
      "feedbackConfirmationToUser",
      variables
    );

    return await this.emailClient.sendEmail(
      user.email,
      "Thank You for Your Feedback - Village Share",
      htmlContent
    );
  }

  /**
   * Send feedback notification to admin
   * @param {Object} user - User object who submitted feedback
   * @param {string} user.firstName - User's first name
   * @param {string} user.lastName - User's last name
   * @param {string} user.email - User's email address
   * @param {string} user.id - User's ID
   * @param {Object} feedback - Feedback object
   * @param {string} feedback.id - Feedback ID
   * @param {string} feedback.feedbackText - The feedback content
   * @param {string} [feedback.url] - URL where feedback was submitted
   * @param {string} [feedback.userAgent] - User's browser user agent
   * @param {Date} feedback.createdAt - Feedback submission timestamp
   * @returns {Promise<{success: boolean, messageId?: string, error?: string}>}
   */
  async sendFeedbackNotificationToAdmin(user, feedback) {
    if (!this.initialized) {
      await this.initialize();
    }

    const adminEmail =
      process.env.FEEDBACK_EMAIL || process.env.CUSTOMER_SUPPORT_EMAIL;

    if (!adminEmail) {
      console.warn("No admin email configured for feedback notifications");
      return { success: false, error: "No admin email configured" };
    }

    const submittedAt = new Date(feedback.createdAt).toLocaleString("en-US", {
      dateStyle: "long",
      timeStyle: "short",
    });

    const variables = {
      userName: `${user.firstName} ${user.lastName}`.trim() || "Unknown User",
      userEmail: user.email,
      userId: user.id,
      feedbackText: feedback.feedbackText,
      feedbackId: feedback.id,
      url: feedback.url || "Not provided",
      userAgent: feedback.userAgent || "Not provided",
      submittedAt: submittedAt,
      year: new Date().getFullYear(),
    };

    const htmlContent = await this.templateManager.renderTemplate(
      "feedbackNotificationToAdmin",
      variables
    );

    return await this.emailClient.sendEmail(
      adminEmail,
      `New Feedback from ${user.firstName} ${user.lastName}`,
      htmlContent
    );
  }
}

module.exports = FeedbackEmailService;
