<?php
declare(strict_types=1);

function hv5_save_workflow(int $id, string $name, string $trigger, array $actions, bool $enabled = true): void {
    if (!function_exists('hv4_table_exists') || !hv4_table_exists('workflow_definitions')) return;
    $json = json_encode($actions, JSON_UNESCAPED_UNICODE);
    if ($id > 0) {
        db()->prepare('UPDATE workflow_definitions SET name=?, trigger_event=?, actions=?, enabled=? WHERE id=?')
            ->execute([$name, $trigger, $json, $enabled ? 1 : 0, $id]);
    } else {
        db()->prepare('INSERT INTO workflow_definitions (name, trigger_event, actions, enabled) VALUES (?,?,?,?)')
            ->execute([$name, $trigger, $json, $enabled ? 1 : 0]);
    }
}

function hv5_workflow_templates(): array {
    return [
        ['name' => 'New booking → notify owner', 'trigger_event' => 'booking.created', 'actions' => [
            ['type' => 'notify', 'title' => 'New reservation'],
            ['type' => 'queue_sync', 'direction' => 'outbound'],
        ]],
        ['name' => 'Checkout → cleaning task', 'trigger_event' => 'booking.checked_out', 'actions' => [
            ['type' => 'create_ticket', 'category' => 'cleaning'],
        ]],
        ['name' => 'Failed payment alert', 'trigger_event' => 'payment.failed', 'actions' => [
            ['type' => 'notify', 'title' => 'Payment failed'],
        ]],
    ];
}
