<?php
declare(strict_types=1);

namespace App\Modules\Cron;

use App\Modules\Automation\AutomationExecutionLogRepository;

final class AutomationHistoryCleanupHandler
{
    public function __construct(private readonly AutomationExecutionLogRepository $repository)
    {
    }

    /**
     * @param array<string, mixed> $payload
     * @return array<string, mixed>
     */
    public function handle(array $payload): array
    {
        $days = max(1, (int) ($payload['days'] ?? 30));
        $deletedCount = $this->repository->purgeOlderThanDays($days);

        return [
            'ok' => true,
            'days' => $days,
            'deleted_count' => $deletedCount,
        ];
    }
}
