<?php

declare(strict_types=1);

namespace App\Service;

use App\Exception\PointTooFarException;
use App\ValueObject\Distance;
use App\ValueObject\Point;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final readonly class PointProximityValidator
{
    public function __construct(
        #[Autowire('%app.max_signal_distance_km%')] private float $maximumDistanceKm,
        #[Autowire(service: 'monolog.logger.signals')] private LoggerInterface $logger,
    ) {
    }

    public function assertWithinRange(Point $userLocation, Point $signalLocation): void
    {
        $distance = Distance::between($userLocation, $signalLocation)->inKilometers();

        $this->logger->debug('Calculated proximity between user and signal.', [
            'distance_km' => $distance,
            'max_distance_km' => $this->maximumDistanceKm,
        ]);

        if ($distance > $this->maximumDistanceKm) {
            $this->logger->warning('Rejected signal because it is beyond the allowed proximity.', [
                'distance_km' => $distance,
                'max_distance_km' => $this->maximumDistanceKm,
            ]);
            throw new PointTooFarException($this->maximumDistanceKm);
        }
    }
}
