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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
"""
DocuLens - Prompt Configuration API endpoints.

Provides CRUD for prompt versions: list, get active, update/create versions,
activate a specific version, seed defaults, and A/B prompt testing.
"""

import io
import os
import json
import time
import base64
import logging
import tempfile
from typing import Optional

from fastapi import APIRouter, HTTPException, Query, UploadFile, File, Form, Depends
from pydantic import BaseModel
from PIL import Image

from extraction import (
    PROMPT_LIBRARY,
    image_to_base64,
    _parse_vlm_json,
    _vlm_data_to_result,
)
from db.supabase import (
    is_enabled,
    list_prompt_versions,
    get_active_prompt,
    save_prompt_version,
    activate_prompt_version,
    get_all_active_prompts,
)
from middleware import check_rate_limit, validate_file_upload, sanitize_filename

logger = logging.getLogger(__name__)

prompts_router = APIRouter()

HF_TOKEN = os.environ.get("HF_TOKEN", "")


# ── Request / response models ────────────────────────────────────────

class PromptUpdateRequest(BaseModel):
    prompt_text: str
    created_by: Optional[str] = None


class ActivateVersionRequest(BaseModel):
    version: int


# ── Endpoints ────────────────────────────────────────────────────────

@prompts_router.get("/api/v1/prompts")
async def api_list_all_prompts(api_key: str = Depends(check_rate_limit)):
    """List all prompts β€” active DB versions merged with hardcoded defaults.

    For each (use_case, stage) in the hardcoded library, returns:
      - The active DB prompt if one exists, otherwise the hardcoded default.
      - source: "db" or "default" so the UI knows which is in effect.
    """
    # Build the full catalogue from the hardcoded library
    result = []
    for stage, stage_prompts in PROMPT_LIBRARY.items():
        for use_case, prompt_text in stage_prompts.items():
            if use_case == "_default":
                continue  # Skip _default in the UI listing
            entry = {
                "use_case": use_case,
                "stage": stage,
                "prompt_text": prompt_text,
                "source": "default",
                "version": 0,
                "is_active": True,
            }
            result.append(entry)

    # Also include _default prompts for each stage
    for stage, stage_prompts in PROMPT_LIBRARY.items():
        if "_default" in stage_prompts:
            result.append({
                "use_case": "_default",
                "stage": stage,
                "prompt_text": stage_prompts["_default"],
                "source": "default",
                "version": 0,
                "is_active": True,
            })

    # Overlay with DB versions if Supabase is configured
    if is_enabled():
        db_prompts = get_all_active_prompts()
        db_map = {(p["use_case"], p["stage"]): p for p in db_prompts}
        for entry in result:
            key = (entry["use_case"], entry["stage"])
            if key in db_map:
                db_row = db_map[key]
                entry["prompt_text"] = db_row["prompt_text"]
                entry["source"] = "db"
                entry["version"] = db_row["version"]
                entry["db_id"] = db_row["id"]

    return {"prompts": result}


@prompts_router.get("/api/v1/prompts/{use_case}/{stage}")
async def api_get_prompt(use_case: str, stage: str, api_key: str = Depends(check_rate_limit)):
    """Get prompt details for a specific (use_case, stage) pair.

    Returns the active prompt (DB or default) plus version history.
    """
    # Active prompt
    hardcoded = PROMPT_LIBRARY.get(stage, {}).get(
        use_case, PROMPT_LIBRARY.get(stage, {}).get("_default", "")
    )

    active_text = hardcoded
    source = "default"
    active_version = 0

    if is_enabled():
        db_text = get_active_prompt(use_case, stage)
        if db_text:
            active_text = db_text
            source = "db"

    # Version history
    versions = []
    if is_enabled():
        versions = list_prompt_versions(use_case, stage)
        for v in versions:
            if v.get("is_active"):
                active_version = v["version"]

    return {
        "use_case": use_case,
        "stage": stage,
        "active_prompt": active_text,
        "default_prompt": hardcoded,
        "source": source,
        "active_version": active_version,
        "versions": versions,
    }


