<?php
/**
 * Notification Settings Class
 *
 * This class is used to generate notification settings specific to email or SMS.
 * These settings are then passed to the gateway function to send notifications.
 *
 * @package miniorange-sms-order-notification-otp-verification/objects
 */

namespace WCSMSOTP\Objects;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'NotificationSettings' ) ) {
	/**
	 * Notification Settings Class
	 *
	 * This class is used to generate notification settings specific to email or SMS.
	 * These settings are then passed to the gateway function to send notifications.
	 */
	class NotificationSettings {

		/**
		 * Whether to send SMS notification.
		 *
		 * @var bool
		 */
		public $send_sms;

		/**
		 * Whether to send email notification.
		 *
		 * @var bool
		 */
		public $send_email;

		/**
		 * Phone number for SMS notification.
		 *
		 * @var string
		 */
		public $phone_number;

		/**
		 * From email address for email notification.
		 *
		 * @var string
		 */
		public $from_email;

		/**
		 * From name for email notification.
		 *
		 * @var string
		 */
		public $from_name;

		/**
		 * To email address for email notification.
		 *
		 * @var string
		 */
		public $to_email;

		/**
		 * To name for email notification.
		 *
		 * @var string
		 */
		public $to_name;

		/**
		 * Subject for email notification.
		 *
		 * @var string
		 */
		public $subject;

		/**
		 * BCC email address for email notification.
		 *
		 * @var string
		 */
		public $bcc_email;

		/**
		 * Message content for SMS or email notification.
		 *
		 * @var string
		 */
		public $message;

		/**
		 * Constructor.
		 *
		 * Creates notification settings for either SMS or email based on the number of arguments.
		 * For SMS: 2 arguments (phone_number, message).
		 * For Email: 5 arguments (from_email, from_name, to_email, subject, message).
		 */
		public function __construct() {
			$arg_count = func_num_args();
			if ( $arg_count < 2 ) {
				return;
			}
			if ( $arg_count < 4 ) {
				$phone_number = func_get_arg( 0 );
				$message      = func_get_arg( 1 );
				if ( is_string( $phone_number ) && is_string( $message ) ) {
					$this->create_sms_notification_settings( $phone_number, $message );
				}
			} else {
				$from_email = func_get_arg( 0 );
				$from_name  = func_get_arg( 1 );
				$to_email   = func_get_arg( 2 );
				$subject    = func_get_arg( 3 );
				$message    = func_get_arg( 4 );
				if ( is_string( $from_email ) && is_string( $from_name ) && is_string( $to_email ) && is_string( $subject ) && is_string( $message ) ) {
					$this->create_email_notification_settings( $from_email, $from_name, $to_email, $subject, $message );
				}
			}
		}


		/**
		 * Create Phone notification settings
		 *
		 * @param string $phone_number phone number.
		 * @param string $message message to send.
		 * @return void
		 */
		public function create_sms_notification_settings( $phone_number, $message ) {
			if ( ! is_string( $phone_number ) || ! is_string( $message ) ) {
				return;
			}
			$this->send_sms     = true;
			$this->phone_number = sanitize_text_field( $phone_number );
			$this->message      = sanitize_textarea_field( $message );
		}


		/**
		 * Create Email notification settings
		 *
		 * @param string $from_email from email param.
		 * @param string $from_name from name param.
		 * @param string $to_email to email param.
		 * @param string $subject subject of notification.
		 * @param string $message message content.
		 * @return void
		 */
		public function create_email_notification_settings( $from_email, $from_name, $to_email, $subject, $message ) {
			if ( ! is_string( $from_email ) || ! is_string( $from_name ) || ! is_string( $to_email ) || ! is_string( $subject ) || ! is_string( $message ) ) {
				return;
			}
			if ( ! is_email( $from_email ) || ! is_email( $to_email ) ) {
				return;
			}
			$this->send_email = true;
			$this->from_email = sanitize_email( $from_email );
			$this->from_name  = sanitize_text_field( $from_name );
			$this->to_email   = sanitize_email( $to_email );
			$this->to_name    = sanitize_email( $to_email );
			$this->subject    = sanitize_text_field( $subject );
			$this->bcc_email  = '';
			$this->message    = sanitize_textarea_field( $message );
		}
	}
}
