<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Inherit pos.order list view to show channel fields -->
    <record id="view_pos_order_channels_list" model="ir.ui.view">
        <field name="name">pos.order.channels.list</field>
        <field name="model">pos.order</field>
        <field name="inherit_id" ref="point_of_sale.view_pos_order_tree"/>
        <field name="arch" type="xml">
            <field name="amount_total" position="before">
                <field name="order_source" string="Source" widget="badge"
                       decoration-info="order_source == 'online'"
                       decoration-warning="order_source == 'phone' or order_source == 'whatsapp'"
                       decoration-primary="order_source == 'kiosk' or order_source == 'qr'"
                       decoration-muted="order_source == 'walk_in'"/>
                <field name="fulfilment_type" string="Fulfilment" widget="badge"
                       decoration-success="fulfilment_type == 'pickup'"
                       decoration-info="fulfilment_type == 'delivery'"
                       decoration-muted="fulfilment_type == 'dine_in'"/>
            </field>
        </field>
    </record>

    <!-- Inherit pos.order form view -->
    <record id="view_pos_order_channels_form" model="ir.ui.view">
        <field name="name">pos.order.channels.form</field>
        <field name="model">pos.order</field>
        <field name="inherit_id" ref="point_of_sale.view_pos_pos_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='state']" position="after">
                <field name="order_source" string="Order Source" widget="badge"/>
                <field name="fulfilment_type" string="Fulfilment" widget="badge"/>
            </xpath>
            <!-- Delivery section in the Notes tab area -->
            <xpath expr="//field[@name='note']" position="before">
                <group string="Delivery Details" invisible="fulfilment_type != 'delivery'">
                    <field name="delivery_partner_id" string="Saved Address"/>
                    <field name="delivery_street"/>
                    <field name="delivery_city"/>
                    <field name="delivery_zip"/>
                    <field name="delivery_phone"/>
                    <field name="delivery_notes"/>
                </group>
                <group string="WhatsApp" invisible="order_source != 'whatsapp'">
                    <field name="whatsapp_number"/>
                </group>
                <group string="Social Media" invisible="order_source != 'social_media'">
                    <field name="social_ref"/>
                </group>
            </xpath>
        </field>
    </record>

    <!-- POS Config settings - add channel settings to existing POS config form -->
    <!-- Note: We skip config view inheritance to avoid xpath issues with pos_self_order -->

    <!-- Action: All Orders by Channel -->
    <record id="action_pos_orders_by_channel" model="ir.actions.act_window">
        <field name="name">Orders by Channel</field>
        <field name="res_model">pos.order</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{'search_default_group_source': 1}</field>
    </record>

    <!-- Search view extension for group-by Source/Fulfilment -->
    <record id="view_pos_order_channels_search" model="ir.ui.view">
        <field name="name">pos.order.channels.search</field>
        <field name="model">pos.order</field>
        <field name="inherit_id" ref="point_of_sale.view_pos_order_filter"/>
        <field name="arch" type="xml">
            <search>
                <field name="order_source"/>
                <field name="fulfilment_type"/>
                <filter name="filter_phone"     string="Phone Orders"    domain="[('order_source','=','phone')]"/>
                <filter name="filter_whatsapp"  string="WhatsApp Orders" domain="[('order_source','=','whatsapp')]"/>
                <filter name="filter_online"    string="Online Orders"   domain="[('order_source','=','online')]"/>
                <filter name="filter_kiosk"     string="Kiosk Orders"   domain="[('order_source','=','kiosk')]"/>
                <filter name="filter_delivery"  string="Delivery"       domain="[('fulfilment_type','=','delivery')]"/>
                <filter name="filter_pickup"    string="Pickup"         domain="[('fulfilment_type','=','pickup')]"/>
                <group expand="0" string="Group By">
                    <filter name="group_source"      string="Order Source"    context="{'group_by': 'order_source'}"/>
                    <filter name="group_fulfilment"  string="Fulfilment Type" context="{'group_by': 'fulfilment_type'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- Menu item in POS menu -->
    <menuitem id="menu_pos_orders_by_channel"
              name="Orders by Channel"
              parent="point_of_sale.menu_point_of_sale"
              action="action_pos_orders_by_channel"
              sequence="25"/>
</odoo>