@prompts_router.put("/api/v1/prompts/{use_case}/{stage}")
async def api_update_prompt(
    use_case: str,
    stage: str,
    body: PromptUpdateRequest,
    api_key: str = Depends(check_rate_limit),
):
    """Save a new version of the prompt for (use_case, stage).

    Creates a new version and makes it active. The previous version
    is preserved in history.
    """
    if not is_enabled():
        raise HTTPException(
            status_code=503,
            detail="Supabase not configured β€” prompt persistence unavailable",
        )

    row = save_prompt_version(
        use_case=use_case,
        stage=stage,
        prompt_text=body.prompt_text,
        created_by=body.created_by,
    )
    if not row:
        raise HTTPException(status_code=500, detail="Failed to save prompt version")

    return {"status": "ok", "version": row}


@prompts_router.post("/api/v1/prompts/{use_case}/{stage}/activate")
async def api_activate_version(
    use_case: str,
    stage: str,
    body: ActivateVersionRequest,
    api_key: str = Depends(check_rate_limit),
):
    """Activate a specific historical version of a prompt."""
    if not is_enabled():
        raise HTTPException(
            status_code=503,
            detail="Supabase not configured β€” prompt persistence unavailable",
        )

    ok = activate_prompt_version(use_case, stage, body.version)
    if not ok:
        raise HTTPException(status_code=500, detail="Failed to activate version")

    return {"status": "ok", "activated_version": body.version}


@prompts_router.post("/api/v1/prompts/{use_case}/{stage}/reset")
async def api_reset_prompt(use_case: str, stage: str, api_key: str = Depends(check_rate_limit)):
    """Reset a prompt to the hardcoded default by creating a new version
    with the default text. This preserves history β€” the user can always
    revert to any previous version.
    """
    if not is_enabled():
        raise HTTPException(
            status_code=503,
            detail="Supabase not configured β€” prompt persistence unavailable",
        )

    hardcoded = PROMPT_LIBRARY.get(stage, {}).get(
        use_case, PROMPT_LIBRARY.get(stage, {}).get("_default", "")
    )
    if not hardcoded:
        raise HTTPException(status_code=404, detail="No default prompt for this use case/stage")

    row = save_prompt_version(
        use_case=use_case,
        stage=stage,
        prompt_text=hardcoded,
        created_by="system_reset",
    )
    if not row:
        raise HTTPException(status_code=500, detail="Failed to reset prompt")

    return {"status": "ok", "version": row}


@prompts_router.post("/api/v1/prompts/seed")
async def api_seed_defaults(api_key: str = Depends(check_rate_limit)):
    """Seed all hardcoded prompts into the DB as version 1.

    Skips any (use_case, stage) that already has a version in the DB.
    Useful for initial setup.
    """
    if not is_enabled():
        raise HTTPException(
            status_code=503,
            detail="Supabase not configured β€” prompt persistence unavailable",
        )

    seeded = []
    skipped = []
    for stage, stage_prompts in PROMPT_LIBRARY.items():
        for use_case, prompt_text in stage_prompts.items():
            existing = list_prompt_versions(use_case, stage)
            if existing:
                skipped.append(f"{stage}/{use_case}")
                continue
            row = save_prompt_version(
                use_case=use_case,
                stage=stage,
                prompt_text=prompt_text,
                created_by="seed",
            )
            if row:
                seeded.append(f"{stage}/{use_case}")

    return {"seeded": seeded, "skipped": skipped}


# ---------------------------------------------------------------------------
# A/B Prompt Testing
# ---------------------------------------------------------------------------

def _run_extraction_with_prompt(
    image: Image.Image,
    prompt_text: str,
    model_id: str,
) -> dict:
    """Run extraction on an image using a specific prompt and model.

    Returns the parsed extraction result as a dict.
    """
    from hf_client import create_inference_client, robust_chat_completion

    img_b64 = image_to_base64(image)
    data_url = f"data:image/jpeg;base64,{img_b64}"

    client = create_inference_client(api_key=HF_TOKEN)

    response = robust_chat_completion(
        client,
        model=model_id,
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "image_url", "image_url": {"url": data_url}},
                    {"type": "text", "text": prompt_text},
                ],
            }
        ],
        max_tokens=4096,
    )

    result_text = response.choices[0].message.content
    data = _parse_vlm_json(result_text)
    result = _vlm_data_to_result(data, model_id, "auto")

    # Return a clean dict without large base64 images
    d = result.to_dict()
    d.pop("page_image", None)
    d.pop("page_images", None)
    d.pop("raw_text", None)
    d.pop("bounding_boxes", None)
    return d


