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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
import re
import json
import io
import os
from datetime import datetime
from pathlib import Path
from openai import OpenAI
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, ValidationError
from mcp.server.fastmcp import FastMCP
from contextlib import asynccontextmanager
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

def load_local_env() -> None:
    env_paths = [
        Path(__file__).with_name(".env"),
        Path(__file__).resolve().parent.parent / ".env",
    ]

    for env_path in env_paths:
        if not env_path.exists():
            continue
        with env_path.open("r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith("#") or "=" not in line:
                    continue
                name, value = line.split("=", 1)
                name = name.strip()
                value = value.strip().strip('"').strip("'")
                if name and name not in os.environ:
                    os.environ[name] = value


def optional_int_env(name: str):
    value = os.getenv(name, "").strip()
    return int(value) if value else None


load_local_env()

app = FastAPI()
mcp = FastMCP("Naaviverse")

allowed_origins = [
    origin.strip()
    for origin in os.getenv("CORS_ALLOW_ORIGINS", "http://localhost:5173,http://127.0.0.1:5173").split(",")
    if origin.strip()
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=allowed_origins,
    allow_methods=["*"],
    allow_headers=["*"],
)

# OpenAI / Codex API config
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "").strip()
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini").strip()
USAGE_STATS_FILE = Path(os.getenv(
    "USAGE_STATS_FILE",
    Path(__file__).with_name("openai_usage_stats.json")
))

# Initialize OpenAI client
client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None


def today_key() -> str:
    return datetime.now().strftime("%Y-%m-%d")


def empty_day_stats() -> dict:
    return {
        "total_calls"        : 0,
        "total_input_tokens" : 0,
        "total_output_tokens": 0,
        "total_tokens"       : 0,
        "calls"              : []
    }


def load_usage_stats() -> dict:
    if not USAGE_STATS_FILE.exists():
        return {}
    try:
        with USAGE_STATS_FILE.open("r", encoding="utf-8") as f:
            return json.load(f)
    except (json.JSONDecodeError, OSError):
        return {}


def save_usage_stats(stats: dict) -> None:
    USAGE_STATS_FILE.parent.mkdir(parents=True, exist_ok=True)
    with USAGE_STATS_FILE.open("w", encoding="utf-8") as f:
        json.dump(stats, f, indent=2)


usage_stats = load_usage_stats()

# ── Token usage tracker ──
token_log = {
    "total_calls"        : 0,
    "total_input_tokens" : 0,
    "total_output_tokens": 0,
    "total_tokens"       : 0,
    "calls"              : []
}
token_log = usage_stats.get(today_key(), token_log)

class GoalRequest(BaseModel):
    goal: str


class MacroView(BaseModel):
    title: str
    why_it_matters: str


class MicroView(BaseModel):
    title: str
    how_its_done: str


class NanoView(BaseModel):
    title: str
    what_you_do: str


class PathStep(BaseModel):
    id: int
    title: str
    description: str
    macro: MacroView
    micro: MicroView
    nano: NanoView


class CareerPathResponse(BaseModel):
    path_name: str
    path_description: str
    total_steps: int
    steps: list[PathStep]


BLOCKED_TOPIC_PATTERNS = [
    r"\b(love|crush|dating|date|romance|romantic|relationship|breakup|ex)\b",
    r"\b(girlfriend|boyfriend|wife|husband|marriage|marry|proposal)\b",
    r"\b(flirt|kiss|sex|sexual|hookup|hot girl|hot boy)\b",
]

EDUCATION_TOPIC_PATTERNS = [
    r"\b(learn|study|course|class|school|college|university|degree|exam|certification)\b",
    r"\b(career|job|profession|internship|resume|interview|portfolio|skill|training)\b",
    r"\b(become|switch|break into|prepare|master|improve|build)\b",
    r"\b(engineer|developer|designer|analyst|scientist|manager|teacher|doctor|nurse|lawyer)\b",
]


def ensure_education_goal(goal: str) -> None:
    normalized = goal.strip().lower()
    if not normalized:
        raise HTTPException(status_code=400, detail="Goal cannot be empty")

    if any(re.search(pattern, normalized) for pattern in BLOCKED_TOPIC_PATTERNS):
        raise HTTPException(
            status_code=400,
            detail="This agent only supports education, career, learning, and skill-building goals."
        )

    if not any(re.search(pattern, normalized) for pattern in EDUCATION_TOPIC_PATTERNS):
        raise HTTPException(
            status_code=400,
            detail="Please enter an education or career goal, such as 'Become a Data Analyst' or 'Prepare for NEET'."
        )


