# request_service.py
from flask import Flask, render_template, request, jsonify
import pika
import json

app = Flask(__name__)

# RabbitMQ setup
RABBITMQ_HOST = 'localhost'
connection = pika.BlockingConnection(pika.ConnectionParameters(RABBITMQ_HOST))
channel = connection.channel()
channel.queue_declare(queue='rental_requests')

# Database simulation
inventory = {
    'perceuse': 5,
    'tondeuse': 3,
    'ponceuse': 4,
    'scie': 2
}

@app.route('/')
def home():
    return render_template('rental_form.html')

@app.route('/submit_rental', methods=['POST'])
def submit_rental():
    rental_data = {
        'product': request.form['product'],
        'quantity': int(request.form['quantity']),
        'customer_name': request.form['customer_name'],
        'rental_duration': int(request.form['duration'])
    }
    
    # Send message to inventory service
    channel.basic_publish(
        exchange='',
        routing_key='rental_requests',
        body=json.dumps(rental_data)
    )
    
    return jsonify({"message": "Demande de location envoyée avec succès!"})

if __name__ == '__main__':
    app.run(port=5000)
