<?php

declare(strict_types=1);

namespace App\Framework\ErrorAggregation\Jobs;

use App\Framework\ErrorAggregation\AlertUrgency;
use App\Framework\ErrorAggregation\ErrorPattern;
use App\Framework\ErrorAggregation\ErrorEvent;
use App\Framework\Logging\Logger;

/**
 * Queue job for processing error pattern alerts
 */
final readonly class ErrorPatternAlertJob
{
    public function __construct(
        public string $patternId,
        public string $eventId,
        public AlertUrgency $urgency,
        public array $patternData,
        public array $triggeringEventData
    ) {
    }

    /**
     * Execute the job
     *
     * @param Logger|null $logger Injected by container
     * @return array Result metadata
     */
    public function handle(?Logger $logger = null): array
    {
        // Process the alert - log, send notifications, trigger webhooks, etc.
        if ($logger) {
            $logger->warning("[ErrorAggregation] Pattern alert triggered",
                \App\Framework\Logging\ValueObjects\LogContext::withData([
                    'pattern_id' => $this->patternId,
                    'event_id' => $this->eventId,
                    'urgency' => $this->urgency->value,
                    'occurrence_count' => $this->patternData['occurrence_count'] ?? 0
                ])
            );
        }

        // Here you would:
        // 1. Send notifications to ops team
        // 2. Trigger alerting systems (PagerDuty, Slack, etc.)
        // 3. Create incident tickets
        // 4. Update monitoring dashboards

        return [
            'type' => 'error_pattern_alert',
            'pattern_id' => $this->patternId,
            'event_id' => $this->eventId,
            'urgency' => $this->urgency->value,
            'processed_at' => time(),
            'success' => true
        ];
    }

    public function getType(): string
    {
        return 'error_aggregation.alert';
    }
}
