<?php

declare(strict_types=1);

namespace App\Entity;

use App\Doctrine\Type\SignalIdType;
use App\Entity\Identifier\SignalId;
use App\Repository\SignalRepository;
use App\ValueObject\Point;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SignalRepository::class)]
#[ORM\Table(name: 'signals')]
#[ORM\Index(name: 'idx_signals_created_at', columns: ['created_at'])]
#[ORM\Index(name: 'idx_signals_user_key', columns: ['user_key'])]
readonly class Signal
{
    private function __construct(
        #[ORM\Column(type: Types::STRING, length: 64)] public string $userKey,
        #[ORM\Embedded(class: Point::class, columnPrefix: 'user_')] public Point $userLocation,
        #[ORM\Embedded(class: Point::class, columnPrefix: 'signal_')] public Point $signalLocation,
        #[ORM\Id] #[ORM\GeneratedValue(strategy: 'NONE')] #[ORM\Column(type: SignalIdType::class)] public SignalId $id = new SignalId(),
        #[ORM\Column(name: 'created_at', type: Types::DATETIME_IMMUTABLE)] public \DateTimeImmutable $createdAt = new \DateTimeImmutable()
    ) {
    }

    public static function create(string $userKey, Point $userLocation, Point $signalLocation): self
    {
        return new self(
            userKey: $userKey,
            userLocation: $userLocation,
            signalLocation: $signalLocation
        );
    }
}
