*/
namespace RankMathPro;
use RankMath\Helper;
use RankMath\Traits\Hooker;
use MyThemeShop\Helpers\Param;
use MyThemeShop\Database\Database;
use RankMathPro\Admin\CSV;
defined( 'ABSPATH' ) || exit;
/**
 * Monitor class.
 *
 * @codeCoverageIgnore
 */
class Monitor_Pro extends CSV {
	use Hooker;
	/**
	 * Total hits cache.
	 *
	 * @var array
	 */
	private $total_hits_cache = [];
	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->filter( 'rank_math/404_monitor/page_title_actions', 'page_title_actions', 20, 1 );
		$this->action( 'rank_math/404_monitor/before_list_table', 'export_panel', 20 );
		$this->action( 'admin_enqueue_scripts', 'enqueue', 20 );
		$this->action( 'init', 'maybe_export', 20 );
		$this->filter( 'rank_math/404_monitor/list_table_columns', 'manage_columns', 20 );
		$this->filter( 'rank_math/404_monitor/list_table_column', 'total_hits_column', 20, 3 );
		$this->filter( 'rank_math/404_monitor/get_logs_args', 'get_logs_args', 20 );
	}
	/**
	 * Add page title action for export.
	 *
	 * @param array $actions Original actions.
	 * @return array
	 */
	public function page_title_actions( $actions ) {
		$actions['export'] = [
			'class' => 'page-title-action',
			'href'  => add_query_arg( 'export-404', '1' ),
			'label' => __( 'Export', 'rank-math-pro' ),
		];
		return $actions;
	}
	/**
	 * Output export panel.
	 *
	 * @return void
	 */
	public function export_panel() {
		$today = date( 'Y-m-d' );
		?>
		
		sanitize_datetime( Param::get( 'date_from' ) );
		$date_to   = $this->sanitize_datetime( Param::get( 'date_to' ) );
		$data = $this->export_items( $date_from, $date_to );
		$this->export(
			[
				'filename' => '404-log',
				'columns'  => $data['columns'],
				'items'    => $data['items'],
			]
		);
		die();
	}
	/**
	 * Do export.
	 *
	 * @param  string $time_from Start date (SQL DateTime format).
	 * @param  string $time_to   End date (SQL DateTime format).
	 *
	 * @return array
	 */
	private function export_items( $time_from = null, $time_to = null ) {
		global $wpdb;
		$logs_table = $wpdb->prefix . 'rank_math_404_logs';
		$query      = "SELECT * FROM {$logs_table} WHERE 1=1";
		$where      = '';
		if ( $time_from ) {
			$where .= " AND accessed > '{$time_from} 00:00:01'";
		}
		if ( $time_to ) {
			$where .= " AND accessed < '{$time_to} 23:59:59'";
		}
		$query .= $where;
		$items  = $wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		if ( empty( $items ) ) {
			return [
				'columns' => [],
				'items'   => [],
			];
		}
		$columns = array_keys( $items[0] );
		return [
			'columns' => $columns,
			'items'   => $items,
		];
	}
	/**
	 * Sanitize date field inputs.
	 *
	 * @param string $date Date input.
	 * @return string
	 */
	public function sanitize_datetime( $date ) {
		return preg_replace( '/[^0-9 :-]/', '', $date );
	}
	/**
	 * Enqueue styles and scripts.
	 *
	 * @param string $hook The current admin page.
	 *
	 * @return void
	 */
	public function enqueue( $hook ) {
		if ( 'rank-math_page_rank-math-404-monitor' !== $hook ) {
			return;
		}
		$url = RANK_MATH_PRO_URL . 'includes/modules/404-monitor/assets/';
		wp_enqueue_script( 'rank-math-pro-404-monitor', $url . 'js/404-monitor.js', [ 'jquery-ui-core', 'jquery-ui-datepicker' ], RANK_MATH_PRO_VERSION, true );
		wp_enqueue_style( 'rank-math-pro-404-monitor', $url . 'css/404-monitor.css', [], RANK_MATH_PRO_VERSION );
	}
	/**
	 * Add extra columns for the list table.
	 *
	 * @param array $columns Original columns.
	 * @return array
	 */
	public function manage_columns( $columns ) {
		if ( 'simple' === Helper::get_settings( 'general.404_monitor_mode' ) ) {
			return $columns;
		}
		$columns['total_hits'] = esc_html__( 'Hits', 'rank-math-pro' );
		return $columns;
	}
	/**
	 * Add content in the extra columns.
	 *
	 * @param string $content Original content.
	 * @param array  $item    Table item.
	 * @param string $column  Column name.
	 * @return string
	 */
	public function total_hits_column( $content, $item, $column ) {
		if ( 'total_hits' !== $column ) {
			return $content;
		}
		if ( ! isset( $this->total_hits_cache[ $item['uri'] ] ) ) {
			$this->total_hits_cache[ $item['uri'] ] = Database::table( 'rank_math_404_logs' )->selectCount( '*', 'count' )->where( 'uri', $item['uri'] )->getVar();
		}
		return '' . $this->total_hits_cache[ $item['uri'] ] . '';
	}
	/**
	 * Change get_logs() args when filtering for a URI.
	 *
	 * @param array $args Original args.
	 * @return array
	 */
	public function get_logs_args( $args ) {
		$uri = Param::get( 'uri' );
		if ( ! $uri ) {
			return $args;
		}
		$args['uri'] = $uri;
		return $args;
	}
}