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

    <!-- Extend Partner Form to add Portal Role fields -->
    <record id="view_partner_form_portal_roles" model="ir.ui.view">
        <field name="name">res.partner.form.portal.roles</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            <!-- Add Send Portal Invitation button to the button box -->
            <xpath expr="//div[@name='button_box']" position="inside">
                <button name="action_grant_portal_access"
                        type="object"
                        string="Send Portal Invitation"
                        class="oe_stat_button"
                        icon="fa-envelope"
                        invisible="authorizer_portal_user_id or not email"/>
            </xpath>
            
            <xpath expr="//page[@name='internal_notes']" position="after">
                <page string="Portal Access" name="portal_access">
                    <group>
                        <group string="Portal Roles">
                            <field name="is_authorizer"/>
                            <field name="is_sales_rep_portal"/>
                            <field name="is_client_portal"/>
                            <field name="is_technician_portal"/>
                            <field name="authorizer_portal_user_id" readonly="1"/>
                            <field name="portal_access_status" widget="badge"
                                   decoration-danger="portal_access_status == 'no_access'"
                                   decoration-warning="portal_access_status == 'invited'"
                                   decoration-success="portal_access_status == 'active'"/>
                        </group>
                        <group string="Statistics" invisible="not is_authorizer and not is_sales_rep_portal and not is_technician_portal">
                            <field name="assigned_case_count" invisible="not is_authorizer and not is_sales_rep_portal"/>
                            <field name="assessment_count" invisible="not is_authorizer and not is_sales_rep_portal"/>
                            <field name="assigned_delivery_count" invisible="not is_technician_portal"/>
                        </group>
                    </group>
                    <group invisible="not is_technician_portal">
                        <group string="Technician Settings">
                            <field name="x_fc_start_address" placeholder="e.g. 123 Main St, Brampton, ON"/>
                        </group>
                    </group>
                    <group>
                        <button name="action_grant_portal_access" 
                                type="object" 
                                string="Send Portal Invitation"
                                class="btn-primary"
                                invisible="authorizer_portal_user_id or not email"
                                icon="fa-envelope"/>
                        <button name="action_resend_portal_invitation" 
                                type="object" 
                                string="Resend Portal Invitation"
                                class="btn-secondary"
                                invisible="not authorizer_portal_user_id"
                                icon="fa-refresh"/>
                        <button name="action_view_assigned_cases"
                                type="object"
                                string="View Assigned Cases"
                                class="btn-secondary"
                                invisible="assigned_case_count == 0"
                                icon="fa-list"/>
                        <button name="action_view_assessments"
                                type="object"
                                string="View Assessments"
                                class="btn-secondary"
                                invisible="assessment_count == 0"
                                icon="fa-clipboard"/>
                    </group>
                    
                    <!-- Warning if no email -->
                    <div class="alert alert-warning" role="alert" invisible="email">
                        <strong>Email Required:</strong> An email address is required to send a portal invitation.
                    </div>
                    
                    <!-- Info if already has access -->
                    <div class="alert alert-success" role="alert" invisible="not authorizer_portal_user_id">
                        <strong>Portal Access Granted:</strong> This contact has portal access. 
                        <field name="authorizer_portal_user_id" readonly="1" nolabel="1" widget="many2one"/>
                    </div>
                </page>
            </xpath>
        </field>
    </record>

    <!-- Search view for authorizers -->
    <record id="view_partner_search_authorizer" model="ir.ui.view">
        <field name="name">res.partner.search.authorizer</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_res_partner_filter"/>
        <field name="arch" type="xml">
            <xpath expr="//filter[@name='type_company']" position="after">
                <separator/>
                <filter string="Authorizers" name="authorizers" domain="[('is_authorizer', '=', True)]"/>
                <filter string="Sales Reps (Portal)" name="sales_reps_portal" domain="[('is_sales_rep_portal', '=', True)]"/>
                <filter string="Technicians (Portal)" name="technicians_portal" domain="[('is_technician_portal', '=', True)]"/>
                <separator/>
                <filter string="Portal: No Access" name="portal_no_access" domain="[('is_authorizer', '=', True), ('portal_access_status', '=', 'no_access')]"/>
                <filter string="Portal: Invited" name="portal_invited" domain="[('is_authorizer', '=', True), ('portal_access_status', '=', 'invited')]"/>
                <filter string="Portal: Active" name="portal_active" domain="[('is_authorizer', '=', True), ('portal_access_status', '=', 'active')]"/>
            </xpath>
        </field>
    </record>

    <!-- Add portal status to contact list view -->
    <record id="view_partner_list_portal_status" model="ir.ui.view">
        <field name="name">res.partner.list.portal.status</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='email']" position="after">
                <field name="is_authorizer" string="Authorizer" optional="hide"/>
                <field name="portal_access_status" string="Portal Status" optional="hide"
                       decoration-danger="portal_access_status == 'no_access'"
                       decoration-warning="portal_access_status == 'invited'"
                       decoration-success="portal_access_status == 'active'"/>
            </xpath>
        </field>
    </record>

</odoo>
