File size: 24,470 Bytes
d8c6e01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Gateway-facing API endpoints — server-to-server, authenticated via X-Gateway-Key.

These endpoints are called by the external Multi-Channel Gateway service to:
- Charge credits for channel messages
- Retrieve connection credentials (decrypted)
- Report connection heartbeats
- Log individual messages
- Receive task completion callbacks
"""

import hmac
import logging
from datetime import datetime, timezone
from uuid import UUID

from fastapi import APIRouter, Depends, Header, HTTPException, Request
from sqlalchemy import func, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession

from src.config import settings
from src.core.encryption import decrypt_value
from src.database import get_db
from src.models.account import Account
from src.models.channel import ChannelConnection, ChannelContactBlock, ChannelMessage
from src.schemas.channel import (
    GatewayChargeRequest,
    GatewayChargeResponse,
    GatewayConnectionResponse,
    GatewayCreateTaskRequest,
    GatewayCreateWorkflowRunRequest,
    GatewayCreateWorkflowRunResponse,
    GatewayHeartbeatRequest,
    GatewayLogMessageRequest,
    GatewayPendingWorkflowDeliveriesResponse,
    GatewayPendingWorkflowDelivery,
    GatewayWorkflowRunStatusResponse,
)
from src.services.credit_ledger import CreditLedgerService

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/gateway", tags=["gateway"])


# ---------------------------------------------------------------------------
# Auth dependency
# ---------------------------------------------------------------------------


async def require_gateway_key(
    x_gateway_key: str = Header(..., alias="X-Gateway-Key"),
    request: Request = None,
    db: AsyncSession = Depends(get_db),
) -> None:
    """Validate the shared gateway service key.

    - 503 if gateway_service_key is not configured (empty).
    - 401 if the provided key doesn't match (audit-logged).
    """
    if not settings.gateway_service_key:
        raise HTTPException(
            status_code=503,
            detail="Gateway authentication not configured on this server.",
        )
    if not hmac.compare_digest(x_gateway_key, settings.gateway_service_key):
        # Audit log auth failure (SOC 2 CC7.2)
        try:
            await _audit_gateway(db, "auth_failure", request=request)
            await db.commit()
        except Exception:
            pass
        logger.warning("Gateway auth failure from %s", request.client.host if request and request.client else "unknown")
        raise HTTPException(status_code=401, detail="Invalid gateway key.")


# ---------------------------------------------------------------------------
# Rate limiting (backend-side fallback — catches bypasses of CF Worker in-memory limits)
# ---------------------------------------------------------------------------

from src.core.rate_limiter import rate_limit_by_ip


# ---------------------------------------------------------------------------
# Audit logging for gateway security events
# ---------------------------------------------------------------------------

async def _audit_gateway(db, action: str, request: Request = None, **kwargs):
    """Log a gateway security event."""
    from src.core.audit import audit_log
    await audit_log(
        db, action=f"gateway.{action}",
        actor_user_id="gateway-service",
        target_type="gateway",
        request=request,
        **kwargs,
    )


# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------


@router.post("/charge", response_model=GatewayChargeResponse, dependencies=[Depends(rate_limit_by_ip)])
async def charge_for_message(
    req: GatewayChargeRequest,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> GatewayChargeResponse:
    """Deduct credits from the connection owner's account for a channel message.

    Enforces the daily credit limit for this connection (if set). Uses
    SELECT FOR UPDATE on the account row for atomicity.
    """
    # Fetch the connection to get the owner
    connection = await db.get(ChannelConnection, req.connection_id)
    if not connection:
        return GatewayChargeResponse(success=False, error="Connection not found.")

    owner_id: UUID = connection.owner_id

    # Determine effective daily limit (request overrides connection setting)
    daily_limit = req.daily_credit_limit if req.daily_credit_limit is not None else connection.daily_credit_limit

    # Compute today's usage for this connection
    today_start = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
    usage_stmt = select(
        func.coalesce(func.sum(ChannelMessage.credits_charged), 0)
    ).where(
        ChannelMessage.connection_id == req.connection_id,
        ChannelMessage.created_at >= today_start,
    )
    today_usage = float((await db.execute(usage_stmt)).scalar_one())

    # Check daily limit
    if daily_limit and (today_usage + req.credits) > daily_limit:
        # Get remaining balance for response
        ledger = CreditLedgerService(db)
        balance_info = await ledger.get_balance(owner_id)
        return GatewayChargeResponse(
            success=False,
            remaining_balance=balance_info["available"],
            today_usage=today_usage,
            error=f"Daily credit limit of {daily_limit} would be exceeded (used today: {today_usage:.2f}).",
        )

    # Lock account row for atomic debit
    account_stmt = (
        select(Account)
        .where(Account.owner_id == owner_id)
        .with_for_update()
    )
    result = await db.execute(account_stmt)
    account = result.scalars().first()

    if not account:
        # Create account (gives signup bonus, then proceed)
        ledger = CreditLedgerService(db)
        account = await ledger.get_or_create_account(owner_id)
        result = await db.execute(
            select(Account).where(Account.owner_id == owner_id).with_for_update()
        )
        account = result.scalars().first()

    from decimal import Decimal

    dec_credits = Decimal(str(req.credits))
    available = account.balance - account.reserved

    if available < dec_credits:
        return GatewayChargeResponse(
            success=False,
            remaining_balance=float(available),
            today_usage=today_usage,
            error=f"Insufficient credits (available: {float(available):.2f}, required: {req.credits}).",
        )

    account.balance -= dec_credits
    await db.commit()

    # Refresh for updated balance
    await db.refresh(account)
    remaining = float(account.balance - account.reserved)
    new_today_usage = today_usage + req.credits

    logger.info(
        "Gateway charge: connection=%s owner=%s credits=%.4f today_usage=%.4f",
        req.connection_id,
        owner_id,
        req.credits,
        new_today_usage,
    )

    # SOC 2 CC7.2: audit log for credit charges (non-blocking)
    try:
        await _audit_gateway(
            db, "charge",
            target_id=str(req.connection_id),
            new_value={"credits": req.credits, "owner": str(owner_id), "remaining": round(remaining, 2)},
        )
        await db.commit()
    except Exception:
        logger.warning("Audit log for charge failed (non-blocking)", exc_info=True)

    return GatewayChargeResponse(
        success=True,
        remaining_balance=remaining,
        today_usage=new_today_usage,
    )


@router.get("/connections/{connection_id}", response_model=GatewayConnectionResponse)
async def get_connection(
    connection_id: UUID,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> GatewayConnectionResponse:
    """Return full connection details with DECRYPTED credentials.

    Only the gateway service should call this — it needs bot_token and
    webhook_secret in plaintext to operate.
    """
    connection = await db.get(ChannelConnection, connection_id)
    if not connection:
        raise HTTPException(status_code=404, detail="Connection not found.")

    decrypted_token = decrypt_value(connection.bot_token) if connection.bot_token else ""
    decrypted_secret = (
        decrypt_value(connection.webhook_secret) if connection.webhook_secret else None
    )

    blocked_result = await db.execute(
        select(ChannelContactBlock.platform_user_id_hash)
        .where(ChannelContactBlock.connection_id == connection_id)
    )
    blocked_users = [row[0] for row in blocked_result.all()]

    from src.services.channel_service import ChannelService
    return GatewayConnectionResponse(
        id=connection.id,
        owner_id=connection.owner_id,
        platform=connection.platform,
        bot_token=decrypted_token,
        webhook_secret=decrypted_secret,
        agent_id=connection.agent_id,
        skill_id=connection.skill_id,
        status=connection.status,
        daily_credit_limit=connection.daily_credit_limit,
        pause_on_limit=connection.pause_on_limit,
        low_balance_threshold=connection.low_balance_threshold,
        config=ChannelService.decrypt_config(connection.config),
        blocked_users=blocked_users,
        privacy_notice_url=connection.privacy_notice_url,
        workflow_id=connection.workflow_id,
        workflow_mappings=connection.workflow_mappings,
    )


@router.post("/heartbeat", status_code=200)
async def heartbeat(
    req: GatewayHeartbeatRequest,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> dict:
    """Update connection status and error_message from a gateway heartbeat.

    Expects each item in ``connections`` to have at least:
    ``{"connection_id": "<uuid>", "status": "active|paused|error", "error_message": null}``
    """
    updated = 0
    for item in req.connections:
        connection = await db.get(ChannelConnection, item.connection_id)
        if not connection:
            continue

        connection.status = item.status
        connection.error_message = item.error_message

        updated += 1

    await db.commit()
    logger.debug("Gateway heartbeat: updated %d connections", updated)
    return {"updated": updated}


@router.post("/log-message", status_code=200, dependencies=[Depends(rate_limit_by_ip)])
async def log_message(
    req: GatewayLogMessageRequest,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> dict:
    """Persist a channel message record.

    Handles IntegrityError (duplicate platform_message_id) gracefully.
    """
    from decimal import Decimal

    # Encrypt outbound message text at rest (GDPR Art. 32 / SOC 2 CC6.1)
    stored_text = req.message_text
    if req.direction == "outbound" and stored_text:
        from src.core.message_crypto import encrypt_message
        try:
            stored_text = encrypt_message(stored_text)
        except Exception:
            logger.warning("Failed to encrypt outbound message — storing plaintext")

    msg = ChannelMessage(
        connection_id=req.connection_id,
        platform_user_id=req.platform_user_id,
        platform_message_id=req.platform_message_id,
        platform_chat_id=req.platform_chat_id,
        direction=req.direction,
        message_text=stored_text,
        media_type=req.media_type,
        task_id=req.task_id,
        workflow_run_id=req.workflow_run_id,
        credits_charged=Decimal(str(req.credits_charged)),
        response_time_ms=req.response_time_ms,
        error=req.error,
    )
    db.add(msg)
    try:
        await db.commit()
    except IntegrityError:
        await db.rollback()
        logger.debug(
            "Gateway log-message: duplicate platform_message_id=%s for connection=%s",
            req.platform_message_id,
            req.connection_id,
        )
        return {"status": "duplicate"}

    logger.debug(
        "Gateway log-message: saved message connection=%s direction=%s",
        req.connection_id,
        req.direction,
    )
    return {"status": "ok"}


@router.get("/task-status/{task_id}")
async def gateway_task_status(
    task_id: UUID,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> dict:
    """Get task status — gateway-authenticated. Used by CF Worker to poll task completion."""
    from src.models.task import Task
    task = await db.get(Task, task_id)
    if not task:
        return {"status": "not_found"}

    status = task.status.value if hasattr(task.status, "value") else task.status
    result = {"status": status, "task_id": str(task.id)}

    if status in ("completed", "failed", "canceled"):
        result["artifacts"] = task.artifacts or []

    return result


@router.post("/create-task", status_code=201, dependencies=[Depends(rate_limit_by_ip)])
async def gateway_create_task(
    req: GatewayCreateTaskRequest,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> dict:
    """Create a task on behalf of a channel user, authenticated via X-Gateway-Key.

    The gateway supplies ``owner_id`` (the CrewHub user whose credits fund the
    task) so that no user-facing auth token is required.  This endpoint is the
    authoritative way for the gateway service to create tasks — it avoids the
    fabricated-API-key anti-pattern and keeps gateway→backend auth consistent.
    """
    from src.schemas.task import TaskCreate, TaskMessage, MessagePart
    from src.services.task_broker import TaskBrokerService

    # If no skill_id, look up the agent's first skill
    skill_id = req.skill_id
    if not skill_id:
        from src.models.skill import AgentSkill
        skill_result = await db.execute(
            select(AgentSkill.id).where(AgentSkill.agent_id == req.provider_agent_id).limit(1)
        )
        first_skill = skill_result.scalar_one_or_none()
        skill_id = str(first_skill) if first_skill else ""

    task_data = TaskCreate(
        provider_agent_id=req.provider_agent_id,
        skill_id=skill_id,
        messages=[
            TaskMessage(
                role="user",
                parts=[MessagePart(type="text", text=req.message)],
            )
        ],
        confirmed=True,  # gateway pre-approves cost; credit check still applies
    )
    service = TaskBrokerService(db)
    task = await service.create_task(data=task_data, user_id=req.owner_id)

    # Set callback URL if provided — backend will POST result when task completes
    if req.callback_url:
        task.callback_url = req.callback_url
        await db.flush()

    logger.info(
        "Gateway create-task: task_id=%s owner=%s agent=%s callback=%s",
        task.id,
        req.owner_id,
        req.provider_agent_id,
        bool(req.callback_url),
    )
    return {"task_id": str(task.id), "status": task.status}


@router.post("/task-callback", status_code=200)
async def task_callback(
    payload: dict,
    _: None = Depends(require_gateway_key),
) -> dict:
    """Placeholder for task completion callbacks from the gateway.

    The gateway service handles forwarding results back to the platform user.
    This endpoint simply acknowledges receipt so the gateway can confirm
    delivery was accepted by the CrewHub backend.
    """
    task_id = payload.get("task_id", "<unknown>")
    status = payload.get("status", "<unknown>")
    logger.info("Gateway task-callback: task_id=%s status=%s", task_id, status)
    return {"status": "acknowledged"}


@router.post(
    "/create-workflow-run",
    response_model=GatewayCreateWorkflowRunResponse,
    status_code=201,
    dependencies=[Depends(rate_limit_by_ip)],
)
async def gateway_create_workflow_run(
    req: GatewayCreateWorkflowRunRequest,
    request: Request,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> GatewayCreateWorkflowRunResponse:
    """Create a workflow run on behalf of a channel user.

    Validates the workflow exists and is accessible, checks credits, then
    dispatches the workflow execution engine.
    """
    from sqlalchemy.orm import selectinload
    from src.models.workflow import Workflow, WorkflowStep
    from src.services.workflow_execution import WorkflowExecutionService

    # Fetch connection to get the owner
    connection = await db.get(ChannelConnection, req.connection_id)
    if not connection:
        return GatewayCreateWorkflowRunResponse(
            status="error", error="Connection not found."
        )

    owner_id: UUID = connection.owner_id

    # Load workflow with steps (needed for cost estimation)
    wf_result = await db.execute(
        select(Workflow)
        .where(Workflow.id == req.workflow_id)
        .options(
            selectinload(Workflow.steps).selectinload(WorkflowStep.agent),
            selectinload(Workflow.steps).selectinload(WorkflowStep.skill),
        )
    )
    wf = wf_result.scalar_one_or_none()
    if not wf:
        return GatewayCreateWorkflowRunResponse(
            status="error", error="Workflow not found."
        )

    # Validate ownership or public access
    if wf.user_id != owner_id and not wf.is_public:
        return GatewayCreateWorkflowRunResponse(
            status="error", error="Workflow not accessible."
        )

    # Estimate credits
    exec_svc = WorkflowExecutionService(db)
    total_estimate = exec_svc._estimate_cost(wf)

    # Check balance
    ledger = CreditLedgerService(db)
    balance_info = await ledger.get_balance(owner_id)
    available = balance_info["available"]
    if total_estimate > 0 and available < total_estimate:
        return GatewayCreateWorkflowRunResponse(
            status="error",
            error=f"Insufficient credits (available: {available}, estimated: {total_estimate}).",
        )

    # Check daily limit (same pattern as /gateway/charge)
    daily_limit = connection.daily_credit_limit
    if daily_limit:
        today_start = datetime.now(timezone.utc).replace(
            hour=0, minute=0, second=0, microsecond=0
        )
        usage_stmt = select(
            func.coalesce(func.sum(ChannelMessage.credits_charged), 0)
        ).where(
            ChannelMessage.connection_id == req.connection_id,
            ChannelMessage.created_at >= today_start,
        )
        today_usage = float((await db.execute(usage_stmt)).scalar_one())
        if (today_usage + total_estimate) > daily_limit:
            return GatewayCreateWorkflowRunResponse(
                status="error",
                error=f"Daily credit limit of {daily_limit} would be exceeded (used today: {today_usage:.2f}).",
            )

    # Execute workflow
    try:
        run = await exec_svc.execute_workflow(
            workflow_id=req.workflow_id,
            user_id=owner_id,
            input_message=req.message,
            channel_connection_id=req.connection_id,
            channel_chat_id=req.chat_id,
        )
    except Exception as exc:
        logger.exception("Gateway create-workflow-run failed: %s", exc)
        return GatewayCreateWorkflowRunResponse(
            status="error", error=str(exc)
        )

    logger.info(
        "Gateway create-workflow-run: run_id=%s workflow=%s owner=%s",
        run.id, req.workflow_id, owner_id,
    )

    # Audit log (non-blocking)
    try:
        await _audit_gateway(
            db, "create_workflow_run",
            request=request,
            target_id=str(run.id),
            new_value={
                "workflow_id": str(req.workflow_id),
                "owner": str(owner_id),
                "estimated_credits": total_estimate,
            },
        )
        await db.commit()
    except Exception:
        logger.warning("Audit log for create-workflow-run failed (non-blocking)", exc_info=True)

    return GatewayCreateWorkflowRunResponse(
        workflow_run_id=str(run.id),
        status=run.status,
        estimated_credits=float(total_estimate),
        step_count=len(wf.steps),
        workflow_name=wf.name or "",
    )


@router.get(
    "/workflow-run-status/{run_id}",
    response_model=GatewayWorkflowRunStatusResponse,
)
async def gateway_workflow_run_status(
    run_id: UUID,
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> GatewayWorkflowRunStatusResponse:
    """Get workflow run status — gateway-authenticated."""
    from sqlalchemy.orm import selectinload
    from src.models.workflow import WorkflowRun

    result = await db.execute(
        select(WorkflowRun)
        .where(WorkflowRun.id == run_id)
        .options(selectinload(WorkflowRun.step_runs))
    )
    run = result.scalar_one_or_none()
    if not run:
        return GatewayWorkflowRunStatusResponse(
            status="not_found", error="Workflow run not found."
        )

    # Concatenate step outputs for final_output
    final_output = None
    if run.status in ("completed", "failed"):
        outputs = []
        for sr in sorted(run.step_runs, key=lambda x: (x.step_group, x.created_at)):
            if sr.output_text:
                outputs.append(sr.output_text)
        final_output = "\n\n---\n\n".join(outputs) if outputs else None

    steps_completed = sum(1 for sr in run.step_runs if sr.status == "completed")
    snapshot = run.workflow_snapshot or {}

    return GatewayWorkflowRunStatusResponse(
        status=run.status,
        final_output=final_output,
        step_count=len(run.step_runs),
        steps_completed=steps_completed,
        total_credits_charged=float(run.total_credits_charged) if run.total_credits_charged else None,
        error=run.error,
        workflow_name=snapshot.get("name", ""),
    )


@router.get(
    "/pending-workflow-deliveries",
    response_model=GatewayPendingWorkflowDeliveriesResponse,
)
async def gateway_pending_workflow_deliveries(
    _: None = Depends(require_gateway_key),
    db: AsyncSession = Depends(get_db),
) -> GatewayPendingWorkflowDeliveriesResponse:
    """Find completed/failed workflow runs that need delivery to a channel."""
    from sqlalchemy.orm import selectinload
    from src.models.workflow import WorkflowRun

    # Find runs linked to a channel that are terminal
    runs_result = await db.execute(
        select(WorkflowRun)
        .where(
            WorkflowRun.channel_connection_id.isnot(None),
            WorkflowRun.status.in_(["completed", "failed"]),
        )
        .options(selectinload(WorkflowRun.step_runs))
        .order_by(WorkflowRun.completed_at.desc())
        .limit(50)
    )
    runs = list(runs_result.scalars().unique().all())

    if not runs:
        return GatewayPendingWorkflowDeliveriesResponse(deliveries=[])

    # Filter out runs that already have an outbound ChannelMessage with matching workflow_run_id
    run_ids = [r.id for r in runs]
    delivered_result = await db.execute(
        select(ChannelMessage.workflow_run_id)
        .where(
            ChannelMessage.workflow_run_id.in_(run_ids),
            ChannelMessage.direction == "outbound",
        )
        .distinct()
    )
    delivered_ids = {row[0] for row in delivered_result.all()}

    deliveries = []
    for run in runs:
        if run.id in delivered_ids:
            continue

        # Collect final output
        outputs = []
        for sr in sorted(run.step_runs, key=lambda x: (x.step_group, x.created_at)):
            if sr.output_text:
                outputs.append(sr.output_text)
        final_output = "\n\n---\n\n".join(outputs) if outputs else None

        # Get connection info for platform
        connection = await db.get(ChannelConnection, run.channel_connection_id)
        if not connection:
            continue

        snapshot = run.workflow_snapshot or {}

        deliveries.append(GatewayPendingWorkflowDelivery(
            run_id=str(run.id),
            connection_id=str(run.channel_connection_id),
            chat_id=run.channel_chat_id or "",
            platform=connection.platform,
            status=run.status,
            final_output=final_output,
            workflow_name=snapshot.get("name", ""),
            total_credits_charged=float(run.total_credits_charged) if run.total_credits_charged else None,
            failure_mode=snapshot.get("failure_mode", "stop"),
        ))

    return GatewayPendingWorkflowDeliveriesResponse(deliveries=deliveries)