<?php
/**
 * Tools: test mail, settings export/import, reset, cleanup-now, health-check.
 *
 * Capability checks live in the calling layer (admin/views/page-tools.php and
 * the AJAX module) — this class is the operational backend only.
 *
 * @package Brainwerk Security Suite
 */

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

class Shieldforge_Tools {

    public static function register( Shieldforge_Loader $loader ) {
        unset( $loader );
    }

    public static function send_test_mail( $to = '' ) {
        $to = $to ? sanitize_email( $to ) : (string) Shieldforge_Settings::get( 'notify_email_to', get_option( 'admin_email' ) );
        if ( ! $to || ! is_email( $to ) ) {
            return new WP_Error( 'shieldforge_no_recipient', __( 'No valid recipient address.', 'brainwerk-security-suite' ) );
        }
        $subject = sprintf( __( '[%s] Brainwerk Security Suite test mail', 'brainwerk-security-suite' ), get_bloginfo( 'name' ) );
        $body    = __( "This is a Brainwerk Security Suite test message.\n\nIf you received this, alert delivery is working.", 'brainwerk-security-suite' );
        $sent    = wp_mail( $to, $subject, $body );
        return $sent ? true : new WP_Error( 'shieldforge_mail_failed', __( 'wp_mail() returned false.', 'brainwerk-security-suite' ) );
    }

    public static function export_settings() {
        return array(
            'plugin'    => 'brainwerk-security-suite',
            'version'   => SHIELDFORGE_VERSION,
            'exported'  => current_time( 'mysql', 1 ),
            'is_multisite' => is_multisite() ? 1 : 0,
            'settings'  => Shieldforge_Settings::all(),
        );
    }

    public static function import_settings( $payload ) {
        if ( ! is_array( $payload ) || empty( $payload['settings'] ) || ! is_array( $payload['settings'] ) ) {
            return new WP_Error( 'shieldforge_bad_payload', __( 'Invalid import payload.', 'brainwerk-security-suite' ) );
        }
        if ( empty( $payload['plugin'] ) || 'brainwerk-security-suite' !== $payload['plugin'] ) {
            return new WP_Error( 'shieldforge_wrong_plugin', __( 'Payload is not from Brainwerk Security Suite.', 'brainwerk-security-suite' ) );
        }
        Shieldforge_Settings::update( $payload['settings'] );
        return true;
    }

    public static function reset_defaults() {
        delete_site_option( SHIELDFORGE_OPTIONS_KEY );
        delete_option( SHIELDFORGE_OPTIONS_KEY );
        if ( method_exists( 'Shieldforge_Activator', 'activate' ) ) {
            Shieldforge_Activator::activate();
        }
        Shieldforge_Settings::flush_cache();
        return true;
    }

    public static function cleanup_now() {
        $deleted = Shieldforge_Logger::cleanup();
        return is_numeric( $deleted ) ? (int) $deleted : 0;
    }

    public static function health_check() {
        global $wpdb;
        $checks = array();

        $log_table = Shieldforge_Utils::table( 'log' );
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- health-check probe of the plugin's own tables; direct live read by design; not cacheable.
        $row       = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $log_table ) );
        $checks[]  = array(
            'name'   => __( 'Database tables present', 'brainwerk-security-suite' ),
            'status' => $row === $log_table ? 'ok' : 'fail',
            'detail' => $row ? $log_table : __( 'shieldforge_log not found — re-activate the plugin.', 'brainwerk-security-suite' ),
        );

        $checks[] = array(
            'name'   => __( 'Daily digest cron scheduled', 'brainwerk-security-suite' ),
            'status' => wp_next_scheduled( 'shieldforge_daily_digest' ) ? 'ok' : 'warn',
            'detail' => wp_next_scheduled( 'shieldforge_daily_digest' )
                ? gmdate( 'Y-m-d H:i', wp_next_scheduled( 'shieldforge_daily_digest' ) )
                : __( 'Not scheduled', 'brainwerk-security-suite' ),
        );

        $checks[] = array(
            'name'   => __( 'Cleanup cron scheduled', 'brainwerk-security-suite' ),
            'status' => wp_next_scheduled( 'shieldforge_cleanup_logs' ) ? 'ok' : 'warn',
            'detail' => wp_next_scheduled( 'shieldforge_cleanup_logs' )
                ? gmdate( 'Y-m-d H:i', wp_next_scheduled( 'shieldforge_cleanup_logs' ) )
                : __( 'Not scheduled', 'brainwerk-security-suite' ),
        );

        $email_to = (string) Shieldforge_Settings::get( 'notify_email_to', '' );
        $checks[] = array(
            'name'   => __( 'Notification address set', 'brainwerk-security-suite' ),
            'status' => is_email( $email_to ) ? 'ok' : 'warn',
            'detail' => is_email( $email_to ) ? $email_to : __( 'No address configured', 'brainwerk-security-suite' ),
        );

        $checks[] = array(
            'name'   => __( 'IP anonymization (GDPR)', 'brainwerk-security-suite' ),
            'status' => Shieldforge_Settings::get( 'privacy_anonymize_ip', true ) ? 'ok' : 'warn',
            'detail' => Shieldforge_Settings::get( 'privacy_anonymize_ip', true )
                ? __( 'Enabled', 'brainwerk-security-suite' )
                : __( 'Disabled — full IPs will be stored', 'brainwerk-security-suite' ),
        );

        return $checks;
    }
}
