<?php

declare(strict_types=1);

namespace App\Framework\Telemetry\Config;

/**
 * Configuration for the Telemetry system
 */
final class TelemetryConfig
{
    /**
     * @param string $serviceName Name of the service
     * @param string $serviceVersion Version of the service
     * @param string $environment Environment (dev, test, prod)
     * @param bool $enabled Whether telemetry is enabled
     * @param float $samplingRatio Sampling ratio (0.0-1.0)
     * @param array<string, ExporterConfig> $exporters Configured exporters
     * @param array<string, mixed> $resourceAttributes Additional resource attributes
     */
    public function __construct(
        private readonly string $serviceName,
        private readonly string $serviceVersion,
        private readonly string $environment,
        private readonly bool $enabled = true,
        private readonly float $samplingRatio = 1.0,
        private readonly array $exporters = [],
        private readonly array $resourceAttributes = [],
    ) {
    }

    /**
     * Create config from array
     */
    public static function fromArray(array $config): self
    {
        $exporters = [];
        foreach ($config['exporters'] ?? [] as $name => $exporterConfig) {
            if (isset($exporterConfig['enabled']) && $exporterConfig['enabled']) {
                $exporters[$name] = new ExporterConfig(
                    $exporterConfig['endpoint'] ?? '',
                    $exporterConfig['protocol'] ?? 'http',
                    $exporterConfig['options'] ?? []
                );
            }
        }

        return new self(
            $config['service_name'] ?? 'app',
            $config['service_version'] ?? '1.0.0',
            $config['environment'] ?? 'dev',
            $config['enabled'] ?? true,
            $config['sampling_ratio'] ?? 1.0,
            $exporters,
            $config['resource_attributes'] ?? []
        );
    }

    /**
     * Get service name
     */
    public function getServiceName(): string
    {
        return $this->serviceName;
    }

    /**
     * Get service version
     */
    public function getServiceVersion(): string
    {
        return $this->serviceVersion;
    }

    /**
     * Get environment
     */
    public function getEnvironment(): string
    {
        return $this->environment;
    }

    /**
     * Get sampling ratio
     */
    public function getSamplingRatio(): float
    {
        return $this->samplingRatio;
    }

    /**
     * Get exporters
     *
     * @return array<string, ExporterConfig>
     */
    public function getExporters(): array
    {
        return $this->exporters;
    }

    /**
     * Get resource attributes
     *
     * @return array<string, mixed>
     */
    public function getResourceAttributes(): array
    {
        return $this->resourceAttributes;
    }

    /**
     * Check if telemetry is enabled
     */
    public function isEnabled(): bool
    {
        return $this->enabled;
    }

    /**
     * Get all resource attributes including standard ones
     *
     * @return array<string, mixed>
     */
    public function getAllResourceAttributes(): array
    {
        return array_merge([
            'service.name' => $this->serviceName,
            'service.version' => $this->serviceVersion,
            'deployment.environment' => $this->environment,
        ], $this->resourceAttributes);
    }

    /**
     * Check if an exporter is configured
     */
    public function hasExporter(string $name): bool
    {
        return isset($this->exporters[$name]);
    }

    /**
     * Get an exporter configuration
     */
    public function getExporter(string $name): ?ExporterConfig
    {
        return $this->exporters[$name] ?? null;
    }
}
