<?php

declare(strict_types=1);

/**
 * bin/process_automations.php
 *
 * Run on a schedule (e.g. every 15 minutes for processDueRuns(), once
 * daily with --daily for checkDateBasedTriggers() — see the crontab
 * suggestion in docs/PHASE_4_SUMMARY.md). Event-triggered workflows
 * (lead_created, customer_created, appointment_completed) fire
 * synchronously from their respective controllers/services and don't
 * need this worker EXCEPT to resume any run currently paused on a
 * 'wait' step.
 */

require dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__) . '/app/Core/Autoloader.php';

use App\Core\Autoloader;
use App\Core\Env;
use App\Services\AutomationEngine;

Autoloader::register(dirname(__DIR__));
Env::load(dirname(__DIR__) . '/.env');
require dirname(__DIR__) . '/app/Helpers/functions.php';

$engine = new AutomationEngine();

$resumed = $engine->processDueRuns();
echo date('Y-m-d H:i:s') . " — resumed {$resumed} paused run(s)\n";

if (($argv[1] ?? '') === '--daily') {
    $engine->checkDateBasedTriggers();
    echo date('Y-m-d H:i:s') . " — checked birthday/inactivity triggers\n";
}
