<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Affiliate tab on the standard partner form (minimal — config fields only).
         Shown when the partner is an affiliate so admins can set commission %
         and withdraw limit from the contact form. -->
    <record id="view_partner_form_inherit_affiliate" model="ir.ui.view">
        <field name="name">res.partner.form.affiliate</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="arch" type="xml">
            <xpath expr="//notebook" position="inside">
                <page name="kj_affiliate_referral" string="Referral"
                      invisible="not affiliate_referrer_config_id">
                    <group>
                        <field name="affiliate_referrer_config_id" readonly="1"/>
                    </group>
                </page>
            </xpath>
        </field>
    </record>

    <!-- Dedicated list view for affiliate partners -->
    <record id="view_affiliate_partner_list" model="ir.ui.view">
        <field name="name">res.partner.affiliate.list</field>
        <field name="model">res.partner</field>
        <field name="arch" type="xml">
            <list>
                <field name="name"/>
                <field name="email"/>
                <field name="website" optional="show"/>
                <field name="affiliate_commission_pct" string="Commission %"/>
                <field name="affiliate_withdraw_limit" string="Min. Withdrawal"/>
                <field name="affiliate_current_amount" string="Available"/>
                <field name="affiliate_withdraw_amount" string="Pending Payouts"/>
                <field name="affiliate_paid_amount" string="Total Paid"/>
                <field name="affiliate_payable" string="Payable?" optional="show"/>
                <field name="affiliate_currency_id" column_invisible="True"/>
            </list>
        </field>
    </record>

    <!-- Affiliate partner form — mirrors V9 affiliate_partners form with
         Credentials (config), Transactions tab (balances + embedded rows). -->
    <record id="view_affiliate_partner_form" model="ir.ui.view">
        <field name="name">res.partner.affiliate.form</field>
        <field name="model">res.partner</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <div class="oe_button_box" name="button_box"/>
                    <field name="image_1920" widget="image" class="oe_avatar"/>
                    <div class="oe_title">
                        <h1>
                            <field name="name" placeholder="Affiliate Name"/>
                        </h1>
                    </div>
                    <group>
                        <group>
                            <field name="email"/>
                            <field name="phone"/>
                            <field name="website" widget="url"/>
                        </group>
                        <group>
                            <field name="affiliate_commission_pct" string="Commission %"/>
                            <field name="affiliate_withdraw_limit" string="Min. Withdrawal"/>
                            <field name="affiliate_currency_id" invisible="1"/>
                        </group>
                    </group>

                    <notebook>
                        <!-- Transactions tab: mirrors V9 Transactions tab on affiliate form -->
                        <page name="transactions" string="Transactions">
                            <!-- Balance summary — mirrors V9 "Available commissions /
                                 Withdrawal request / Total paid" header -->
                            <group>
                                <group string="Available">
                                    <field name="affiliate_current_amount"
                                           nolabel="1"
                                           widget="monetary"
                                           options="{'currency_field': 'affiliate_currency_id'}"/>
                                </group>
                                <group string="Pending Withdrawal">
                                    <field name="affiliate_withdraw_amount"
                                           nolabel="1"
                                           widget="monetary"
                                           options="{'currency_field': 'affiliate_currency_id'}"/>
                                </group>
                                <group string="Total Paid">
                                    <field name="affiliate_paid_amount"
                                           nolabel="1"
                                           widget="monetary"
                                           options="{'currency_field': 'affiliate_currency_id'}"/>
                                </group>
                            </group>
                            <!-- Transaction rows — commissions + payouts inline -->
                            <field name="commission_ids" nolabel="1">
                                <list create="1" delete="1"
                                      decoration-success="state == 'completed'"
                                      decoration-muted="state == 'canceled'"
                                      decoration-info="state == 'pending'">
                                    <field name="create_date" string="Opened"/>
                                    <field name="write_date" string="Last Action" optional="show"/>
                                    <field name="transaction_type" string="Type"/>
                                    <field name="commission_amount" string="Amount" sum="Total"/>
                                    <field name="widget_config_id" string="Widget" optional="show"/>
                                    <field name="service_type" string="Service Type" optional="show"/>
                                    <field name="job_id" string="Job" optional="show"/>
                                    <field name="referrer_source" string="Source" optional="show"/>
                                    <field name="state" widget="badge"
                                           decoration-success="state == 'completed'"
                                           decoration-info="state == 'pending'"
                                           decoration-muted="state == 'canceled'"
                                           string="Status"/>
                                    <field name="currency_id" column_invisible="True"/>
                                </list>
                            </field>
                        </page>

                        <!-- Widget configs tab -->
                        <page name="widget_configs" string="Widget Configurations">
                            <field name="affiliate_widget_config_ids" nolabel="1">
                                <list create="0">
                                    <field name="widget_key"/>
                                    <field name="widget_mode"/>
                                    <field name="affiliate_website"/>
                                    <field name="is_active"/>
                                    <field name="total_impressions"/>
                                    <field name="total_clicks"/>
                                </list>
                            </field>
                        </page>

                        <!-- Config tab -->
                        <page name="affiliate_config" string="Configuration">
                            <group>
                                <group string="Commission">
                                    <field name="affiliate_commission_pct"/>
                                    <field name="affiliate_withdraw_limit"/>
                                    <field name="affiliate_payable" readonly="1"/>
                                </group>
                                <group string="Attribution">
                                    <field name="affiliate_referrer_config_id"
                                           invisible="not affiliate_referrer_config_id"
                                           readonly="1"/>
                                </group>
                            </group>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- Action: Affiliate Partners list (filtered to role='affiliate') -->
    <record id="action_affiliate_partners" model="ir.actions.act_window">
        <field name="name">Affiliate Partners</field>
        <field name="res_model">res.partner</field>
        <field name="view_mode">list,form</field>
        <field name="domain">[('role_user_type', '=', 'affiliate')]</field>
        <field name="context">{'default_role_user_type': 'affiliate'}</field>
        <!-- (5,0,0) clears existing view links before re-adding so the
             record is idempotent on repeated -u runs. -->
        <field name="view_ids" eval="[
            (5, 0, 0),
            (0, 0, {'view_mode': 'list', 'view_id': ref('view_affiliate_partner_list')}),
            (0, 0, {'view_mode': 'form', 'view_id': ref('view_affiliate_partner_form')}),
        ]"/>
    </record>
</odoo>
