
# Workflow Templates

Workflow templates let you define a workflow's step structure once and dispatch it multiple times by name. This is useful when the same multi-step process needs to run with different payloads.

## Defining a template

Use `Queuety::define_workflow()` to build the template, then register it:

```php
use Queuety\Queuety;
use Queuety\Enums\Priority;

$builder = Queuety::define_workflow( 'onboarding' )
    ->on_queue( 'pipelines' )
    ->with_priority( Priority::Normal )
    ->max_attempts( 5 )
    ->then( CreateAccountHandler::class )
    ->then( SendWelcomeEmailHandler::class )
    ->then( NotifyAdminHandler::class );

Queuety::register_workflow_template( $builder );
```

Register templates early, for example in a plugin's initialization hook:

```php
add_action( 'init', function () {
    $builder = Queuety::define_workflow( 'onboarding' )
        ->then( CreateAccountHandler::class )
        ->then( SendWelcomeEmailHandler::class )
        ->then( NotifyAdminHandler::class );

    Queuety::register_workflow_template( $builder );
} );
```

## Running a template

Dispatch a registered template by name with `Queuety::run_workflow()`:

```php
$workflow_id = Queuety::run_workflow( 'onboarding', [ 'email' => 'user@example.com' ] );
```

The second argument is the initial payload/state, just like `->dispatch()` on a builder. The returned integer is the workflow ID.

If the template is not registered, a `RuntimeException` is thrown.

## Listing registered templates

Access the template registry:

```php
$registry = Queuety::workflow_templates();
$template = $registry->get( 'onboarding' ); // WorkflowTemplate or null
```

## Complete example

```php
// Register templates on init.
add_action( 'init', function () {
    // User onboarding pipeline
    $onboarding = Queuety::define_workflow( 'onboarding' )
        ->on_queue( 'users' )
        ->then( CreateAccountHandler::class )
        ->then( SetupDefaultsHandler::class )
        ->then( SendWelcomeEmailHandler::class );
    Queuety::register_workflow_template( $onboarding );

    // Content generation pipeline
    $content = Queuety::define_workflow( 'generate_content' )
        ->on_queue( 'llm' )
        ->max_attempts( 5 )
        ->then( BuildPromptHandler::class )
        ->then( CallLLMHandler::class )
        ->then( ReviewOutputHandler::class )
        ->then( PublishHandler::class );
    Queuety::register_workflow_template( $content );
} );

// Dispatch them anywhere in your application.
$wf1 = Queuety::run_workflow( 'onboarding', [ 'email' => 'alice@example.com' ] );
$wf2 = Queuety::run_workflow( 'generate_content', [ 'topic' => 'WordPress performance' ] );
```

## Templates vs inline workflows

| | Inline workflow | Template |
|---|---|---|
| Definition | At dispatch time | Once, at registration time |
| Dispatch | `Queuety::workflow('name')->then(...)->dispatch()` | `Queuety::run_workflow('name', $payload)` |
| Reuse | Build the chain each time | Register once, dispatch many times |
| Best for | One-off or dynamically composed workflows | Standard, repeatable processes |
