import os
import json
from pathlib import Path
from openai import OpenAI
from file_reader import read_file_content
from dotenv import load_dotenv

load_dotenv(override=True)
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

CLASSIFY_PROMPT = """You are a pharma document classifier.

Classify the uploaded document based on its content.

Return JSON only with these exact fields:
{
  "filename": "<filename>",
  "document_type": one of ["SOP", "Batch Record", "Equipment Log", "LIMS Results", "Maintenance Log", "Deviation Report", "Unknown"],
  "document_type_code": one of ["SOP", "BMR", "EQP", "LIMS", "MAINT", "DEV", "UNKNOWN"],
  "confidence": number between 0 and 100,
  "batch_id": string or null,
  "product_name": string or null,
  "equipment_id": string or null,
  "plant": string or null,
  "date_range": string or null,
  "key_fields_found": array of strings,
  "missing_context": array of strings
}

Document filename: <filename>
Document content:
<content>
"""


async def classify_document(file_path: Path) -> dict:
    try:
        content = read_file_content(file_path)

        prompt = CLASSIFY_PROMPT.replace("<filename>", file_path.name)
        prompt = prompt.replace("<content>", content[:4000])

        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[
                {
                    "role": "system",
                    "content": "You are a pharma document classifier. Return JSON only. No extra text."
                },
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            response_format={"type": "json_object"},
            temperature=0
        )

        raw = response.choices[0].message.content
        result = json.loads(raw)
        result["filename"] = file_path.name
        result["status"] = "classified"

        # Calculate verification_status for Screen 1 badge
        confidence = result.get("confidence", 0)
        if confidence >= 85:
            result["verification_status"] = "VERIFIED"
        elif confidence >= 60:
            result["verification_status"] = "NEEDS REVIEW"
        else:
            result["verification_status"] = "LOW CONFIDENCE"

        return result

    except json.JSONDecodeError as e:
        return {
            "filename": file_path.name,
            "document_type": "Unknown",
            "document_type_code": "UNKNOWN",
            "confidence": 0,
            "verification_status": "LOW CONFIDENCE",
            "status": "parse_error",
            "error": str(e)
        }
    except Exception as e:
        return {
            "filename": file_path.name,
            "document_type": "Unknown",
            "document_type_code": "UNKNOWN",
            "confidence": 0,
            "verification_status": "LOW CONFIDENCE",
            "status": "error",
            "error": str(e)
        }