import librosa
import numpy as np
from sklearn.preprocessing import StandardScaler

def extract_features(audio_path):
    y, sr = librosa.load(audio_path, sr=16000)
    mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
    return np.mean(mfcc.T, axis=0)

def detect_emotion(audio_path):
    features = extract_features(audio_path)

    # MVP logic (rule-based threshold)
    energy = np.mean(np.abs(features))

    if energy > 50:
        emotion = "Angry"
    elif energy > 20:
        emotion = "Frustrated"
    else:
        emotion = "Calm"

    return {
        "emotion": emotion,
        "energy_score": float(energy)
    }
