"""Orders API handlers."""

from flask import Flask, request, jsonify

from order_app.services.order_service import OrderService
from order_app.repositories.order_repository import OrderRepository


def create_app() -> Flask:
    app = Flask(__name__)
    repo = OrderRepository()
    service = OrderService(repo)

    @app.route("/orders", methods=["POST"])
    def create_order():
        data = request.get_json() or {}
        order = service.create_order(
            product_id=data.get("product_id"),
            quantity=data.get("quantity", 1),
        )
        return jsonify({"order_id": order.id, "status": order.status}), 201

    @app.route("/orders/<order_id>", methods=["GET"])
    def get_order(order_id: str):
        order = service.get_order(order_id)
        if order is None:
            return jsonify({"error": "not found"}), 404
        return jsonify({"id": order.id, "status": order.status})

    return app
