# Workflow - Implementation Details

## Step Types

### 1. SendEmail
```csharp
WorkflowStep.CreateEmailStep("Welcome Email", emailTemplateId, order: 1);
// Config: templateId, recipientVariable, ccEmails
```

### 2. Wait
```csharp
WorkflowStep.CreateWaitStep("Wait 24h", delayMinutes: 1440, order: 2);
```

### 3. Condition
```csharp
WorkflowStep.CreateConditionStep("Check premium", JsonSerializer.Serialize(new {
    condition = "{{userType}} == 'Premium'",
    trueStepOrder = 3, falseStepOrder = 4
}), order: 2);
// Operators: ==, !=, >, <, contains, &&, ||
```

### 4. Webhook
```csharp
WorkflowStep.CreateWebhookStep("Notify CRM", JsonSerializer.Serialize(new {
    url = "https://...", method = "POST",
    headers = new { Authorization = "Bearer {{apiKey}}" },
    body = new { eventType = "...", userId = "{{userId}}" },
    retryCount = 3
}), order: 3);
```

## Workflow Creation

```csharp
// 1. Create workflow
var workflow = Workflow.Create("welcome-sequence", "Welcome Sequence", description, triggerId, null, false, 10);

// 2. Add steps
workflow.AddStep(WorkflowStep.CreateEmailStep("Welcome", templateId, 1));
workflow.AddStep(WorkflowStep.CreateWaitStep("Wait 24h", 1440, 2));
workflow.AddStep(WorkflowStep.CreateEmailStep("Follow-up", followUpTemplateId, 3));

// 3. Seed data (WorkflowConfiguration.cs)
builder.HasData(new { Id = ..., Code = "welcome-sequence", TriggerId = ..., IsActive = true, ... });
```

## Triggering

```csharp
// In Service or Controller
await _workflowService.TriggerAsync("user.registered", new Dictionary<string, object>
{
    ["userId"] = user.Id,
    ["email"] = user.Email,
    ["confirmUrl"] = GenerateConfirmUrl(user.Id)
}, language: "en", ct);
```

## Email Templates

### Structure
```csharp
EmailTemplate: Code, Name, Category, IsActive, Translations[]
EmailTemplateTranslation: LanguageCode, Subject, HtmlBody, TextBody
```

### Syntax (Handlebars)
```html
<h1>Welcome {{userName}}!</h1>
{{#if isPremium}}<p>Thank you Premium member!</p>{{else}}<p>Premium offers...</p>{{/if}}
{{#each items}}<li>{{this.name}}</li>{{/each}}
```

## Frontend API

```typescript
workflowsApi.getAll() / getById(id) / create(data) / update(id, data)
workflowsApi.getTriggers()
workflowsApi.addStep(workflowId, step) / updateStep / deleteStep / reorderSteps
workflowsApi.execute(workflowId, variables)
```
