<?php

declare(strict_types=1);

namespace Tests\Unit\Framework\LiveComponents\Profiling;

use App\Framework\Random\RandomGenerator;
use App\Framework\Telemetry\OperationHandle;
use App\Framework\Telemetry\TelemetryService;

/**
 * Simple Test Implementation of TelemetryService
 *
 * Minimal implementation for testing without complex dependencies.
 * Provides valid OperationHandle instances but doesn't track anything.
 */
final class SimpleTelemetryService implements TelemetryService
{
    /** @var array<string, mixed> */
    private array $recordedMetrics = [];

    /** @var array<string, mixed> */
    private array $recordedEvents = [];

    public function __construct(
        private readonly RandomGenerator $randomGenerator
    ) {}

    public function startOperation(
        string $name,
        string $type,
        array $attributes = []
    ): OperationHandle {
        // Create a valid OperationHandle with this service instance
        $operationId = 'test-op-' . bin2hex($this->randomGenerator->bytes(4));
        return new OperationHandle($operationId, $this);
    }

    public function trace(
        string $name,
        string $type,
        callable $callback,
        array $attributes = []
    ): mixed {
        // Just execute the callback without tracing
        return $callback();
    }

    public function recordMetric(
        string $name,
        float $value,
        string $unit = '',
        array $attributes = []
    ): void {
        $this->recordedMetrics[] = [
            'name' => $name,
            'value' => $value,
            'unit' => $unit,
            'attributes' => $attributes,
        ];
    }

    public function recordEvent(
        string $name,
        array $attributes = [],
        string $severity = 'info'
    ): void {
        $this->recordedEvents[] = [
            'name' => $name,
            'attributes' => $attributes,
            'severity' => $severity,
        ];
    }

    /**
     * End an operation (called by OperationHandle)
     */
    public function endOperation(string $operationId, ?string $status = null, ?string $errorMessage = null): void
    {
        // No-op for testing
    }

    /**
     * Add an attribute to an operation
     */
    public function addOperationAttribute(string $operationId, string $key, mixed $value): void
    {
        // No-op for testing
    }

    /**
     * Get recorded metrics for test assertions
     */
    public function getRecordedMetrics(): array
    {
        return $this->recordedMetrics;
    }

    /**
     * Get recorded events for test assertions
     */
    public function getRecordedEvents(): array
    {
        return $this->recordedEvents;
    }

    /**
     * Clear all recorded data
     */
    public function clear(): void
    {
        $this->recordedMetrics = [];
        $this->recordedEvents = [];
    }
}
