<?php

namespace App\Modules\Inventory\Stock\DTOs;

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

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

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