<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Marketing;

use CpCreation\VitiCore\Marketing\Model\ProductReview;
use CpCreation\VitiCore\Marketing\Model\ProductReviewModerationAction;
use CpCreation\VitiCore\Marketing\Repository\ProductReviewModerationActionRepository;
use Symfony\Bundle\SecurityBundle\Security;

final readonly class ProductReviewHistoryRecorder
{
    public function __construct(
        private ProductReviewModerationActionRepository $actionRepository,
        private Security $security,
    ) {}

    public function record(
        ProductReview $review,
        string $action,
        ?string $reasonCode = null,
        ?string $reasonDetail = null,
        ?string $actorIdentifier = null,
        ?\DateTimeImmutable $occurredAt = null,
    ): ProductReviewModerationAction {
        $actorIdentifier ??= $this->security->getUser()?->getUserIdentifier() ?? 'system';
        $moderationAction = new ProductReviewModerationAction(
            review: $review,
            action: $action,
            actorIdentifier: $actorIdentifier,
            snapshot: $review->moderationSnapshot(),
            reasonCode: $reasonCode,
            reasonDetail: $reasonDetail,
            occurredAt: $occurredAt,
        );
        $this->actionRepository->persist($moderationAction);

        return $moderationAction;
    }
}
