import sqlite3
import json

db = sqlite3.connect('/app/data/relay.db')

chain_config = {
    'entry_step': 'send_notification',
    'max_steps': 10,
    'timeout': 3600,
    'steps': [
        {
            'id': 'send_notification',
            'type': 'webhook',
            'config': {
                'url': 'http://localhost:5060/api/test',
                'headers': {'X-Chain': 'test'},
            },
            'on_success': 'wait_step',
            'on_failure': 'error_log',
        },
        {
            'id': 'wait_step',
            'type': 'wait',
            'config': {'duration': 1},
            'on_success': 'send_followup',
        },
        {
            'id': 'send_followup',
            'type': 'email',
            'config': {
                'recipients': ['test@example.com'],
                'subject': 'Follow-up from {{name}}',
                'body': 'Thanks {{name}}! Your submission has been processed.',
            },
            'on_success': 'complete_log',
        },
        {
            'id': 'complete_log',
            'type': 'log',
            'config': {
                'level': 'info',
                'message': 'Chain completed for {{name}}',
            },
        },
        {
            'id': 'error_log',
            'type': 'log',
            'config': {
                'level': 'error',
                'message': 'Chain failed for {{name}}',
            },
        },
    ],
}

db.execute(
    "UPDATE form_actions SET config = ? WHERE id = 2",
    (json.dumps(chain_config),)
)
db.commit()

# Verify
row = db.execute("SELECT config FROM form_actions WHERE id = 2").fetchone()
print("Updated config:", json.loads(row[0])['steps'][0]['id'])
db.close()