from flask import session, request
from datetime import datetime

def audit_log(action: str, detail: str = ""):
    email = session.get("user_email", "anonymous")
    ip = request.remote_addr

    line = f"{datetime.utcnow().isoformat()} | {email} | {ip} | {action} | {detail}\n"

    with open("audit.log", "a", encoding="utf-8") as f:
        f.write(line)