---
name: workflow
description: |
  Creates and configures automated SmartStack workflows.
  Use this skill when:
  - User wants to automate actions (emails, webhooks)
  - User mentions "workflow", "automation", "trigger"
  - Creating a business process with steps
  - Integrating transactional emails
  Types: SendEmail, Wait, Condition, Webhook
---

# Skill Workflow SmartStack

> **Architecture:** Trigger -> Steps -> Actions (Email/Wait/Condition/Webhook)

**Reference:** [_shared.md](../_shared.md) for common services

## When This Skill Activates

| Trigger | Example |
|---------|---------|
| Explicit request | "Create a workflow for registration" |
| Automatic email | "Send an email when a ticket is created" |
| Chaining | "After 24h without response, send a reminder" |
| Keywords | "workflow", "trigger", "automation", "email template" |

## Flow

```
EVENT -> WorkflowTrigger("user.registered")
    |
IWorkflowService.TriggerAsync(code, variables)
    |
Workflow Steps: SendEmail -> Wait -> Condition -> Webhook
```

## Available Triggers

### User Events
| Trigger | Variables |
|---------|-----------|
| `user.registered` | userId, email, displayName, confirmUrl |
| `user.email-confirmed` | userId, email, displayName |
| `user.password-reset-requested` | userId, email, resetUrl |
| `user.account-locked/unlocked` | userId, email, lockReason |

### Ticket Events
| Trigger | Variables |
|---------|-----------|
| `ticket.created` | ticketId, ticketNumber, title, creatorEmail |
| `ticket.resolved/assigned` | ticketId, ticketNumber, assigneeName |
| `ticket.sla-warning/breached` | ticketId, deadline, remainingMinutes |

### Add a Trigger
```csharp
// WorkflowTriggerConfiguration.cs - GetSeedData()
new { Id = Guid.Parse("..."), Code = "entity.event", Name = "Entity Event",
    AvailableVariablesJson = JsonSerializer.Serialize(new[]{ ... }), ... }

// Trigger
await _workflowService.TriggerAsync("entity.event", new Dictionary<string, object>{...}, language: "en");
```

## Implementation

Start with [steps/step-00-init.md](steps/step-00-init.md) to identify workflow triggers and requirements, then proceed to [steps/step-01-implementation.md](steps/step-01-implementation.md) for:
- Step types (SendEmail, Wait, Condition, Webhook) with code templates
- Workflow creation and seed data
- Triggering from services
- Email templates (Handlebars syntax)
- Frontend API

## Relation with Domain Events

Workflow triggers (`TriggerAsync`) are the **primary mechanism** for cross-process automation (emails, webhooks, multi-step flows).

**Domain Events** (MediatR `INotificationHandler<T>`) are complementary for **in-process** side effects (audit logging, cache invalidation, read-model updates).

| Scenario | Mechanism |
|----------|-----------|
| Send email after entity creation | Workflow Trigger |
| Invalidate cache after entity update | Domain Event |
| Log audit trail | Domain Event |
| Multi-step business process with delays | Workflow Trigger |
| Notify multiple services of same change | Domain Event -> Workflow Trigger |

See `review-code/references/smartstack-conventions.md` (Domain Events section) for full pattern.

## Checklist

```
- Trigger identified (existing or new in WorkflowTriggerConfiguration.cs)
- Workflow created: Unique code, Linked trigger, Priority (10=standard, 20+=priority)
- Steps: SendEmail+templateId, Wait+delayMinutes, Condition+expression, Webhook+url
- Email templates created (if SendEmail)
- Trigger added to source code
- Tests: trigger, emails, variables
```

## Absolute Rules

| DO | DON'T |
|----|-------|
| IWorkflowService.TriggerAsync | Direct execution |
| All trigger variables | Hardcoded URLs in templates |
| Specify language for emails | Secrets in variables |
| Unique kebab-case codes | Infinite loops (A->B->A) |
| Log executions | Forget email templates |

## Key Files

| File | Role |
|------|------|
| `Domain/Communications/Workflow.cs` | Workflow entity |
| `Domain/Communications/WorkflowStep.cs` | Step entity |
| `Domain/Communications/WorkflowTrigger.cs` | Trigger entity |
| `Application/Common/Interfaces/IWorkflowService.cs` | Interface |
| `Infrastructure/Services/Workflow/WorkflowExecutionService.cs` | Implementation |
| `Infrastructure/.../WorkflowConfiguration.cs` | EF Config + Seed |

<success_criteria>
- Workflow entity, steps, and triggers created following SmartStack patterns
- EF Core configuration and seed data generated
- IWorkflowService integration wired correctly
- Email templates created with i18n support (4 languages)
</success_criteria>
