<?php
/**
 * Class Noesia Cs Ecommerce Doctor Module.
 *
 * @package Noesia_Commerce_Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class implementation.
 */
class Noesia_CS_Ecommerce_Doctor_Module extends Noesia_CS_Admin_Module {
	/**
	 * Handles the related plugin operation.
	 */
	public function __construct() {
		$this->key         = 'ecommerce-doctor';
		$this->label       = __( 'Ecommerce Doctor', 'noesia-commerce-suite' );
		$this->icon        = '⚕';
		$this->description = __( 'Manual ecommerce health checks without page-load scans.', 'noesia-commerce-suite' );
		$this->view        = 'modules/ecommerce-doctor/views/admin.php';
		$this->primary     = false;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		$doctor_service = $this->service( 'doctor_service' );
		$issue_code     = isset( $_GET['noesia_cs_doctor_issue'] ) ? sanitize_key( wp_unslash( $_GET['noesia_cs_doctor_issue'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin detail selector; no state changes happen here.
		$issue_page     = isset( $_GET['noesia_cs_doctor_page'] ) ? max( 1, absint( wp_unslash( $_GET['noesia_cs_doctor_page'] ) ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin pagination value.
		$issue_details  = array();

		if ( $doctor_service && $issue_code && method_exists( $doctor_service, 'get_issue_details' ) ) {
			$issue_details = $doctor_service->get_issue_details( $issue_code, $issue_page, 25 );
		}

		return array(
			'report'                      => $doctor_service && method_exists( $doctor_service, 'get_last_report' ) ? $doctor_service->get_last_report() : array(),
			'doctor_issue_details'        => $issue_details,
			'doctor_scan_scope'           => array(
				'product_limit'               => absint( $this->setting( 'doctor_product_scan_limit', 250 ) ),
				'large_image_kb'              => absint( $this->setting( 'doctor_large_image_kb', 500 ) ),
				'broken_links_enabled'        => 'yes' === $this->setting( 'doctor_scan_broken_links', 'no' ),
				'broken_link_limit'           => absint( $this->setting( 'doctor_broken_link_limit', 20 ) ),
				'suspicious_signals_enabled'  => 'yes' === $this->setting( 'doctor_scan_suspicious_order_signals', 'yes' ),
				'data_retention_enabled'      => 'yes' === $this->setting( 'doctor_scan_data_retention_review', 'yes' ),
				'data_retention_months'       => absint( $this->setting( 'doctor_data_retention_months', 24 ) ),
				'temporary_data_enabled'      => 'yes' === $this->setting( 'doctor_scan_temporary_data', 'yes' ),
				'autoloaded_options_enabled'  => 'yes' === $this->setting( 'doctor_scan_autoloaded_options', 'yes' ),
				'action_scheduler_enabled'    => 'yes' === $this->setting( 'doctor_scan_action_scheduler_queue', 'yes' ),
			),
		);
	}

}
