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

    <!-- ═══════════════════════════════════════════════════════════
         Product Form: Two-column layout
         LEFT  = Brand/Classification + Taxes (moved fields)
         RIGHT = Pricing only (Sales Price, Margin, Profit, Cost)
         ═══════════════════════════════════════════════════════════ -->

    <record id="view_product_template_form_inherit_fi" model="ir.ui.view">
        <field name="name">product.template.form.fusion.inventory</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">

            <!-- ── Make fields required ── -->
            <xpath expr="//field[@name='type']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>
            <xpath expr="//field[@name='list_price']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>
            <xpath expr="//field[@name='standard_price']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>
            <xpath expr="//field[@name='categ_id']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>
            <xpath expr="//field[@name='default_code']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>

            <!-- ═════════════════════════════════════════════════════
                 RIGHT COLUMN: Margin + Profit above Cost
                 (everything else pricing-related stays here)
                 ═════════════════════════════════════════════════════ -->
            <xpath expr="//label[@id='standard_price_label']" position="before">
                <label for="x_fi_margin_pct" string="Margin"/>
                <div name="margin_pct_uom" class="d-flex align-items-center">
                    <field name="x_fi_margin_pct" class="oe_inline"
                           style="max-width: 80px;"/>
                    <span class="ms-1 text-muted">%</span>
                </div>
                <label for="x_fi_profit_amount" string="Profit"/>
                <div name="profit_amount_wrapper">
                    <field name="x_fi_profit_amount" class="oe_inline"
                           widget="monetary" readonly="1"
                           options="{'currency_field': 'currency_id'}"/>
                </div>
            </xpath>

            <!-- ═════════════════════════════════════════════════════
                 LEFT COLUMN: move fields from right, add sections
                 ═════════════════════════════════════════════════════ -->
            <xpath expr="//group[@name='group_general']" position="inside">

                <!-- ── Brand &amp; Classification ── -->
                <separator string="Brand &amp; Classification"/>
                <field name="x_fi_brand_ids" widget="many2many_tags"
                       options="{'color_field': 'id'}"
                       placeholder="Select brand(s)..."/>
                <label for="x_fi_expected_cost" string="Expected Cost"
                       invisible="not x_fi_brand_ids"/>
                <div class="d-flex align-items-center"
                     invisible="not x_fi_brand_ids">
                    <field name="x_fi_expected_cost" class="oe_inline"
                           widget="monetary" readonly="1"
                           options="{'currency_field': 'currency_id'}"/>
                    <span class="ms-2 text-muted fst-italic">(from brand discount)</span>
                </div>

                <xpath expr="//field[@name='categ_id']" position="move"/>
                <xpath expr="//field[@name='default_code']" position="move"/>
                <xpath expr="//field[@name='barcode']" position="move"/>
                <xpath expr="//label[@for='base_unit_count']" position="move"/>
                <xpath expr="//div[@name='base_unit_price']" position="move"/>
                <xpath expr="//group[@name='group_standard_price']/field[@name='company_id']" position="move"/>

                <!-- ── Taxes ── -->
                <separator string="Taxes"/>
                <xpath expr="//label[@for='taxes_id']" position="move"/>
                <xpath expr="//div[@name='taxes_div']" position="move"/>
                <xpath expr="//field[@name='supplier_taxes_id']" position="move"/>

            </xpath>

            <!-- ── Shipping Cost below Cost ── -->
            <xpath expr="//div[@name='standard_price_uom']" position="after">
                <label for="x_fi_shipping_cost" string="Shipping Cost"/>
                <div name="shipping_cost_wrapper">
                    <field name="x_fi_shipping_cost" class="oe_inline"
                           widget="monetary"
                           options="{'currency_field': 'currency_id'}"/>
                </div>
            </xpath>

            <!-- ── Apply Margin to Variants button in right column ── -->
            <xpath expr="//group[@name='group_standard_price']" position="inside">
                <div colspan="2" class="mt-2"
                     invisible="product_variant_count &lt;= 1">
                    <button name="action_apply_margin_to_all_variants"
                            type="object"
                            string="Apply Margin to All Variants"
                            class="btn-secondary btn-sm"
                            icon="fa-refresh"
                            confirm="Apply the template margin to all non-overridden variants? Overridden variants will be skipped."/>
                </div>
            </xpath>

            <!-- ── Fusion Inventory tab ── -->
            <xpath expr="//notebook" position="inside">
                <page string="Fusion Inventory" name="fusion_inventory_tab">
                    <group string="Name Case Conversion">
                        <field name="x_fi_case_conversion" widget="radio"/>
                    </group>
                    <group string="Remote Inventory" invisible="not has_remote_mapping">
                        <group>
                            <field name="remote_qty_available"/>
                            <field name="remote_qty_forecast"/>
                        </group>
                    </group>
                </page>
            </xpath>

            <!-- ── Purchase History tab ── -->
            <xpath expr="//notebook" position="inside">
                <page string="Purchase History" name="purchase_history_tab">
                    <div class="d-flex align-items-center mb-3">
                        <h3 class="mb-0">Vendor Bill History</h3>
                        <button name="action_view_purchase_history" type="object"
                                string="Open Full History" class="btn-link ms-3"
                                icon="fa-external-link"/>
                        <button name="action_refresh_cost_from_bills" type="object"
                                string="Refresh Cost from Latest Bill"
                                class="btn-secondary ms-3"
                                icon="fa-refresh"
                                confirm="Update this product's cost to the latest vendor bill price?"/>
                    </div>
                    <field name="x_fi_purchase_history_ids" readonly="1" nolabel="1">
                        <list limit="50" default_order="date desc">
                            <field name="move_id" string="Bill Number"/>
                            <field name="date" string="Date"/>
                            <field name="partner_id" string="Vendor"/>
                            <field name="price_unit" string="Unit Cost"/>
                            <field name="quantity"/>
                            <field name="x_fi_suggested_price" string="Suggested Price"/>
                        </list>
                    </field>
                </page>
            </xpath>

        </field>
    </record>

    <!-- ═══════════════════════════════════════════════════════════
         Taxes: make required
         ═══════════════════════════════════════════════════════════ -->

    <record id="view_product_template_form_inherit_fi_taxes" model="ir.ui.view">
        <field name="name">product.template.form.fusion.inventory.taxes</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="account.product_template_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='taxes_id']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>
            <xpath expr="//field[@name='supplier_taxes_id']" position="attributes">
                <attribute name="required">1</attribute>
            </xpath>
        </field>
    </record>

</odoo>