def _compute_field_diff(result_a: dict, result_b: dict) -> list:
    """Compare two extraction results field by field.

    Returns a list of diff entries:
      {field, value_a, value_b, match: bool, type: 'added'|'removed'|'changed'|'same'}
    """
    fields_a = result_a.get("extracted_fields", {})
    fields_b = result_b.get("extracted_fields", {})

    all_keys = sorted(set(list(fields_a.keys()) + list(fields_b.keys())))
    diff = []

    for key in all_keys:
        val_a = fields_a.get(key)
        val_b = fields_b.get(key)

        if key not in fields_a:
            diff.append({"field": key, "value_a": None, "value_b": val_b, "match": False, "type": "added"})
        elif key not in fields_b:
            diff.append({"field": key, "value_a": val_a, "value_b": None, "match": False, "type": "removed"})
        elif str(val_a).strip() == str(val_b).strip():
            diff.append({"field": key, "value_a": val_a, "value_b": val_b, "match": True, "type": "same"})
        else:
            diff.append({"field": key, "value_a": val_a, "value_b": val_b, "match": False, "type": "changed"})

    # Also compare line_items count
    li_a = result_a.get("line_items", [])
    li_b = result_b.get("line_items", [])
    diff.append({
        "field": "_line_items_count",
        "value_a": len(li_a),
        "value_b": len(li_b),
        "match": len(li_a) == len(li_b),
        "type": "same" if len(li_a) == len(li_b) else "changed",
    })

    return diff


@prompts_router.post("/api/v1/prompts/ab-test")
async def api_ab_test(
    file: UploadFile = File(...),
    prompt_a: str = Form(...),
    prompt_b: str = Form(...),
    model: str = Form("Qwen/Qwen2.5-VL-72B-Instruct"),
    golden_fields: str = Form(""),
    api_key: str = Depends(check_rate_limit),
):
    """Run A/B test: extract from the same document using two different prompts.

    Returns both results plus a field-by-field diff, and optionally compares
    against golden-copy fields if provided.
    """
    validate_file_upload(file.filename, file.size or 0, file.content_type)
    safe_filename = sanitize_filename(file.filename)

    if not HF_TOKEN:
        raise HTTPException(status_code=503, detail="HF_TOKEN not configured")

    contents = await file.read()
    ext = os.path.splitext(safe_filename)[1].lower()

    # Load image
    if ext == ".pdf":
        try:
            import fitz
            doc = fitz.open(stream=contents, filetype="pdf")
            page = doc[0]
            pix = page.get_pixmap(dpi=200)
            image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
            doc.close()
        except Exception as e:
            raise HTTPException(status_code=422, detail=f"Failed to render PDF: {e}")
    else:
        try:
            image = Image.open(io.BytesIO(contents))
        except Exception as e:
            raise HTTPException(status_code=422, detail=f"Failed to open image: {e}")

    # Run both extractions
    errors = {}
    result_a = None
    result_b = None

    start_a = time.time()
    try:
        result_a = _run_extraction_with_prompt(image, prompt_a, model)
        result_a["_processing_ms"] = int((time.time() - start_a) * 1000)
    except Exception as e:
        errors["prompt_a"] = str(e)[:200]
        logger.warning(f"A/B test prompt_a failed: {e}")

    start_b = time.time()
    try:
        result_b = _run_extraction_with_prompt(image, prompt_b, model)
        result_b["_processing_ms"] = int((time.time() - start_b) * 1000)
    except Exception as e:
        errors["prompt_b"] = str(e)[:200]
        logger.warning(f"A/B test prompt_b failed: {e}")

    if not result_a and not result_b:
        raise HTTPException(
            status_code=500,
            detail=f"Both prompts failed: {errors}",
        )

    # Compute diff between A and B
    diff_ab = _compute_field_diff(result_a or {}, result_b or {}) if result_a and result_b else []

    # Compare against golden copy if provided
    golden_diff_a = []
    golden_diff_b = []
    golden_score_a = None
    golden_score_b = None

    if golden_fields:
        try:
            golden = json.loads(golden_fields)
            golden_wrapper = {"extracted_fields": golden}

            if result_a:
                golden_diff_a = _compute_field_diff(golden_wrapper, result_a)
                matches = sum(1 for d in golden_diff_a if d["match"])
                golden_score_a = round(matches / max(len(golden_diff_a), 1), 2)

            if result_b:
                golden_diff_b = _compute_field_diff(golden_wrapper, result_b)
                matches = sum(1 for d in golden_diff_b if d["match"])
                golden_score_b = round(matches / max(len(golden_diff_b), 1), 2)
        except json.JSONDecodeError:
            pass  # Ignore invalid golden fields

    return {
        "result_a": result_a,
        "result_b": result_b,
        "diff": diff_ab,
        "golden_comparison": {
            "score_a": golden_score_a,
            "score_b": golden_score_b,
            "diff_a": golden_diff_a,
            "diff_b": golden_diff_b,
        } if golden_fields else None,
        "errors": errors if errors else None,
    }