def ensure_document_scope(content: str) -> None:
    normalized = content.strip().lower()
    has_blocked_topic = any(re.search(pattern, normalized) for pattern in BLOCKED_TOPIC_PATTERNS)
    has_education_topic = any(re.search(pattern, normalized) for pattern in EDUCATION_TOPIC_PATTERNS)

    if has_blocked_topic and not has_education_topic:
        raise HTTPException(
            status_code=400,
            detail="Uploaded content must be related to education, career, learning, or skills."
        )


def validate_path_response(data: dict) -> dict:
    try:
        if hasattr(CareerPathResponse, "model_validate"):
            validated = CareerPathResponse.model_validate(data)
            result = validated.model_dump()
        else:
            validated = CareerPathResponse.parse_obj(data)
            result = validated.dict()
    except ValidationError as e:
        raise HTTPException(status_code=500, detail=f"Invalid path format from model: {e}")

    if result["total_steps"] != len(result["steps"]):
        result["total_steps"] = len(result["steps"])

    for index, step in enumerate(result["steps"], start=1):
        step["id"] = index

    return result


PROMPT_TEMPLATE = """You are the Naaviverse career path engine.
Generate a structured career path for this goal: "{goal}"

This agent is ONLY for education, career, learning, exam preparation, and skill-building goals.
If the goal is about love, dating, romance, girlfriend, boyfriend, marriage, entertainment gossip, or any unrelated personal topic, respond with this exact JSON:
{{
  "path_name": "UNSUPPORTED GOAL",
  "path_description": "This agent only supports education, career, learning, and skill-building goals.",
  "total_steps": 0,
  "steps": []
}}

Decide how many steps are genuinely needed:
- Simple goals (e.g. Learn Excel): 2-3 steps
- Mid-level goals (e.g. Become a UX Designer): 3-4 steps
- Complex goals (e.g. Become a Data Scientist, career switches): 4-5 steps

Respond ONLY with valid JSON. No markdown, no backticks, no explanation.

{{
  "path_name": "<specific path title in CAPS style, e.g. DATA SCIENCE CAREER PATH - INDIA>",
  "path_description": "<1 concise sentence describing who this path is for and what it achieves>",
  "total_steps": <number of steps>,
  "steps": [
    {{
      "id": 1,
      "title": "<meaningful phase name, e.g. Technology Awareness and Career Exploration>",
      "description": "<1 sentence describing what this step covers and achieves>",
      "macro": {{
        "title": "<big-picture concept title for this step>",
        "why_it_matters": "<1 concise sentence on the strategic importance of this step>"
      }},
      "micro": {{
        "title": "<specific skill or sub-topic to learn in this step>",
        "how_its_done": "<1 concise sentence on exactly how to execute with specific tools, methods, platforms>"
      }},
      "nano": {{
        "title": "<concrete hands-on task or deliverable>",
        "what_you_do": "<1 concise sentence describing the actual project or action to complete>"
      }}
    }}
  ]
}}

Rules:
- path_name must be specific to the goal β€” never generic
- Step titles must be meaningful phase names, NOT Step 1 or Phase 1
- macro = big picture strategy: why this step matters
- micro = specific skill/method: exactly what to learn with real platform names
- nano = hands-on deliverable: a real project or action to complete
- Resources in micro must be real: Coursera, freeCodeCamp, specific book titles, YouTube channels
"""

FILE_PROMPT = """You are the Naaviverse career path engine.

A user has uploaded a document. Here is the extracted text:

--- DOCUMENT START ---
{content}
--- DOCUMENT END ---

{goal_line}

Read the document carefully. Identify background, skills, education, experience.
Generate a HIGHLY PERSONALIZED career path based on their profile.
Only generate paths related to education, career, learning, exam preparation, and skill-building.

Respond ONLY with valid JSON. No markdown, no backticks, no explanation.

{{
  "path_name": "<specific path title tailored to their background>",
  "path_description": "<1 concise sentence personalized to their actual background and goal>",
  "total_steps": <number of steps needed>,
  "steps": [
    {{
      "id": 1,
      "title": "<meaningful phase name>",
      "description": "<1 sentence personalized to their background>",
      "macro": {{
        "title": "<big-picture concept title>",
        "why_it_matters": "<1 concise sentence on why this matters given their starting point>"
      }},
      "micro": {{
        "title": "<specific skill to learn>",
        "how_its_done": "<1 concise sentence on exactly how to learn it>"
      }},
      "nano": {{
        "title": "<concrete task or project>",
        "what_you_do": "<1 concise sentence on the actual hands-on thing to build or do>"
      }}
    }}
  ]
}}
"""


