<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <record id="choose_delivery_carrier_view_form_fusion" model="ir.ui.view">
        <field name="name">choose.delivery.carrier.form.fusion</field>
        <field name="model">choose.delivery.carrier</field>
        <field name="inherit_id" ref="delivery.choose_delivery_carrier_view_form"/>
        <field name="arch" type="xml">
            <!-- Hidden state fields -->
            <xpath expr="//field[@name='delivery_price']" position="after">
                <field name="fusion_selected_service" invisible="1"/>
                <field name="fusion_dimension_unit_label" invisible="1"/>
                <field name="fusion_weight_unit_label" invisible="1"/>
            </xpath>

            <!-- Hide base "Get rate" button for Fusion shipping carriers -->
            <xpath expr="//button[@name='update_price']" position="attributes">
                <attribute name="invisible">delivery_type in ('fixed', 'base_on_rule', 'fusion_canada_post')</attribute>
            </xpath>

            <!-- "Get Prices" button (inside the Cost / Get-rate row) -->
            <xpath expr="//button[@name='update_price']" position="after">
                <button name="update_price" type="object"
                        invisible="delivery_type != 'fusion_canada_post'">
                    <i class="oi oi-arrow-right me-1"/>Get Prices
                </button>
            </xpath>

            <!--
                 Full-width sections AFTER the outer <group>.
                 This avoids them being crammed into the group's
                 label-field grid and gives them the full dialog width.
            -->
            <xpath expr="//form/group" position="after">
                <!-- Package list -->
                <div invisible="delivery_type != 'fusion_canada_post'"
                     class="px-3 mt-2">
                    <span class="o_form_label fw-bold mb-1">Packages</span>
                    <field name="fusion_package_ids" nolabel="1">
                        <list editable="bottom">
                            <field name="sequence" widget="handle"/>
                            <field name="package_type_id" string="Box Type"/>
                            <field name="package_length" string="L"/>
                            <field name="package_width" string="W"/>
                            <field name="package_height" string="H"/>
                            <field name="weight" string="Weight"/>
                            <field name="selected_price" string="Cost"
                                   widget="monetary"
                                   options="{'currency_field': 'currency_id'}"
                                   optional="hide"/>
                            <field name="currency_id" column_invisible="1"/>
                        </list>
                    </field>
                </div>

                <!-- Service selection -->
                <div invisible="delivery_type != 'fusion_canada_post' or not fusion_rate_ids"
                     class="px-3 mt-3">
                    <span class="o_form_label fw-bold mb-1">Select a Service</span>
                    <field name="fusion_rate_ids" nolabel="1">
                        <list create="0" delete="0" no_open="1"
                              decoration-bf="is_selected"
                              decoration-success="is_selected">
                            <field name="is_selected" column_invisible="1"/>
                            <field name="service_name" string="Service"/>
                            <field name="price" string="Shipping Cost"
                                   widget="monetary"
                                   options="{'currency_field': 'currency_id'}"/>
                            <field name="currency_id" column_invisible="1"/>
                            <field name="expected_delivery"
                                   string="Expected Delivery"/>
                            <button name="action_select" type="object"
                                    string="Select"
                                    class="btn-sm btn-link"
                                    icon="fa-circle-o"
                                    invisible="is_selected"/>
                            <button name="action_select" type="object"
                                    string="Selected"
                                    class="btn-sm btn-primary"
                                    icon="fa-check-circle"
                                    invisible="not is_selected"/>
                        </list>
                    </field>
                </div>
            </xpath>
        </field>
    </record>

</odoo>
