<?php

declare(strict_types=1);

namespace ImovelZapAi\Analytics\Application\Actions;

use ImovelZapAi\Analytics\Domain\Enums\AlertStatus;
use ImovelZapAi\Analytics\Domain\Exceptions\AnalyticsException;
use ImovelZapAi\Analytics\Infrastructure\Persistence\Eloquent\AnalyticsAlertModel;

final class AcknowledgeAnalyticsAlertAction
{
    public function execute(string $tenantId, string $alertId): AnalyticsAlertModel
    {
        $alert = AnalyticsAlertModel::query()
            ->where('tenant_id', $tenantId)
            ->where('id', $alertId)
            ->first();

        if ($alert === null) {
            throw AnalyticsException::notFound('Alerta');
        }

        $alert->update([
            'status' => AlertStatus::Acked->value,
        ]);

        return $alert->fresh();
    }
}