def call_openai(prompt: str, label: str = "goal") -> dict:
    global token_log, client

    api_key = os.getenv("OPENAI_API_KEY", "").strip() or OPENAI_API_KEY
    if not api_key:
        raise HTTPException(
            status_code=500,
            detail="OpenAI API key missing. Set OPENAI_API_KEY environment variable."
        )

    # Re-initialize client if key was set after startup (e.g. Hugging Face secrets)
    if client is None or client.api_key != api_key:
        client = OpenAI(api_key=api_key)

    try:
        response = client.chat.completions.create(
            model=os.getenv("OPENAI_MODEL", OPENAI_MODEL).strip(),
            messages=[
                {
                    "role": "system",
                    "content": "You are a career path engine. Respond ONLY with valid JSON. No markdown, no backticks, no explanation whatsoever."
                },
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            max_tokens=4096,
            temperature=0.7,
        )
    except Exception as e:
        error_msg = str(e)
        if "401" in error_msg or "Unauthorized" in error_msg:
            raise HTTPException(status_code=401, detail="Invalid OpenAI API key. Check your OPENAI_API_KEY.")
        if "429" in error_msg or "rate" in error_msg.lower():
            raise HTTPException(status_code=429, detail="OpenAI rate limit hit. Please wait and try again.")
        if "insufficient_quota" in error_msg:
            raise HTTPException(status_code=429, detail="OpenAI quota exceeded. Check your billing.")
        raise HTTPException(status_code=500, detail=f"OpenAI API error: {error_msg}")

    # Extract token usage
    usage = response.usage
    input_tokens = usage.prompt_tokens if usage else 0
    output_tokens = usage.completion_tokens if usage else 0
    total_tokens = usage.total_tokens if usage else (input_tokens + output_tokens)
    current_day = today_key()
    token_log = usage_stats.get(current_day, empty_day_stats())

    # ── Update running totals ──
    token_log["total_calls"]         += 1
    token_log["total_input_tokens"]  += input_tokens
    token_log["total_output_tokens"] += output_tokens
    token_log["total_tokens"]        += total_tokens
    token_log["calls"].append({
        "call"          : token_log["total_calls"],
        "type"          : label,
        "input_tokens"  : input_tokens,
        "output_tokens" : output_tokens,
        "total_tokens"  : total_tokens,
    })

    # ── Print to terminal ──
    usage_stats[current_day] = token_log
    save_usage_stats(usage_stats)

    model_used = os.getenv("OPENAI_MODEL", OPENAI_MODEL).strip()
    print("\n" + "="*55)
    print(f"  CALL #{token_log['total_calls']}  [{label}]")
    print("="*55)
    print(f"  Input tokens   : {input_tokens:,}")
    print(f"  Output tokens  : {output_tokens:,}")
    print(f"  Total this call: {total_tokens:,}")
    print("-"*55)
    print(f"  RUNNING TOTALS:")
    print(f"  Total calls    : {token_log['total_calls']}")
    print(f"  Input tokens   : {token_log['total_input_tokens']:,}")
    print(f"  Output tokens  : {token_log['total_output_tokens']:,}")
    print(f"  Total tokens   : {token_log['total_tokens']:,}")
    print(f"  Provider       : OpenAI")
    print(f"  Model          : {model_used}")
    print(f"  Used this run  : {token_log['total_calls']} calls")
    print("="*55 + "\n")

    try:
        raw = response.choices[0].message.content
    except (IndexError, AttributeError):
        raise HTTPException(status_code=500, detail="OpenAI returned no text content.")

    raw = re.sub(r"```(?:json)?", "", raw).strip().strip("`")
    result = validate_path_response(json.loads(raw))

    # Attach token info to response so frontend can see it too
    result["_token_usage"] = {
        "this_call": {
            "input_tokens" : input_tokens,
            "output_tokens": output_tokens,
            "total_tokens" : total_tokens,
        },
        "running_total": {
            "calls"         : token_log["total_calls"],
            "input_tokens"  : token_log["total_input_tokens"],
            "output_tokens" : token_log["total_output_tokens"],
            "total_tokens"  : token_log["total_tokens"],
        }
    }
    return result


def extract_text_from_pdf(file_bytes: bytes) -> str:
    try:
        import pdfplumber
        with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
            pages = [page.extract_text() or "" for page in pdf.pages]
            return "\n".join(pages).strip()
    except ImportError:
        raise HTTPException(status_code=500, detail="Run: pip install pdfplumber")


def extract_text_from_docx(file_bytes: bytes) -> str:
    try:
        import docx
        doc = docx.Document(io.BytesIO(file_bytes))
        return "\n".join([p.text for p in doc.paragraphs]).strip()
    except ImportError:
        raise HTTPException(status_code=500, detail="Run: pip install python-docx")


def extract_text_from_image(file_bytes: bytes) -> str:
    try:
        from PIL import Image
        import pytesseract
        img = Image.open(io.BytesIO(file_bytes))
        return pytesseract.image_to_string(img).strip()
    except ImportError:
        try:
            from PIL import Image
            img = Image.open(io.BytesIO(file_bytes))
            w, h = img.size
            return f"[Image {w}x{h}px β€” OCR not available. Type your goal manually.]"
        except Exception:
            return "[Image could not be read.]"
    except Exception as e:
        return f"[Image extraction failed: {str(e)}]"


@mcp.tool()
async def generate_career_path(goal: str):
    """
    Generate a personalized career path.
    """

    ensure_education_goal(goal)

    prompt = PROMPT_TEMPLATE.format(goal=goal.strip())

    return call_openai(prompt, label=goal[:40])


# ── Endpoint 1: Text goal ─────────────────────────────────────────
@app.post("/api/path")
async def generate_path(req: GoalRequest):
    ensure_education_goal(req.goal)
    try:
        prompt = PROMPT_TEMPLATE.format(goal=req.goal.strip())
        return call_openai(prompt, label=req.goal.strip()[:40])
    except HTTPException:
        raise
    except json.JSONDecodeError as e:
        raise HTTPException(status_code=500, detail=f"Invalid JSON from model: {e}")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"⚠ {str(e)}")


