<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="delivery_carrier_form_view_fusion_canada_post" model="ir.ui.view">
        <field name="name">delivery.carrier.form.view.fusion.canada.post</field>
        <field name="model">delivery.carrier</field>
        <field name="inherit_id" ref="delivery.view_delivery_carrier_form"/>
        <field name="arch" type="xml">

            <!-- Rename "Get Rate" → "Get Prices" on the Integration Level radio -->
            <xpath expr="//field[@name='integration_level']" position="attributes">
                <attribute name="invisible">delivery_type == 'fusion_canada_post'</attribute>
            </xpath>
            <xpath expr="//field[@name='integration_level']" position="after">
                <field name="integration_level"
                       invisible="delivery_type != 'fusion_canada_post'"
                       widget="radio"
                       options="{'horizontal': true}"
                       help="Get Prices: only fetch shipping prices on sale orders. Get Prices and Create Shipment: also generate labels and tracking when the delivery order is validated."/>
            </xpath>

            <xpath expr="//page[@name='destination']" position='before'>
                <page string="Configuration" name="configuration" invisible="delivery_type != 'fusion_canada_post'">
                    <group string="API Credentials"
                           invisible="delivery_type != 'fusion_canada_post'">
                        <group>
                            <field name="username" string="API Key"
                                   required="delivery_type == 'fusion_canada_post'"
                                   help="API key from the Canada Post Developer Program."/>
                            <field name="password" string="API Password" password="True"
                                   required="delivery_type == 'fusion_canada_post'"
                                   help="API password from the Canada Post Developer Program."/>
                            <field name="customer_number" required="delivery_type == 'fusion_canada_post'"
                                   help="Your Canada Post customer number (mailed-by number)."/>
                        </group>
                        <group>
                            <field name="fusion_cp_type" string="Account Type"
                                   required="delivery_type == 'fusion_canada_post'"/>
                            <field name="fusion_cp_contract_id" required="fusion_cp_type == 'commercial'"
                                   invisible="fusion_cp_type == 'counter'"/>
                        </group>
                    </group>
                    <group>
                        <group string="Shipping Options">
                            <field name="service_type" string="Default Service"
                                   required="delivery_type == 'fusion_canada_post'"
                                   help="Default shipping service. Users can choose a different service when adding shipping to a sale order."/>
                            <field name="option_code" string="Shipping Option"/>
                            <field name="reason_for_export"/>
                            <field name="product_packaging_id"
                                   help="Optional default package type. Dimensions will pre-fill in the shipping wizard but can be overridden per shipment."
                            />
                            <field name="fusion_cp_dimension_unit"
                                   required="delivery_type == 'fusion_canada_post'"
                                   help="Unit for package dimensions. Values are automatically converted to centimetres for the Canada Post API."/>
                        </group>
                        <group string="Payment &amp; Tracking">
                            <field
                                name="fusion_cp_payment_method"
                                string="Payment Method"
                                required="delivery_type == 'fusion_canada_post'"
                            />
                            <field name="tracking_link"
                                   help="Custom tracking URL. Leave blank to use the default Canada Post tracking page."/>
                        </group>
                    </group>
                    <group string="Label Settings"
                           invisible="delivery_type != 'fusion_canada_post'">
                        <group>
                            <field name="fusion_cp_output_format"
                                   required="delivery_type == 'fusion_canada_post'"/>
                        </group>
                    </group>
                </page>
            </xpath>
        </field>
    </record>
</odoo>
