<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\Device;

/**
 * Single source of truth for the wire shape of a Device. The REST API uses it
 * to render `/api/devices` responses; the Mercure publisher uses it so live
 * pushes are byte-identical to a fresh GET, letting the SPA splat the payload
 * straight into its store.
 */
final class DeviceSerializer
{
    /** @return array<string, mixed> */
    public function serialize(Device $d): array
    {
        return [
            'id'                      => $d->getId(),
            'mac'                     => $d->getMac(),
            'name'                    => $d->getName(),
            'model'                   => $d->getModel()->value,
            'orientation'             => $d->getOrientation()->value,
            'rotationIntervalMinutes' => $d->getRotationIntervalMinutes(),
            'wakeTimes'               => $d->getWakeTimes(),
            'timezone'                => $d->getTimezone(),
            'uniquenessWindow'        => $d->getUniquenessWindow(),
            'rotationMode'            => $d->getRotationMode()->value,
            'prioritizeNeverShown'    => $d->isPrioritizeNeverShown(),
            'linkedAt'                => $d->getLinkedAt()->format(\DateTimeInterface::ATOM),
            'lastSeenAt'              => $d->getLastSeenAt()?->format(\DateTimeInterface::ATOM),
            'nextPollExpectedAt'      => $d->getNextPollExpectedAt()?->format(\DateTimeInterface::ATOM),
            'lockedImageId'           => $d->getLockedImage()?->getId(),
            'currentImageId'          => $d->getCurrentImage()?->getId(),
        ];
    }
}