# ── Endpoint 2: File upload ───────────────────────────────────────
@app.post("/api/path-from-file")
async def generate_path_from_file(
    file: UploadFile = File(...),
    goal: str = Form("")
):
    file_bytes = await file.read()
    content_type = file.content_type or ""
    filename = file.filename or ""
    content = ""

    if content_type == "application/pdf" or filename.endswith(".pdf"):
        content = extract_text_from_pdf(file_bytes)
    elif "wordprocessingml" in content_type or filename.endswith(".docx"):
        content = extract_text_from_docx(file_bytes)
    elif content_type == "text/plain" or filename.endswith(".txt"):
        content = file_bytes.decode("utf-8", errors="ignore")
    elif content_type.startswith("image/") or filename.lower().endswith((".png", ".jpg", ".jpeg")):
        content = extract_text_from_image(file_bytes)
    else:
        raise HTTPException(status_code=400, detail=f"Unsupported file type: {content_type}")

    if not content.strip():
        raise HTTPException(status_code=400, detail="Could not extract text. Please type your goal manually.")

    if goal.strip():
        ensure_education_goal(goal)
    else:
        ensure_document_scope(content)

    content = content[:3000]
    goal_line = (
        f"The user's stated career goal is: {goal.strip()}"
        if goal.strip()
        else "Based on the document, infer the most suitable career goal for this person."
    )

    try:
        prompt = FILE_PROMPT.format(content=content, goal_line=goal_line)
        return call_openai(prompt, label=f"file:{filename[:30]}")
    except HTTPException:
        raise
    except json.JSONDecodeError as e:
        raise HTTPException(status_code=500, detail=f"Invalid JSON from model: {e}")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"⚠ {str(e)}")


# ── Endpoint 3: Token stats ───────────────────────────────────────
@app.get("/api/token-stats")
async def get_token_stats():
    all_usage_stats = load_usage_stats()
    today = today_key()
    today_stats = all_usage_stats.get(today, token_log if token_log["total_calls"] else empty_day_stats())

    return {
        "provider"           : "openai",
        "model"              : OPENAI_MODEL,
        "date"               : today,
        "stats_source"       : str(USAGE_STATS_FILE),
        "total_calls"        : today_stats["total_calls"],
        "total_input_tokens" : today_stats["total_input_tokens"],
        "total_output_tokens": today_stats["total_output_tokens"],
        "total_tokens_used"  : today_stats["total_tokens"],
        "quota_note"         : "Using OpenAI API. Token usage is tracked per call.",
        "call_history"       : today_stats["calls"],
        "daily_usage_history": all_usage_stats
    }
app.mount("/mcp", mcp.sse_app())

# ── Serve Frontend ────────────────────────────────────────────────
frontend_dist = Path(__file__).resolve().parent.parent / "Agent_Frontend" / "dist"
if frontend_dist.exists():
    app.mount("/assets", StaticFiles(directory=str(frontend_dist / "assets")), name="assets")
    
    @app.get("/{full_path:path}")
    async def serve_frontend(full_path: str):
        file_path = frontend_dist / full_path
        if file_path.exists() and file_path.is_file():
            return FileResponse(file_path)
        return FileResponse(frontend_dist / "index.html")