/** @odoo-module **/

import { registry } from "@web/core/registry";

const UberStatusService = {
    dependencies: ["bus_service", "action"],
    start(env, { bus_service, action }) {
        // Odoo 17 Bus Service uses addChannel and subscribe
        bus_service.addChannel("uber_status_updates");
        bus_service.subscribe("notification", (notifications) => {
            for (const { type, payload } of notifications) {
                if (type === "uber_status_updates") {
                    const currentController = action.currentController;
                    if (currentController && 
                        currentController.props.resModel === "pos.order" && 
                        currentController.props.resId === payload.order_id) {
                        
                        console.log("Uber Status Update Received. Refreshing Form...");
                        action.restore();
                    }
                }
            }
        });
    },
};

registry.category("services").add("uber_status_service", UberStatusService);
