<?php
/**
 * WeCodeArt Framework.
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package 	WeCodeArt Framework
 * @subpackage 	Support\Modules\Security\Helper
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		6.7.0
 * @version		6.7.0
 */

namespace WeCodeArt\Support\Modules;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Config\Traits\Singleton;
use function WeCodeArt\Functions\get_prop;

/**
 * Security Helper
 */
final class Helper {

	use Singleton;

	/**
	 * Get security status for all modules
	 *
	 * @return array
	 */
	public function get_all_modules_security_status(): array {
		$modules = wecodeart_option( 'modules', [] );
		$security = Security::get_instance();
		$status = [];

		foreach ( $modules as $module ) {
			$module_name = get_prop( $module, 'destination', '' );
			if ( ! $module_name ) {
				continue;
			}

			$issues = $security->get_security_issues( $module_name );
			$status[$module_name] = [
				'secure' => empty( $issues ),
				'issues_count' => count( $issues ),
				'issues' => $issues
			];
		}

		return $status;
	}

	/**
	 * Get security status for a specific module
	 *
	 * @param  string $module_name
	 * @return array
	 */
	public function get_module_security_status( string $module_name ): array {
		$security = Security::get_instance();
		$issues = $security->get_security_issues( $module_name );

		return [
			'secure' => empty( $issues ),
			'issues_count' => count( $issues ),
			'issues' => $issues
		];
	}

	/**
	 * Get security summary
	 *
	 * @return array
	 */
	public function get_security_summary(): array {
		$status = $this->get_all_modules_security_status();
		$total_modules = count( $status );
		$secure_modules = count( array_filter( $status, function( $module ) {
			return $module['secure'];
		} ) );

		return [
			'total_modules' => $total_modules,
			'secure_modules' => $secure_modules,
			'insecure_modules' => $total_modules - $secure_modules,
			'security_percentage' => $total_modules > 0 ? round( ( $secure_modules / $total_modules ) * 100, 2 ) : 100
		];
	}

	/**
	 * Validate all installed modules
	 *
	 * @return array
	 */
	public function validate_all_modules(): array {
		$modules = wecodeart_option( 'modules', [] );
		$security = Security::get_instance();
		$results = [];

		foreach ( $modules as $module ) {
			$module_name = get_prop( $module, 'destination', '' );
			if ( ! $module_name ) {
				continue;
			}

			$module_path = wecodeart_config( 'paths' )['directory'] . '/inc/support/modules/' . $module_name;
			
			if ( is_dir( $module_path ) ) {
				$results[$module_name] = $security->validate_module( $module_path );
			}
		}

		return $results;
	}

	/**
	 * Get security recommendations
	 *
	 * @return array
	 */
	public function get_security_recommendations(): array {
		$status = $this->get_all_modules_security_status();
		$recommendations = [];

		foreach ( $status as $module_name => $module_status ) {
			if ( ! $module_status['secure'] ) {
				$recommendations[] = sprintf( 
					__( 'Review module "%s" for %d security issues', 'wecodeart' ),
					$module_name,
					$module_status['issues_count']
				);
			}
		}

		return $recommendations;
	}
} 