<?php
/**
 * SMS Notification Abstract Class
 *
 * This abstract class defines the base class for all SMS Notifications.
 * Each SMS Notification class must extend this class and implement the send_sms function.
 *
 * @package miniorange-sms-order-notification-otp-verification/objects
 */

namespace WCSMSOTP\Objects;

use WCSMSOTP\Helper\MoPHPSessions;

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

/**
 * This class is used to define the base class of the SMS Notifications.
 * Each of the SMS Notification class needs to extend this class. They
 * will have to implement the send_sms function.
 */
if ( ! class_exists( 'SMSNotification' ) ) {
	/**
	 * SMS Notification Abstract Class
	 *
	 * This abstract class defines the base class for all SMS Notifications.
	 * Each SMS Notification class must extend this class and implement the send_sms function.
	 */
	abstract class SMSNotification {


		/**
		 * The settings page associated with the class
		 *
		 * @var string
		 */
		public $page;

		/**
		 * If the notification is enabled or not
		 *
		 * @var bool
		 */
		public $is_enabled;


		/**
		 * The header of the tooltip in the table
		 *
		 * @var string
		 */
		public $tool_tip_header;

		/**
		 * The body of the tooltip in the table
		 *
		 * @var string
		 */
		public $tool_tip_body;

		/**
		 * The recipient of the SMS Notification
		 *
		 * @var string
		 */
		public $recipient;

		/**
		 * The admin recipient of the SMS Notification
		 *
		 * @var string
		 */
		public $admin_recipient;

		/**
		 * The text body of the SMS
		 *
		 * @var string
		 */
		public $sms_body;

		/**
		 * The default SMS Body set if no sms
		 * text is explicitly specified
		 *
		 * @var string
		 */
		public $default_sms_body;

		/**
		 * The Title of the page
		 *
		 * @var string
		 */
		public $title;

		/**
		 * The available Tags that can be placed
		 * in the SMS body to dynamically add
		 * values to the messages being sent
		 *
		 * @var array
		 */
		public $available_tags;

		/**
		 * The Header of the Page
		 *
		 * @var string
		 */
		public $page_header;

		/**
		 * The description of the page
		 *
		 * @var string
		 */
		public $page_description;

		/**
		 * The phone input field identifier
		 *
		 * @var string
		 */
		public $phone_input;

		/**
		 * This determines who the notification is for: customer / admin / vendor etc.
		 *
		 * @var string
		 */
		public $notification_type;


		/**
		 * Constructor.
		 *
		 * Initializes a new instance of the SMSNotification class.
		 */
		public function __construct() {}

		/**
		 * Send SMS notification.
		 *
		 * This abstract method must be implemented by all child classes
		 * to send SMS notifications based on the provided arguments.
		 *
		 * @param array $args SMS notification arguments.
		 * @return void
		 */
		abstract public function send_sms( array $args );


		/**
		 * Set notification in session.
		 *
		 * Stores the notification page identifier in the session.
		 *
		 * @param string $notification_type Notification type/page identifier. If not provided or invalid, uses $this->page.
		 * @return void
		 */
		public function set_notif_in_session( $notification_type = '' ) {
			$page_to_store = '';
			if ( ! empty( $notification_type ) && is_string( $notification_type ) ) {
				$page_to_store = sanitize_key( $notification_type );
			} elseif ( ! empty( $this->page ) && is_string( $this->page ) ) {
				$page_to_store = sanitize_key( $this->page );
			}

			if ( ! empty( $page_to_store ) ) {
				MoPHPSessions::add_session_var( 'mo_addon_notif_type', $page_to_store );
			}
		}

		/**
		 * Setter function for is_enabled variable
		 *
		 * @param bool $is_enabled Whether the notification is enabled.
		 * @return SMSNotification Returns the current instance for method chaining.
		 */
		public function set_is_enabled( $is_enabled ) {
			$this->is_enabled = (bool) $is_enabled;
			return $this;
		}


		/**
		 * Setter function for recipient variable
		 *
		 * @param string $recipient SMS recipient.
		 * @return SMSNotification Returns the current instance for method chaining.
		 */
		public function set_recipient( $recipient ) {
			if ( is_string( $recipient ) ) {
				$this->recipient = sanitize_text_field( $recipient );
			}
			return $this;
		}

		/**
		 * Setter function for admin recipient variable
		 *
		 * @param string $admin_recipient SMS admin recipient.
		 * @return SMSNotification Returns the current instance for method chaining.
		 */
		public function set_admin_recipient( $admin_recipient ) {
			if ( is_string( $admin_recipient ) ) {
				$this->admin_recipient = sanitize_text_field( $admin_recipient );
			}
			return $this;
		}

		/**
		 * Setter function for SMS body variable
		 *
		 * @param string $sms_body SMS body content.
		 * @return SMSNotification Returns the current instance for method chaining.
		 */
		public function set_sms_body( $sms_body ) {
			if ( is_string( $sms_body ) ) {
				$this->sms_body = sanitize_textarea_field( $sms_body );
			}
			return $this;
		}
	}
}
