<?php
/**
 * Activity Guard – Real Time Notifier to Slack Rest Route.
 *
 * This class is used to response and all rest route works.
 *
 * @package WPNTS\Inc
 */

namespace WPNTS\Inc;

use WPNTS\Inc\wpnts_nts_fs; // phpcs:ignore
use WPNTS\Inc\Activate; // phpcs:ignore
use WPNTS\Inc\Deactivate; // phpcs:ignore
use WPNTS\Inc\Database\DB; // phpcs:ignore
use WPCF7_ContactForm; //phpcs:ignore
use WPCF7_Submission; //phpcs:ignore
use WPNTS\Inc\Formflow; //phpcs:ignore
use WPNTS\Inc\SlackAttachment; // phpcs:ignore
use WPNTS\Inc\SlackChannelRouter; // phpcs:ignore

defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');
/**
 * Update used to rest route created.
 *
 * @since 1.0.0
 */
class Integration {

	/**
	 * Construct method.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$wpnts_db_instance = new DB();
		$is_active = $wpnts_db_instance->is_pro_active();
		$schedules_int = get_option( 'wpntswebhook_integration_settings');
		$schedules_interval = json_decode($schedules_int);

		$formflow_webhook = $schedules_interval->formflow_webhook ?? '';
		$formflow_active_notice = $schedules_interval->formflow_active_notice ?? 'false';
		$formflow_get_leads_with_notice = $schedules_interval->formflow_get_leads_with_notice ?? 'false';

		$cf7_webhook = $schedules_interval->cf7_webhook ?? '';
		$cf7_active_notice = $schedules_interval->cf7_active_notice ?? 'false';
		$cf7_get_leads_with_notice = $schedules_interval->cf7_get_leads_with_notice ?? 'false';

		if ( true === $cf7_active_notice || true === $cf7_get_leads_with_notice ) {
			add_action('wpcf7_mail_sent', [ $this, 'wpnts_cf7_integration' ] );
		}

		if ( true === $formflow_active_notice || true === $formflow_get_leads_with_notice ) {
			add_action('formflow_form_submit', [ $this, 'form_submit_action' ], 10, 1);
		}
	}

	/**
	 * Integrates Contact Form 7 submissions with Slack notifications and logs activities.
	 *
	 * This function handles the Contact Form 7 submission process, formats the submitted data,
	 * and sends notifications to Slack based on the configuration. It also logs the submission
	 * details into the database.
	 *
	 * @param WPCF7_ContactForm $contact_form The Contact Form 7 object being submitted.
	 * @return void
	 */
	public function wpnts_cf7_integration( $contact_form ) {

		$schedules_int = get_option( 'wpntswebhook_integration_settings');
		$schedules_interval = json_decode($schedules_int);
		$cf7_active_notice = $schedules_interval->cf7_active_notice ?? 'false';
		$cf7_get_leads_with_notice = $schedules_interval->cf7_get_leads_with_notice ?? 'false';

		// Get the site URL, including child site for multisite.
		$attachmentHandler = new SlackAttachment();
		$site_url = $attachmentHandler->getSiteURLAddOption();

		$message = ":yawning_face: User $user_login has logged in.  $site_url";

		$submission = WPCF7_Submission::get_instance();
		if ( $submission ) {
			$posted_data = $submission->get_posted_data();
			$contact_forms_id = $contact_form->id();

			$current_time = current_time('Y-m-d H:i:s');

			$formatted_data = '';
			foreach ( $posted_data as $key => $value ) {
				$formatted_data .= "*$key:* $value\n";
			}

			// Send attachments to Slack.
			$attachments = [
				[
					'fallback' => 'Form Data',
					'color'    => '#36a64f',
					'text'     => "New Form Submission: Form ID - $contact_forms_id\n*Form Data:*\n$formatted_data",
					'mrkdwn_in' => [ 'text' ],
				],
			];

			// Send to Slack if cf7_get_leads_with_notice is true.
			if ( true === $cf7_get_leads_with_notice ) {
				$message = "Contact Form 7 Form (ID: $contact_forms_id) submitted at $current_time\n";
				$message .= "Form ID - $contact_forms_id\n*Form Data:*\n$formatted_data";
				$this->wpnts_notify_slack($message);
			} elseif ( true === $cf7_active_notice ) {
				$message = "Contact Form 7 Form (ID: $contact_forms_id) submitted at $current_time";
				$this->wpnts_notify_slack($message);
			}

			$log_message = "Contact Form 7 ID: $contact_forms_id submitted at $current_time  <br> $site_url";
			$severity = 'Normal';
			$event_type = 'Submission';
			$time = current_time( 'mysql' );

			$ip = $attachmentHandler->get_user_ip();
			$attachmentHandler->log_activity_to_db( $severity, $time, $ip, $display_name = 'CF7', $event_type, $log_message  );

		}
	}

