from flask import Blueprint, request, jsonify
import oletools.oleid
from utils.file_handler import allowed_file, delete_file, save_file

oleid_bp = Blueprint('oleid', __name__)

@oleid_bp.route('/analyze', methods=['POST'])
def analyze_ole():
    if 'file' not in request.files:
        return jsonify({'error': 'No file uploaded'}), 400

    file = request.files['file']
    if file.filename == '' or not allowed_file(file.filename):
        return jsonify({'error': 'Invalid or unsupported file type'}), 400

    filepath = save_file(file)

    # Analyze with oleid
    oid = oletools.oleid.OleID(filepath)
    indicators = oid.check()
    results = {indicator.name: indicator.value for indicator in indicators}
    delete_file(filepath)

    return jsonify({'filename': file.filename, 'analysis': results})
