-- Recent workflows with task progress counts
SELECT
    w.id,
    w.name,
    w.status,
    w.on_error,
    w.output_task_index,
    w.success_policy,
    w.result,
    w.error,
    w.sent_at,
    w.created_at,
    w.started_at,
    w.completed_at,
    w.updated_at,
    COUNT(wt.id) as total_tasks,
    COUNT(*) FILTER (WHERE wt.status = 'COMPLETED') as completed_tasks,
    COUNT(*) FILTER (WHERE wt.status = 'FAILED') as failed_tasks,
    COUNT(*) FILTER (WHERE wt.status = 'RUNNING') as running_tasks,
    COUNT(*) FILTER (WHERE wt.status = 'PENDING' OR wt.status = 'READY') as pending_tasks,
    COUNT(*) FILTER (WHERE wt.status = 'ENQUEUED') as enqueued_tasks,
    COUNT(*) FILTER (WHERE wt.status = 'SKIPPED') as skipped_tasks
FROM horsies_workflows w
LEFT JOIN horsies_workflow_tasks wt ON wt.workflow_id = w.id
GROUP BY w.id
ORDER BY w.created_at DESC
LIMIT 100
