/** @odoo-module */

import { ReceiptScreen } from "@point_of_sale/app/screens/receipt_screen/receipt_screen";
import { patch } from "@web/core/utils/patch";
import { useService } from "@web/core/utils/hooks";

patch(ReceiptScreen.prototype, {
    setup() {
        super.setup(...arguments);
        this.orm = useService("orm");
        this.notification = useService("notification");
    },
    async requestUber() {
        const order = this.props.order;
        const serverId = order.server_id;

        if (!serverId) {
            this.notification.add("Wait! This order hasn't been sent to the server yet. Please wait a second.", {
                title: "Uber Integration",
                type: "warning",
            });
            return;
        }

        try {
            await this.orm.call("pos.order", "action_request_uber_delivery", [[serverId]]);
            this.notification.add("Uber Direct delivery requested successfully!", {
                title: "Uber Integration",
                type: "success",
            });
            // Disable the button or change text if needed
        } catch (error) {
            const message = error.message?.data?.message || "Check server logs for details.";
            this.notification.add("Failed to request Uber: " + message, {
                title: "Uber Error",
                type: "danger",
            });
        }
    }
});
