<?php
/**
 * FluentC WordPress Performance Monitor File.
 *
 * This file contains the functions for monitoring performance of the FluentC plugin.
 *
 * @category Performance
 * @package  FluentCPlugin
 * @author   FluentC <wp@fluentc.io>
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link     https://www.fluentc.ai
 */

namespace FluentC\Utils;

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

/**
 * Performance Monitoring Class
 */
class Performance_Monitor
{
    /**
     * @var array Stores timing data for various operations
     */
    private $timings = [];

    /**
     * @var array Stores memory usage data for various operations
     */
    private $memoryUsage = [];

    /**
     * Start timing for a specific operation
     *
     * @param string $operation Name of the operation to time
     * @return void
     */
    public function startTiming($operation)
    {
        $this->timings[$operation] = [
            'start' => microtime(true),
            'end' => null,
        ];
    }

    /**
     * End timing for a specific operation
     *
     * @param string $operation Name of the operation to end timing for
     * @return void
     */
    public function endTiming($operation)
    {
        if (isset($this->timings[$operation])) {
            $this->timings[$operation]['end'] = microtime(true);
        }
    }

    /**
     * Record memory usage for a specific operation
     *
     * @param string $operation Name of the operation
     * @return void
     */
    public function recordMemoryUsage($operation)
    {
        $this->memoryUsage[$operation] = memory_get_usage(true);
    }

    /**
     * Get the execution time for a specific operation
     *
     * @param string $operation Name of the operation
     * @return float|null Execution time in seconds, or null if timing is incomplete
     */
    public function getExecutionTime($operation)
    {
        if (isset($this->timings[$operation]) && $this->timings[$operation]['end'] !== null) {
            return $this->timings[$operation]['end'] - $this->timings[$operation]['start'];
        }
        return null;
    }

    /**
     * Get memory usage for a specific operation
     *
     * @param string $operation Name of the operation
     * @return int|null Memory usage in bytes, or null if not recorded
     */
    public function getMemoryUsage($operation)
    {
        return $this->memoryUsage[$operation] ?? null;
    }

    /**
     * Get a summary of all performance metrics
     *
     * @return array Performance summary
     */
    public function getSummary()
    {
        $summary = [
            'timings' => [],
            'memory_usage' => $this->memoryUsage,
        ];

        foreach ($this->timings as $operation => $timing) {
            $summary['timings'][$operation] = $this->getExecutionTime($operation);
        }

        return $summary;
    }

    /**
     * Log performance metrics
     *
     * @return void
     */
    public function logPerformance()
    {
        $summary = $this->getSummary();
        do_action('qm/info', 'FluentC Performance Metrics: ' . print_r($summary, true));
    }
}