<?php

namespace SGC\eGiftCard\Services;

defined('ABSPATH') || exit;

use SGC\eGiftCard\Foundation\AppHelper;

/**
 * Class Mailer
 *
 * Handles the sending of eGift Card emails.
 */
class Mailer
{
    use AppHelper;

    /**
     * Get the "from" name for the email.
     *
     * @return string The "from" name.
     */
    public static function get_from_name()
    {
        $from_name = get_option('woocommerce_email_from_name');
        return wp_specialchars_decode(esc_html($from_name), ENT_QUOTES);
    }

    /**
     * Get the "from" email address.
     *
     * @return string The "from" email address.
     */
    public static function get_from_address()
    {
        $from_email = get_option('woocommerce_email_from_address');
        return sanitize_email($from_email);
    }

    /**
     * Send the gift email.
     *
     * @param array $email_data The data for the email.
     * @return void
     */
    public static function send_gift_email($email_data)
    {
        // Sanitize incoming data
        $receiver_name    = sanitize_text_field($email_data['receiver_name']);
        $sender_name      = sanitize_text_field($email_data['sender_name']);
        $selected_image   = esc_url($email_data['selected_image']);
        $gift_message     = wp_kses_post($email_data['gift_message']);
        $gift_amount      = floatval($email_data['gift_amount']);
        $brand_name       = sanitize_text_field($email_data['brand_name']);
        $brand_logo       = esc_url($email_data['brand_logo']);
        $gift_code        = sanitize_text_field($email_data['gift_code']);
        $to_email         = sanitize_email($email_data['to_email']);
        $redeem_gift_page = esc_url($email_data['redeem_gift_page']);

        $currency_symbols = self::get_sgc_gift_card_currency_symbols();
        $currency = get_option('woocommerce_currency');
        $currency_symbol = $currency_symbols[$currency] ?? '$';

        // Start output buffering and include the email template
        ob_start();
        include_once(SMART_GIFT_CARD_PLUGIN_DIR . '/app/views/email.php');
        $message = ob_get_clean();
      
        // Set email subject
        $subject = "A Special Gift Just for You!";

        // Get headers for the email
        $headers = self::get_email_headers();

        // Attempt to send the email
        wp_mail( $to_email, $subject, $message, $headers );
    }

    public static function send_email_to_gift_sender($email_data)
    {
        [
            'receiver_name'    => $receiver_name,
            'receiver_email'   => $receiver_email,
            'sender_name'      => $sender_name,
            'purchased_date'   => $purchased_date,
            'delivery_date'    => $delivery_date,
            'selected_image'   => $selected_image,
            'gift_message'     => $gift_message,
            'gift_amount'      => $gift_amount,
            'brand_name'       => $brand_name,
            'brand_logo'       => $brand_logo,
            'to_email'         => $to_email
        ] = $email_data;

        $currency_symbols = self::get_sgc_gift_card_currency_symbols();
        $currency = get_option('woocommerce_currency');
        $currency_symbol = isset($currency_symbols[$currency]) ? $currency_symbols[$currency] : '';

        // Start output buffering
        ob_start();

        // Include the email template file, passing the data into it
        include_once(dirname(SMART_GIFT_CARD_PLUGIN_DIR) . '/app/views/sender-email.php');

        // Get the contents of the buffer and store it in the $message variable
        $message = ob_get_clean();

        // Set email subject
        $subject = "Your Purchased Gift Card Has Been Successfully Delivered!";

        // Set headers for HTML email
        $headers = array(
            'Content-Type: text/html; charset=UTF-8',
            'Reply-To: ' . self::get_from_name() . ' <' . self::get_from_address() . '>',
            'From: ' . self::get_from_name() . ' <' . self::get_from_address() . '>'
        );

        // Attempt to send the email
        wp_mail( $to_email, $subject, $message, $headers );
    }

    /**
     * Get the email headers.
     *
     * @return array The email headers.
     */
    private static function get_email_headers()
    {
        return [
            'Content-Type: text/html; charset=UTF-8',
            'Reply-To: ' . self::get_from_name() . ' <' . self::get_from_address() . '>',
            'From: ' . self::get_from_name() . ' <' . self::get_from_address() . '>',
        ];
    }
}