<?php

declare(strict_types=1);

namespace App\Framework\ErrorReporting;

use Throwable;

/**
 * Contextual reporter with request information
 */
final readonly class RequestContextualReporter
{
    public function __construct(
        private ErrorReporter $reporter,
        private string        $method,
        private string        $route,
        private ?string       $requestId = null,
        private ?string       $userAgent = null,
        private ?string       $ipAddress = null,
        private ?array        $requestData = null
    ) {
    }

    public function reportThrowable(Throwable $throwable, string $level = 'error', array $context = []): string
    {
        $report = ErrorReport::fromThrowable($throwable, $level, $context)
            ->withRequest(
                method     : $this->method,
                route      : $this->route,
                requestId  : $this->requestId,
                userAgent  : $this->userAgent,
                ipAddress  : $this->ipAddress,
                requestData: $this->requestData
            );

        return $this->reporter->report($report);
    }

    public function reportError(string $level, string $message, array $context = [], ?Throwable $exception = null): string
    {
        $report = ErrorReport::create($level, $message, $context, $exception)
            ->withRequest(
                method     : $this->method,
                route      : $this->route,
                requestId  : $this->requestId,
                userAgent  : $this->userAgent,
                ipAddress  : $this->ipAddress,
                requestData: $this->requestData
            );

        return $this->reporter->report($report);
    }
}
