<?php
declare(strict_types=1);

namespace App\Service\Rest\Operation;

use JetBrains\PhpStorm\Pure;

/**
 * A single update operation (a PUT request)
 */
class UpdateOperation extends BaseRestOperation
{
    /**
     * @param string $label A label for the operation
     * @param string $entityName The name of the entity to update. This will be used in the path of the request.
     * @param int $id
     * @param array<mixed> $data The data to update, will be post as Json within the request.
     * @param array<mixed> $initialData The data of the existing object, before the update
     * @param array<mixed> $parameters
     * @param array<mixed> $options
     */
    #[Pure]
    public function __construct(
        protected string $label,
        protected string $entityName,
        protected int $id,
        protected array $data,
        protected array $initialData = [],
        protected array $parameters = [],
        protected array $options = []
    ) {
        $options['json'] = $this->data;

        parent::__construct(
            $label,
            'PUT',
            $entityName . '/' . $id,
            $initialData,
            $parameters,
            $options
        );
    }

    /**
     * @return string
     */
    public function getEntityName(): string
    {
        return $this->entityName;
    }

    /**
     * @return array<mixed>
     */
    public function getData(): array
    {
        return $this->data;
    }

    /**
     * Return an array of messages describing the change that this operation will bring
     *
     * @return list<string>
     */
    public function getChangeLog(): array
    {
        $messages = [
            '[PUT ' . $this->entityName . '/' . $this->id . ']'
        ];
        array_push(
            $messages,
            ...self::getRecursiveChangeLog($this->initialData, $this->data)
        );
        return $messages;
    }
}
