<?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 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 Debuglog {


	private $lastLoggedTimestamp;

	/**
	 * Construct method.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		add_filter( 'cron_schedules', [ $this, 'wpnts_add_cron_interval' ]);
		$schedules_int = get_option('wpnts_schedules_interval_debuglog_settings');
		$schedules_interval = json_decode($schedules_int);
		$wpdebuglognotice = $schedules_interval->wpdebuglognotice ?? 'false';

		if ( true === $wpdebuglognotice ) {
			add_action('wpnts_corn_hook', [ $this, 'log_error_to_slack' ]);
		}

		$this->lastLoggedTimestamp = get_option('wpnts_last_logged_timestamp', 0);
	}


	/**
	 * Corn setup time interval
	 *
	 * @since 1.0.0
	 */
	public function wpnts_add_cron_interval() {
		$wpntspro_time = 900;

		$schedules['added_schedules_interval'] = [
			'interval' => isset($wpntspro_time) ? $wpntspro_time : 900,
			'display'  => esc_html__( 'Assigned Time', 'notifier-to-slack' ),
		];
		return $schedules;
	}


	/**
	 * WordPress sales notification.
	 *
	 * @since 1.0.0
	 */
	public function log_error_to_slack() {

		   $schedules_int = get_option('wpnts_schedules_interval_debuglog_settings');
		   $schedules_interval = json_decode($schedules_int);
		   $wpntspro_webhook = $schedules_interval->webhook ?? '';
		   $wpdebuglognotice = $schedules_interval->wpdebuglognotice ?? 'false';
		   $debug_interval_time = 900;
		   $last_sent_time = get_option('wpnts_new_last_debug_log_sent_time', 3);
		  $current_time = time();

		   $slack_webhook_url = $wpntspro_webhook;

		if ( true === $wpdebuglognotice && isset($last_sent_time) && ( $current_time - $last_sent_time ) >= $debug_interval_time ) {

			$wp_debug = ( defined('WP_DEBUG') && WP_DEBUG === true );
			$output = '';

			if ( $wp_debug ) {
				   $debug_log = WP_CONTENT_DIR . '/debug.log';

				if ( file_exists($debug_log) ) {
					$debug_log_entries = file($debug_log, FILE_IGNORE_NEW_LINES);
					if ( ! empty($debug_log_entries) ) {
						   // Filter only new entries since the last logged timestamp.
						   $new_entries = array_filter($debug_log_entries, function ( $entry ) {
							   // Assuming timestamp is at the beginning of each log entry.
							   $timestamp = strtotime(substr($entry, 1, 19));
							   // Check if the timestamp is greater than the last logged timestamp.
							   return $timestamp > $this->lastLoggedTimestamp;
						   });

						if ( ! empty($new_entries) ) {
							$output = implode("\n", $new_entries);
							// Update last logged timestamp in options.
							$this->lastLoggedTimestamp = strtotime(substr(end($new_entries), 1, 19));
							update_option('wpnts_last_logged_timestamp', $this->lastLoggedTimestamp);
						}
					}
				} else {
					$output = 'Debug log file not found. No errors found yet.';
				}
			} else {
				   $output = 'Debug mode not activated.';
			}

			if ( ! empty($output) ) {
				   // Send notification to Slack with channel routing.
				   SlackChannelRouter::send_notification_with_routing(
					   'wpnts_schedules_interval_debuglog_settings',
					   $output
				   );
			}

				  update_option('wpnts_new_last_debug_log_sent_time', $current_time);
		}
	}
}