# ---------------------------------------------------------------------------
# AI-Powered Prompt Improvement
# ---------------------------------------------------------------------------

class PromptImproveRequest(BaseModel):
    use_case: str
    stage: str = "extraction"
    feedback: str
    current_output: Optional[str] = None
    model: str = "Qwen/Qwen2.5-VL-72B-Instruct"


META_PROMPT_TEMPLATE = """You are an expert prompt engineer specializing in document extraction prompts for vision-language models.

## Current Extraction Prompt
```
{current_prompt}
```

## User Feedback
The user reports the following issues with the current extraction output:
```
{feedback}
```

{output_section}

## Your Task
Based on the user's feedback, generate an IMPROVED version of the extraction prompt that addresses the reported issues. The improved prompt must:
1. Keep the same general structure and JSON output format as the original
2. Address each specific issue mentioned in the feedback
3. Add clearer instructions or constraints where the original was ambiguous
4. Maintain any field names and output schema from the original prompt

Return your response in this exact JSON format:
{{
  "improved_prompt": "<the full improved extraction prompt>",
  "changes_summary": "<bullet-point summary of what you changed and why>"
}}

Return ONLY the JSON, no other text."""


@prompts_router.post("/api/v1/prompts/improve")
async def api_improve_prompt(body: PromptImproveRequest, api_key: str = Depends(check_rate_limit)):
    """Use AI to generate an improved prompt based on user feedback.

    Auto-loads the current active prompt for the given (use_case, stage),
    sends it along with user feedback to the VLM, and returns the
    improved prompt plus a summary of changes.
    """
    if not HF_TOKEN:
        raise HTTPException(status_code=503, detail="HF_TOKEN not configured")

    # 1. Auto-load the current active prompt
    current_prompt = None
    if is_enabled():
        current_prompt = get_active_prompt(body.use_case, body.stage)

    if not current_prompt:
        # Fall back to hardcoded PROMPT_LIBRARY
        current_prompt = PROMPT_LIBRARY.get(body.stage, {}).get(
            body.use_case,
            PROMPT_LIBRARY.get(body.stage, {}).get("_default", ""),
        )

    if not current_prompt:
        raise HTTPException(
            status_code=404,
            detail=f"No prompt found for use_case={body.use_case}, stage={body.stage}",
        )

    # 2. Build the meta-prompt
    output_section = ""
    if body.current_output:
        # Truncate if very long to stay within token limits
        output_text = body.current_output[:3000]
        output_section = f"""## Current Extraction Output (for reference)
```json
{output_text}
```"""

    meta_prompt = META_PROMPT_TEMPLATE.format(
        current_prompt=current_prompt,
        feedback=body.feedback,
        output_section=output_section,
    )

    # 3. Call the VLM (text-only, no image needed)
    from hf_client import create_inference_client, robust_chat_completion

    client = create_inference_client(api_key=HF_TOKEN)

    try:
        response = robust_chat_completion(
            client,
            model=body.model,
            messages=[{"role": "user", "content": meta_prompt}],
            max_tokens=4096,
        )
        result_text = response.choices[0].message.content
    except Exception as e:
        logger.error(f"Prompt improvement VLM call failed: {e}")
        raise HTTPException(
            status_code=502,
            detail=f"VLM call failed: {str(e)[:200]}",
        )

    # 4. Parse the response
    try:
        parsed = _parse_vlm_json(result_text)
        improved_prompt = parsed.get("improved_prompt", "")
        changes_summary = parsed.get("changes_summary", "")
    except Exception:
        # If parsing fails, treat the whole response as the improved prompt
        improved_prompt = result_text.strip()
        changes_summary = "AI generated an improved prompt but structured parsing failed."

    if not improved_prompt:
        raise HTTPException(
            status_code=500,
            detail="VLM returned empty improved prompt",
        )

    return {
        "current_prompt": current_prompt,
        "improved_prompt": improved_prompt,
        "changes_summary": changes_summary,
        "model": body.model,
        "use_case": body.use_case,
        "stage": body.stage,
    }