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

    <!-- Extend Sale Order Form to add Portal Documents Tab and Message Buttons -->
    <record id="view_sale_order_form_portal_docs" model="ir.ui.view">
        <field name="name">sale.order.form.portal.docs</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="priority">99</field>
        <field name="arch" type="xml">
            <!-- Message Authorizer button moved to chatter topbar (icon-only) -->
            <!-- See static/src/js/chatter_message_authorizer.js -->
            
            <xpath expr="//page[@name='other_information']" position="after">
                <page string="Portal" name="portal_info">
                    <group>
                        <group string="Portal Activity">
                            <field name="portal_document_count" string="Documents Uploaded"/>
                            <field name="portal_comment_count" string="Comments"/>
                        </group>
                        <group string="Source">
                            <field name="assessment_id" readonly="1"/>
                        </group>
                    </group>
                    <group>
                        <button name="action_view_portal_documents"
                                type="object"
                                string="View Portal Documents"
                                class="btn-secondary"
                                icon="fa-file-pdf-o"
                                invisible="portal_document_count == 0"/>
                        <button name="action_view_portal_comments"
                                type="object"
                                string="View Portal Comments"
                                class="btn-secondary"
                                icon="fa-comments"
                                invisible="portal_comment_count == 0"/>
                    </group>
                </page>
            </xpath>

            <!-- Filter authorizer dropdown to only show actual authorizers -->
            <xpath expr="//field[@name='x_fc_authorizer_id']" position="attributes">
                <attribute name="domain">[('is_company', '=', False), ('is_authorizer', '=', True)]</attribute>
            </xpath>
        </field>
    </record>

</odoo>
