/** @odoo-module */

import { patch } from "@web/core/utils/patch";
import { Order } from "@point_of_sale/app/store/models";

/**
 * Patch the POS Order model to carry channel data.
 * These values are sent to the backend via _order_fields which we overrode in Python.
 */
patch(Order.prototype, {
    setup(_defaultObj, options) {
        super.setup(...arguments);
        // Initialize with POS config defaults
        const config = this.pos.config;
        this.order_source = config.default_order_source || 'walk_in';
        this.fulfilment_type = config.default_fulfilment_type || 'dine_in';
        this.delivery_street = '';
        this.delivery_city = '';
        this.delivery_zip = '';
        this.delivery_phone = '';
        this.delivery_notes = '';
        this.whatsapp_number = '';
        this.social_ref = '';
        this.telephone_number = '';
    },

    export_as_JSON() {
        const json = super.export_as_JSON(...arguments);
        json.order_source = this.order_source;
        json.fulfilment_type = this.fulfilment_type;
        json.delivery_street = this.delivery_street;
        json.delivery_city = this.delivery_city;
        json.delivery_zip = this.delivery_zip;
        json.delivery_phone = this.delivery_phone;
        json.delivery_notes = this.delivery_notes;
        json.whatsapp_number = this.whatsapp_number;
        json.social_ref = this.social_ref;
        json.telephone_number = this.telephone_number;
        return json;
    },

    init_from_JSON(json) {
        super.init_from_JSON(...arguments);
        this.order_source = json.order_source || 'walk_in';
        this.fulfilment_type = json.fulfilment_type || 'dine_in';
        this.delivery_street = json.delivery_street || '';
        this.delivery_city = json.delivery_city || '';
        this.delivery_zip = json.delivery_zip || '';
        this.delivery_phone = json.delivery_phone || '';
        this.delivery_notes = json.delivery_notes || '';
        this.whatsapp_number = json.whatsapp_number || '';
        this.social_ref = json.social_ref || '';
        this.telephone_number = json.telephone_number || '';
    },

    setOrderSource(source) {
        this.order_source = source;
    },

    setFulfilmentType(type) {
        this.fulfilment_type = type;
    },

    setDeliveryAddress(fields) {
        this.delivery_street = fields.street || '';
        this.delivery_city = fields.city || '';
        this.delivery_zip = fields.zip || '';
        this.delivery_phone = fields.phone || '';
        this.delivery_notes = fields.notes || '';
    },

    export_for_printing() {
        const result = super.export_for_printing(...arguments);

        const SOURCE_LABELS = {
            walk_in: 'Walk-In',
            phone: 'Phone',
            whatsapp: 'WhatsApp',
            social_media: 'Social',
            online: 'Online',
            kiosk: 'Kiosk',
            qr: 'QR Code',
            platform: 'Platform',
        };

        const FULFILMENT_LABELS = {
            dine_in: 'Dine-In',
            pickup: 'Pickup',
            delivery: 'Delivery',
        };

        result.order_source = this.order_source;
        result.order_source_label = SOURCE_LABELS[this.order_source] || this.order_source;
        result.fulfilment_type = this.fulfilment_type;
        result.fulfilment_type_label = FULFILMENT_LABELS[this.fulfilment_type] || this.fulfilment_type;
        result.delivery_street = this.delivery_street;
        result.delivery_city = this.delivery_city;
        result.delivery_zip = this.delivery_zip;
        result.delivery_phone = this.delivery_phone;
        result.delivery_notes = this.delivery_notes;
        result.whatsapp_number = this.whatsapp_number;
        result.social_ref = this.social_ref;
        result.telephone_number = this.telephone_number;

        return result;
    },
});
