<?php
/**
 * VMPFence Diagnostic Report Generator
 *
 * Generates reports in various formats
 *
 * @package VMPFence
 * @subpackage Tools/Models
 * @since 2.2.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Diagnostic Report Generator
 *
 * @since 2.2.0
 */
class VMPFence_Tools_Diagnostic_Report {

    /**
     * Generate text report
     *
     * @since 2.2.0
     * @param array $results Diagnostic results
     * @return string Plain text report
     */
    public static function generate_text($results) {
        $output = "=== VMPFence Diagnostic Report ===\n";
        $output .= "Generated: " . current_time('mysql') . "\n";
        $output .= "Overall Status: " . strtoupper($results['overall_status']) . "\n\n";

        $output .= "=== Summary ===\n";
        $output .= "Total Tests: " . $results['summary']['total_tests'] . "\n";
        $output .= "Passed: " . $results['summary']['passed'] . "\n";
        $output .= "Warnings: " . $results['summary']['warnings'] . "\n";
        $output .= "Failed: " . $results['summary']['failed'] . "\n\n";

        foreach ($results['tests'] as $test_name => $test) {
            $output .= "=== " . $test['name'] . " ===\n";
            $output .= "Status: " . strtoupper($test['status']) . "\n";
            $output .= "Message: " . $test['message'] . "\n";

            if (!empty($test['details'])) {
                $output .= "\nDetails:\n";
                foreach ($test['details'] as $detail) {
                    $output .= "  - " . $detail['label'] . ": " . $detail['value'];
                    $output .= " (" . strtoupper($detail['status']) . ")\n";
                }
            }

            if (!empty($test['recommendations'])) {
                $output .= "\nRecommendations:\n";
                foreach ($test['recommendations'] as $recommendation) {
                    $output .= "  - " . $recommendation . "\n";
                }
            }

            $output .= "\n";
        }

        if (!empty($results['recommendations'])) {
            $output .= "=== Overall Recommendations ===\n";
            foreach ($results['recommendations'] as $recommendation) {
                $output .= "  - " . $recommendation . "\n";
            }
        }

        return $output;
    }

    /**
     * Export diagnostic results
     *
     * @since 2.2.0
     * @param array $results Diagnostic results
     * @param string $format Export format (json, txt)
     * @return array Export result with file path
     */
    public static function export($results, $format = 'json') {
        $upload_dir = wp_upload_dir();
        $filename = 'vmpfence-diagnostic-' . date('Y-m-d-H-i-s') . '.' . $format;
        $filepath = $upload_dir['basedir'] . '/' . $filename;

        $content = '';
        switch ($format) {
            case 'json':
                $content = json_encode($results, JSON_PRETTY_PRINT);
                break;
            case 'txt':
                $content = self::generate_text($results);
                break;
            default:
                $content = serialize($results);
        }

        $saved = file_put_contents($filepath, $content);

        if ($saved === false) {
            return array(
                'success' => false,
                'message' => __('Failed to save export file', 'vmpfence-security')
            );
        }

        return array(
            'success' => true,
            'file' => $filename,
            'url' => $upload_dir['baseurl'] . '/' . $filename,
            'path' => $filepath
        );
    }
}
