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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Voice API endpoints for Phase 2 of ScamShield AI.

Provides voice-based scam interaction endpoints that wrap the existing
Phase 1 text pipeline with ASR (speech-to-text) on input and TTS
(text-to-speech) on output.  Phase 1 code is imported and reused
without modification.

Endpoints:
    POST /api/v1/voice/engage         - Voice-based honeypot engagement
    GET  /api/v1/voice/audio/{filename} - Serve generated audio files
    GET  /api/v1/voice/health         - Voice subsystem health check
"""

import os
import tempfile
import time
import uuid
from pathlib import Path
from typing import Optional

from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
from fastapi.responses import FileResponse

from app.api.auth import verify_api_key
from app.api.schemas import ExtractedIntelligence
from app.api.voice_schemas import (
    TranscriptionMetadata,
    VoiceEngageResponse,
    VoiceHealthResponse,
)
from app.config import settings
from app.utils.logger import get_logger

logger = get_logger(__name__)

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------

AUDIO_OUTPUT_DIR: Path = Path(tempfile.gettempdir()) / "scamshield_audio"
AUDIO_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

ALLOWED_AUDIO_CONTENT_TYPES = frozenset({
    "audio/mpeg",
    "audio/mp3",
    "audio/wav",
    "audio/x-wav",
    "audio/wave",
    "audio/ogg",
    "audio/flac",
    "audio/webm",
    "audio/mp4",
    "audio/x-m4a",
})

# TTS-supported language codes (subset of ISO 639-1 supported by gTTS)
_TTS_SUPPORTED_CODES = frozenset({"en", "hi", "gu", "ta", "te", "bn", "mr"})

# ---------------------------------------------------------------------------
# Router
# ---------------------------------------------------------------------------

voice_router = APIRouter(prefix="/api/v1/voice", tags=["voice"])


# ---------------------------------------------------------------------------
# POST /engage
# ---------------------------------------------------------------------------

@voice_router.post(
    "/engage",
    response_model=VoiceEngageResponse,
    dependencies=[Depends(verify_api_key)],
    summary="Voice-based honeypot engagement",
)
async def voice_engage(
    audio_file: UploadFile = File(
        ..., description="Audio file (mp3, wav, ogg, flac, webm)"
    ),
    session_id: Optional[str] = Form(
        None, description="Session ID for multi-turn conversations"
    ),
    language: Optional[str] = Form(
        None,
        description="Language hint (ISO 639-1 code, e.g. 'en', 'hi')",
    ),
) -> VoiceEngageResponse:
    """Process voice input through the scam detection and honeypot pipeline.

    Complete flow:
        1. Save uploaded audio to a temporary file.
        2. Transcribe audio to text via ASR (Whisper).
        3. Run Phase 1 scam detection on the transcribed text.
        4. If scam detected (or ongoing session), engage honeypot agent.
        5. Extract financial intelligence from the conversation.
        6. Convert the agent's text reply to speech via TTS (gTTS).
        7. Return structured response with transcription, detection
           results, agent reply text, audio URL, and intelligence.

    Args:
        audio_file: Uploaded audio in a supported format.
        session_id: Optional session identifier for continuing a
            multi-turn conversation.
        language: Optional ISO 639-1 language hint for ASR and TTS.

    Returns:
        VoiceEngageResponse containing the full processing result.

    Raises:
        HTTPException: 400 for invalid input, 500 for internal errors.
    """
    start_time: float = time.time()
    temp_audio_path: Optional[str] = None

    try:
        # Lazy imports -- avoids loading heavy models at module import time
        # and mirrors the Phase 1 import pattern inside endpoint handlers.
        from app.models.detector import get_detector
        from app.models.extractor import extract_intelligence
        from app.agent.honeypot import HoneypotAgent
        from app.database.redis_client import (
            get_session_state_with_fallback,
            save_session_state_with_fallback,
        )
        from app.voice.asr import get_asr_engine
        from app.voice.tts import get_tts_engine

        # ---- 1. Validate and persist upload --------------------------------
        _validate_audio_upload(audio_file)
        temp_audio_path = await _save_upload_to_temp(audio_file)

        # ---- 2. ASR: speech-to-text ----------------------------------------
        asr_engine = get_asr_engine()
        transcription = asr_engine.transcribe(
            temp_audio_path,
            language=language if language and language != "auto" else None,
        )

        transcribed_text: str = transcription.get("text", "")
        asr_language: str = transcription.get("language", "unknown")
        asr_confidence: float = transcription.get("confidence", 0.0)

        if not transcribed_text.strip():
            raise HTTPException(
                status_code=400,
                detail={
                    "code": "EMPTY_TRANSCRIPTION",
                    "message": (
                        "No speech detected in the uploaded audio file."
                    ),
                },
            )

        logger.info(
            "ASR complete: language=%s, confidence=%.3f, text_length=%d",
            asr_language,
            asr_confidence,
            len(transcribed_text),
        )

        # ---- 3. Determine processing language ------------------------------
        process_language: str = (
            language
            if language and language != "auto"
            else asr_language
        )

        # ---- 4. Session management -----------------------------------------
        if not session_id:
            session_id = str(uuid.uuid4())

        session_state = None
        is_ongoing_session: bool = False

        existing_state = get_session_state_with_fallback(session_id)
        if existing_state and existing_state.get("turn_count", 0) > 0:
            session_state = existing_state
            is_ongoing_session = True
            logger.info(
                "Continuing voice session %s, turn=%d",
                session_id,
                session_state.get("turn_count", 0),
            )

        # ---- 5. Scam detection (Phase 1 detector) --------------------------
        detector = get_detector()
        detection = detector.detect(transcribed_text, process_language)

        scam_detected: bool = detection.get("scam_detected", False)
        confidence: float = detection.get("confidence", 0.0)
        indicators: list = detection.get("indicators", [])
        scam_type: Optional[str] = indicators[0] if indicators else None

        # ---- 6. Non-scam, non-ongoing -> early return ----------------------
        if not scam_detected and not is_ongoing_session:
            processing_time_ms = int((time.time() - start_time) * 1000)
            logger.info(
                "No scam detected in voice session %s (confidence=%.2f)",
                session_id,
                confidence,
            )
            return VoiceEngageResponse(
                session_id=session_id,
                scam_detected=False,
                scam_confidence=confidence,
                scam_type=None,
                turn_count=0,
                ai_reply_text="No scam detected. Message appears legitimate.",
                ai_reply_audio_url=None,
                transcription=TranscriptionMetadata(
                    text=transcribed_text,
                    language=asr_language,
                    confidence=asr_confidence,
                ),
                voice_fraud=None,
                extracted_intelligence=None,
                processing_time_ms=processing_time_ms,
            )

        # ---- 7. Scam or ongoing -> honeypot engagement ---------------------
        if is_ongoing_session:
            existing_confidence = session_state.get("scam_confidence", 0.0)
            confidence = max(confidence, existing_confidence)
            scam_detected = True

        agent = HoneypotAgent()
        result = agent.engage(transcribed_text, session_state)

        # ---- 8. Intelligence extraction (Phase 1 extractor) ----------------
        full_text: str = " ".join(
            msg.get("message", "") for msg in result.get("messages", [])
        )
        intel, extraction_confidence = extract_intelligence(full_text)

        result["extracted_intel"] = intel
        result["extraction_confidence"] = extraction_confidence
        result["scam_confidence"] = confidence

        # ---- 9. Persist session to Redis -----------------------------------
        save_session_state_with_fallback(session_id, result)

        # ---- 10. Extract agent reply text ----------------------------------
        agent_messages = [
            m for m in result.get("messages", [])
            if m.get("sender") == "agent"
        ]
        agent_reply: str = (
            agent_messages[-1]["message"]
            if agent_messages
            else "Engaged with scammer."
        )

        # ---- 11. TTS: text-to-speech ---------------------------------------
        tts_language: str = _resolve_tts_language(asr_language, language)
        audio_url: Optional[str] = None

        try:
            tts_engine = get_tts_engine()
            audio_filename: str = f"reply_{uuid.uuid4().hex}.mp3"
            audio_output_path: str = str(AUDIO_OUTPUT_DIR / audio_filename)

            tts_engine.synthesize(
                text=agent_reply,
                language=tts_language,
                output_path=audio_output_path,
            )
            audio_url = f"/api/v1/voice/audio/{audio_filename}"
            logger.info("TTS complete: %s", audio_url)
        except Exception as tts_exc:
            # TTS is non-critical; the text reply is still usable.
            logger.warning(
                "TTS synthesis failed, returning text-only response: %s",
                tts_exc,
            )

        # ---- 12. Build response --------------------------------------------
        turn_count: int = result.get("turn_count", 1)
        processing_time_ms = int((time.time() - start_time) * 1000)

        extracted_intelligence = ExtractedIntelligence(
            upi_ids=intel.get("upi_ids", []),
            bank_accounts=intel.get("bank_accounts", []),
            ifsc_codes=intel.get("ifsc_codes", []),
            phone_numbers=intel.get("phone_numbers", []),
            phishing_links=intel.get("phishing_links", []),
            extraction_confidence=extraction_confidence,
        )

        logger.info(
            "Voice engagement complete: session=%s, turn=%d, "
            "confidence=%.2f, processing_time=%dms",
            session_id,
            turn_count,
            confidence,
            processing_time_ms,
        )

        return VoiceEngageResponse(
            session_id=session_id,
            scam_detected=True,
            scam_confidence=confidence,
            scam_type=scam_type,
            turn_count=turn_count,
            ai_reply_text=agent_reply,
            ai_reply_audio_url=audio_url,
            transcription=TranscriptionMetadata(
                text=transcribed_text,
                language=asr_language,
                confidence=asr_confidence,
            ),
            voice_fraud=None,
            extracted_intelligence=extracted_intelligence,
            processing_time_ms=processing_time_ms,
        )

    except HTTPException:
        raise
    except ValueError as exc:
        logger.warning("Validation error in voice engage: %s", exc)
        raise HTTPException(
            status_code=400,
            detail={
                "code": "INVALID_REQUEST",
                "message": str(exc),
            },
        )
    except Exception as exc:
        logger.error("Voice engage failed: %s", exc, exc_info=True)
        raise HTTPException(
            status_code=500,
            detail={
                "code": "VOICE_PROCESSING_ERROR",
                "message": "An error occurred during voice processing.",
                "details": str(exc) if settings.DEBUG else None,
            },
        )
    finally:
        if temp_audio_path:
            _cleanup_file(temp_audio_path)


# ---------------------------------------------------------------------------
# GET /audio/{filename}
# ---------------------------------------------------------------------------

@voice_router.get(
    "/audio/{filename}",
    summary="Serve generated audio response",
)
async def serve_audio(filename: str) -> FileResponse:
    """Serve a previously generated TTS audio file.

    Args:
        filename: Name of the audio file (e.g. ``reply_<uuid>.mp3``).

    Returns:
        FileResponse streaming the MP3 audio content.

    Raises:
        HTTPException: 400 if the filename contains path-traversal
            characters, 404 if the file does not exist.
    """
    if ".." in filename or "/" in filename or "\\" in filename:
        raise HTTPException(
            status_code=400,
            detail={
                "code": "INVALID_FILENAME",
                "message": "Invalid audio filename.",
            },
        )

    file_path: Path = AUDIO_OUTPUT_DIR / filename

    if not file_path.exists() or not file_path.is_file():
        raise HTTPException(
            status_code=404,
            detail={
                "code": "AUDIO_NOT_FOUND",
                "message": f"Audio file '{filename}' not found.",
            },
        )

    return FileResponse(
        path=str(file_path),
        media_type="audio/mpeg",
        filename=filename,
    )


# ---------------------------------------------------------------------------
# GET /health
# ---------------------------------------------------------------------------

@voice_router.get(
    "/health",
    response_model=VoiceHealthResponse,
    summary="Voice subsystem health check",
)
async def voice_health() -> VoiceHealthResponse:
    """Check the operational status of ASR and TTS engines.

    Returns:
        VoiceHealthResponse reporting the status of each voice engine.
    """
    asr_info = None
    tts_info = None
    overall_status = "healthy"

    # ASR health
    try:
        from app.voice.asr import get_asr_engine

        asr = get_asr_engine()
        asr_info = {
            "model": asr.model_size,
            "device": asr.device,
            "loaded": asr.model is not None,
        }
    except Exception as exc:
        logger.warning("ASR health check failed: %s", exc)
        asr_info = {"loaded": False, "error": str(exc)}
        overall_status = "degraded"

    # TTS health
    try:
        from app.voice.tts import get_tts_engine

        tts = get_tts_engine()
        tts_info = {
            "engine": tts.engine,
            "loaded": True,
        }
    except Exception as exc:
        logger.warning("TTS health check failed: %s", exc)
        tts_info = {"loaded": False, "error": str(exc)}
        overall_status = "degraded"

    # Both engines down => unhealthy
    if (
        asr_info
        and not asr_info.get("loaded")
        and tts_info
        and not tts_info.get("loaded")
    ):
        overall_status = "unhealthy"

    return VoiceHealthResponse(
        status=overall_status,
        asr=asr_info,
        tts=tts_info,
    )


# ---------------------------------------------------------------------------
# Helper functions
# ---------------------------------------------------------------------------

def _validate_audio_upload(audio_file: UploadFile) -> None:
    """Validate that the uploaded file is a usable audio file.

    Args:
        audio_file: FastAPI UploadFile instance.

    Raises:
        HTTPException: 400 if the file is missing or has an
            unsupported content type.
    """
    if not audio_file or not audio_file.filename:
        raise HTTPException(
            status_code=400,
            detail={
                "code": "MISSING_AUDIO_FILE",
                "message": "An audio file is required.",
            },
        )

    content_type: str = (audio_file.content_type or "").lower()

    # Only reject when the content type is explicitly non-audio.
    # Allow unknown/octet-stream through so that clients without proper
    # MIME detection still work.
    if (
        content_type
        and content_type not in ALLOWED_AUDIO_CONTENT_TYPES
        and not content_type.startswith("audio/")
        and content_type != "application/octet-stream"
    ):
        raise HTTPException(
            status_code=400,
            detail={
                "code": "INVALID_AUDIO_FORMAT",
                "message": (
                    f"Unsupported content type '{content_type}'. "
                    f"Accepted: {', '.join(sorted(ALLOWED_AUDIO_CONTENT_TYPES))}"
                ),
            },
        )


async def _save_upload_to_temp(audio_file: UploadFile) -> str:
    """Persist an uploaded audio file to a temporary path on disk.

    Args:
        audio_file: FastAPI UploadFile instance.

    Returns:
        Absolute path to the saved temporary file.

    Raises:
        HTTPException: 400 if the file body is empty, 500 if the
            file cannot be written.
    """
    suffix: str = Path(audio_file.filename or "audio.wav").suffix or ".wav"

    try:
        fd, temp_path = tempfile.mkstemp(suffix=suffix, prefix="voice_in_")
        with os.fdopen(fd, "wb") as tmp:
            content: bytes = await audio_file.read()
            if not content:
                os.unlink(temp_path)
                raise HTTPException(
                    status_code=400,
                    detail={
                        "code": "EMPTY_AUDIO_FILE",
                        "message": "The uploaded audio file is empty.",
                    },
                )
            tmp.write(content)

        logger.debug(
            "Saved upload to temp file: %s (%d bytes)",
            temp_path,
            len(content),
        )
        return temp_path

    except HTTPException:
        raise
    except Exception as exc:
        logger.error("Failed to save uploaded audio: %s", exc)
        raise HTTPException(
            status_code=500,
            detail={
                "code": "FILE_SAVE_ERROR",
                "message": "Failed to process the uploaded audio file.",
            },
        ) from exc


def _resolve_tts_language(
    asr_language: str,
    language_hint: Optional[str],
) -> str:
    """Determine the best language code for TTS synthesis.

    Priority order:
        1. Explicit client hint (if TTS-supported).
        2. ASR-detected language (if TTS-supported).
        3. English (``'en'``) as fallback.

    Args:
        asr_language: Language code detected by the ASR engine.
        language_hint: Optional language hint from the client form data.

    Returns:
        ISO 639-1 code supported by the TTS engine.
    """
    if language_hint:
        normalized: str = language_hint.strip().lower()
        if normalized in _TTS_SUPPORTED_CODES:
            return normalized

    if asr_language:
        normalized = asr_language.strip().lower()
        if normalized in _TTS_SUPPORTED_CODES:
            return normalized

    return "en"


def _cleanup_file(path: str) -> None:
    """Remove a temporary file, suppressing errors.

    Args:
        path: Absolute path to the file to remove.
    """
    try:
        if os.path.exists(path):
            os.remove(path)
    except OSError as exc:
        logger.debug("Failed to clean up temp file %s: %s", path, exc)