	/**
	 * Processes Formflow form submissions, sends Slack notifications, and logs activities.
	 *
	 * This function handles the submission process for Formflow forms, formats the submitted data,
	 * and sends notifications to Slack based on the configuration. It also logs the submission
	 * details into the database.
	 *
	 * @param array $contact The submitted form data.
	 * @return void
	 */
	public function form_submit_action( $contact ) {

		$schedules_int = get_option( 'wpntswebhook_integration_settings');
		$schedules_interval = json_decode($schedules_int);
		$formflow_active_notice = $schedules_interval->formflow_active_notice ?? 'false';
		$formflow_get_leads_with_notice = $schedules_interval->formflow_get_leads_with_notice ?? 'false';

		// Get the site URL, including child site for multisite.
		$attachmentHandler = new SlackAttachment();
		$site_url = $attachmentHandler->getSiteURLAddOption();

		$formatted_data = '';
		foreach ( $contact['fields'] as $key => $value ) {
			$formatted_data .= "*$key:* $value\n";
		}

		$current_time = current_time( 'Y-m-d H:i:s' );

		// Send attachments to Slack.
		$attachments = [
			[
				'fallback'   => 'Form Data',
				'color'      => '#36a64f',
				'text'       => "New Formflow Submission: Form ID - {$contact['form_id']}\n*Form Data:*\n$formatted_data",
				'mrkdwn_in'  => [ 'text' ],
			],
		];

		// Send to Slack if formflow_get_leads_with_notice is true.
		if ( true === $formflow_get_leads_with_notice ) {
			$message = "Formflow Form (ID: {$contact['form_id']}) submitted at $current_time\n  $site_url";
			$message .= "Form ID - {$contact['form_id']}\n*Form Data:*\n$formatted_data";
			$this->wpnts_formflow_notify_slack( $message );
		} elseif ( true === $formflow_active_notice ) {
			$message = "Formflow Form (ID: {$contact['form_id']}) submitted at $current_time";
			$this->wpnts_formflow_notify_slack( $message );
		}

		$log_message = "Formflow Form (ID: {$contact['form_id']}) submitted at $current_time  $site_url";
		$severity = 'Normal';
		$event_type = 'Submission';
		$time = current_time( 'mysql' );

		$ip = $attachmentHandler->get_user_ip();
		$attachmentHandler->log_activity_to_db( $severity, $time, $ip, $display_name = 'Formflow', $event_type, $log_message  );
	}


	/**
	 * Sends a notification to Slack for Contact Form 7 submissions.
	 *
	 * This helper function sends a formatted message to the Slack webhook URL configured
	 * for Contact Form 7 notifications.
	 *
	 * @param string $message The message to send to Slack.
	 * @return void
	 */
	private function wpnts_notify_slack( $message ) {
		// Use SlackChannelRouter for channel and user tag support
		SlackChannelRouter::send_notification_with_routing(
			'wpntswebhook_integration_settings',
			$message
		);
	}

	/**
	 * Sends a notification to Slack for Formflow form submissions.
	 *
	 * This helper function sends a formatted message to the Slack webhook URL configured
	 * for Formflow notifications.
	 *
	 * @param string $message The message to send to Slack.
	 * @return void
	 */
	private function wpnts_formflow_notify_slack( $message ) {
		// Use SlackChannelRouter for channel and user tag support
		SlackChannelRouter::send_notification_with_routing(
			'wpntswebhook_integration_settings',
			$message
		);
	}
}
