# Workflow Automation Scheduling Guide

## Overview
This module integrates with `WorkflowScheduleModule` to enable creating scheduled workflow automations. You can create schedules that run automatically via cron, or create them in a disabled state for manual triggering only.

## Controlling Auto-Scheduling Behavior

### Option 1: Create Schedules WITHOUT Auto-Execution
When creating a schedule, set `is_enabled: false` to prevent it from running automatically:

```typescript
// Inject WorkflowScheduleService in your controller/service
constructor(
  private readonly workflowScheduleService: WorkflowScheduleService,
) {}

// Create schedule without auto-execution
const schedule = await this.workflowScheduleService.createSchedule(
  {
    workflow_id: workflowId,
    workflow_name: 'My Workflow',
    name: 'My Schedule',
    cron_expression: '0 9 * * *', // Every day at 9 AM
    timezone: 'America/New_York',
    is_enabled: false, // 👈 This prevents auto-execution
    // ... other fields
  },
  loggedInUser,
);
```

### Option 2: Create Schedules WITH Auto-Execution
Simply set `is_enabled: true` (or omit it, as it defaults to true):

```typescript
const schedule = await this.workflowScheduleService.createSchedule(
  {
    workflow_id: workflowId,
    workflow_name: 'My Workflow',
    name: 'My Schedule',
    cron_expression: '0 9 * * *',
    timezone: 'America/New_York',
    is_enabled: true, // 👈 Schedule will run automatically on cron
    // ... other fields
  },
  loggedInUser,
);
```

## How It Works

The scheduling logic in `WorkflowScheduleService.createSchedule()` (line 101-103) only adds jobs to the Bull queue if:
1. `is_enabled === true`
2. `schedule_status === 'ACTIVE'`

If either condition is false, the schedule is saved to the database but NOT added to the queue, meaning the cron processor won't execute it.

## Manual Triggering

You can manually trigger any schedule (enabled or disabled) using:

```typescript
const result = await this.workflowScheduleService.triggerManualExecution(
  scheduleId,
  loggedInUser,
  { /* optional metadata */ },
);
```

## Enabling/Disabling Schedules Later

### To Enable a Disabled Schedule:
```typescript
await this.workflowScheduleService.updateSchedule(
  {
    id: scheduleId,
    is_enabled: true,
  },
  loggedInUser,
);
```

### To Disable an Active Schedule:
```typescript
await this.workflowScheduleService.updateSchedule(
  {
    id: scheduleId,
    is_enabled: false,
  },
  loggedInUser,
);
```

### To Pause a Schedule Temporarily:
```typescript
await this.workflowScheduleService.pauseSchedule(scheduleId, loggedInUser);
```

### To Resume a Paused Schedule:
```typescript
await this.workflowScheduleService.resumeSchedule(scheduleId, loggedInUser);
```

## Best Practices

1. **Testing**: Create schedules with `is_enabled: false` during development and testing
2. **Staged Rollout**: Create schedules disabled, test manually, then enable when ready
3. **Monitoring**: Use `getExecutionLogs()` and `getExecutionStats()` to monitor schedule performance
4. **Cleanup**: Use `deleteSchedule()` to soft-delete schedules you no longer need

## Example: Complete Workflow

```typescript
// 1. Create schedule (disabled)
const schedule = await this.workflowScheduleService.createSchedule({
  workflow_id: 123,
  workflow_name: 'Send Daily Report',
  name: 'Daily Report Schedule',
  cron_expression: '0 9 * * *',
  is_enabled: false, // Start disabled
}, loggedInUser);

// 2. Test manually
const testResult = await this.workflowScheduleService.triggerManualExecution(
  schedule.id,
  loggedInUser,
);

// 3. Check execution logs
const logs = await this.workflowScheduleService.getExecutionLogs({
  schedule_id: schedule.id,
}, loggedInUser);

// 4. If tests pass, enable for auto-execution
if (logs.data[0].execution_status === 'COMPLETED') {
  await this.workflowScheduleService.updateSchedule({
    id: schedule.id,
    is_enabled: true, // Now it will run on cron
  }, loggedInUser);
}
```

## Environment Configuration

The Bull queue (Redis) connection is configured in `src/config/bull.config.ts`. Ensure Redis is running and accessible for the scheduler to work.
