<?php

namespace App\Modules\Inventory\Reservation\DTOs;

final readonly class ReservationApplyResult
{
    /**
     * @param  array<string, mixed>  $payload
     */
    public function __construct(
        public int $movementId,
        public string $reservationPublicId,
        public string $onHandAfter,
        public string $reservedAfter,
        public string $availableAfter,
        public array $payload = [],
    ) {}

    /**
     * @param  array<string, mixed>  $stored
     */
    public static function fromIdempotencyPayload(array $stored): self
    {
        return new self(
            movementId: (int) $stored['movement_id'],
            reservationPublicId: (string) $stored['reservation_public_id'],
            onHandAfter: (string) $stored['on_hand_after'],
            reservedAfter: (string) $stored['reserved_after'],
            availableAfter: (string) $stored['available_after'],
            payload: $stored,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toIdempotencyPayload(): array
    {
        return [
            'movement_id' => $this->movementId,
            'reservation_public_id' => $this->reservationPublicId,
            'on_hand_after' => $this->onHandAfter,
            'reserved_after' => $this->reservedAfter,
            'available_after' => $this->availableAfter,
        ] + $this->payload;
    }
}
