# ============================================================
# inference.py
# Kinetic Cosmos CSC Chatbot Intent Detection
# Python 3.9 compatible
# ============================================================

import re


INTENTS = {
    "greeting": [
        "hi",
        "hello",
        "hey",
        "namaste",
        "namaskar",
        "good morning",
        "good afternoon",
        "good evening",
    ],

    "government_forms": [
        "form",
        "forms",
        "government form",
        "govt form",
        "application form",
        "sarkari form",
        "online form",
    ],

    "jobs": [
        "job",
        "jobs",
        "government job",
        "govt job",
        "sarkari job",
        "vacancy",
        "recruitment",
        "bharti",
        "rojgar",
    ],

    "scholarship": [
        "scholarship",
        "scholarships",
        "student scholarship",
        "student scholarship",
        "scholarship form",
        "scholarship scheme",
    ],

    "admit_card": [
        "admit card",
        "admitcard",
        "hall ticket",
        "hallticket",
        "exam admit card",
        "download admit card",
    ],

    "result": [
        "result",
        "results",
        "exam result",
        "exam results",
        "score card",
        "scorecard",
        "result check",
    ],

    "application_status": [
        "application status",
        "application ka status",
        "form status",
        "form ka status",
        "status check",
        "my application",
        "meri application",
        "application kaha hai",
        "application kahan hai",
    ],

    "services": [
        "service",
        "services",
        "csc service",
        "csc services",
        "available services",
        "kaunsi services",
        "konsi services",
        "service list",
    ],

    "profile": [
        "profile",
        "my profile",
        "meri profile",
        "profile edit",
        "profile update",
        "account",
        "my account",
    ],
}


def normalize_text(text):
    """
    Normalize user message.
    """

    if not text:
        return ""

    text = str(text).lower().strip()

    text = re.sub(
        r"[^a-z0-9\s]",
        " ",
        text,
    )

    text = re.sub(
        r"\s+",
        " ",
        text,
    )

    return text


def calculate_score(
    message,
    keywords,
):
    """
    Calculate simple keyword score.
    """

    score = 0

    for keyword in keywords:

        keyword = normalize_text(keyword)

        if not keyword:
            continue

        # Exact phrase gets higher score
        if keyword in message:

            if " " in keyword:
                score += 2
            else:
                score += 1

    return score


def predict_intent(message):
    """
    Predict chatbot intent.

    Returns:
    {
        "intent": "...",
        "confidence": 0.0
    }
    """

    text = normalize_text(message)

    if not text:
        return {
            "intent": "unknown",
            "confidence": 0.0,
        }

    best_intent = "unknown"
    best_score = 0

    for intent, keywords in INTENTS.items():

        score = calculate_score(
            text,
            keywords,
        )

        if score > best_score:

            best_score = score
            best_intent = intent

    # --------------------------------------------------------
    # Confidence
    # --------------------------------------------------------

    if best_score <= 0:

        confidence = 0.0

    elif best_score == 1:

        confidence = 0.65

    elif best_score == 2:

        confidence = 0.80

    else:

        confidence = 0.92

    return {
        "intent": best_intent,
        "confidence": confidence,
    }
