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

    <!-- ============================================================ -->
    <!--         FOLLOW-UP LEVEL: Configuration Views                  -->
    <!-- ============================================================ -->

    <!-- List View: Follow-up Levels -->
    <record id="fusion_followup_level_view_list" model="ir.ui.view">
        <field name="name">fusion.followup.level.list</field>
        <field name="model">fusion.followup.level</field>
        <field name="arch" type="xml">
            <list string="Follow-up Levels" editable="bottom">
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="delay"/>
                <field name="send_email"/>
                <field name="send_sms"/>
                <field name="send_letter"/>
                <field name="join_invoices"/>
                <field name="company_id" optional="hide" groups="base.group_multi_company"/>
            </list>
        </field>
    </record>

    <!-- Form View: Follow-up Level -->
    <record id="fusion_followup_level_view_form" model="ir.ui.view">
        <field name="name">fusion.followup.level.form</field>
        <field name="model">fusion.followup.level</field>
        <field name="arch" type="xml">
            <form string="Follow-up Level">
                <sheet>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" placeholder="e.g. First Reminder"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Timing">
                            <field name="sequence"/>
                            <field name="delay"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                            <field name="active" invisible="1"/>
                        </group>
                        <group string="Communication Channels">
                            <field name="send_email"/>
                            <field name="send_sms"/>
                            <field name="send_letter"/>
                        </group>
                    </group>
                    <group string="Templates">
                        <field name="email_template_id" invisible="not send_email"/>
                        <field name="sms_template_id" invisible="not send_sms"/>
                        <field name="join_invoices" invisible="not send_email"/>
                    </group>
                    <notebook>
                        <page string="Message Body" name="page_description">
                            <field name="description" placeholder="Write the follow-up message content here..."/>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Action: Follow-up Levels -->
    <record id="action_fusion_followup_level" model="ir.actions.act_window">
        <field name="name">Follow-up Levels</field>
        <field name="res_model">fusion.followup.level</field>
        <field name="view_mode">list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Define your payment follow-up levels
            </p>
            <p>
                Create escalating reminder stages with configurable delays
                and communication channels. The system will automatically
                move partners through these levels as invoices remain unpaid.
            </p>
        </field>
    </record>

    <!-- ============================================================ -->
    <!--         FOLLOW-UP LINE: Partner Tracking Views                -->
    <!-- ============================================================ -->

    <!-- List View: Follow-up Lines (Dashboard) -->
    <record id="fusion_followup_line_view_list" model="ir.ui.view">
        <field name="name">fusion.followup.line.list</field>
        <field name="model">fusion.followup.line</field>
        <field name="arch" type="xml">
            <list string="Follow-up Reports"
                  decoration-danger="followup_status == 'in_need'"
                  decoration-warning="followup_status == 'with_overdue'"
                  decoration-muted="followup_status == 'no_action_needed'">
                <field name="partner_id"/>
                <field name="followup_level_id"/>
                <field name="date"/>
                <field name="next_followup_date"/>
                <field name="overdue_amount" widget="monetary"/>
                <field name="overdue_count"/>
                <field name="followup_status" widget="badge"
                       decoration-danger="followup_status == 'in_need'"
                       decoration-warning="followup_status == 'with_overdue'"
                       decoration-success="followup_status == 'no_action_needed'"/>
                <field name="company_id" optional="hide" groups="base.group_multi_company"/>
                <field name="currency_id" column_invisible="True"/>
            </list>
        </field>
    </record>

    <!-- Search View: Follow-up Lines -->
    <record id="fusion_followup_line_view_search" model="ir.ui.view">
        <field name="name">fusion.followup.line.search</field>
        <field name="model">fusion.followup.line</field>
        <field name="arch" type="xml">
            <search string="Follow-up Search">
                <field name="partner_id"/>
                <field name="followup_level_id"/>
                <separator/>
                <filter name="filter_in_need" string="Action Needed"
                        domain="[('followup_status', '=', 'in_need')]"/>
                <filter name="filter_with_overdue" string="With Overdue"
                        domain="[('followup_status', '=', 'with_overdue')]"/>
                <filter name="filter_no_action" string="No Action Needed"
                        domain="[('followup_status', '=', 'no_action_needed')]"/>
                <separator/>
                <filter name="filter_overdue_today" string="Due Today"
                        domain="[('next_followup_date', '&lt;=', context_today().strftime('%Y-%m-%d'))]"/>
                <separator/>
                <group>
                    <filter name="group_by_status" string="Status"
                            context="{'group_by': 'followup_status'}"/>
                    <filter name="group_by_level" string="Follow-up Level"
                            context="{'group_by': 'followup_level_id'}"/>
                    <filter name="group_by_partner" string="Partner"
                            context="{'group_by': 'partner_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- Form View: Follow-up Line (Partner Follow-up Detail) -->
    <record id="fusion_followup_line_view_form" model="ir.ui.view">
        <field name="name">fusion.followup.line.form</field>
        <field name="model">fusion.followup.line</field>
        <field name="arch" type="xml">
            <form string="Payment Follow-up">
                <header>
                    <button name="execute_followup" type="object"
                            string="Send Follow-up"
                            class="btn-primary"
                            invisible="followup_status == 'no_action_needed'"
                            confirm="This will send the follow-up notification to the partner. Continue?"/>
                    <button name="compute_followup_status" type="object"
                            string="Refresh Status"
                            class="btn-secondary"/>
                    <button name="%(fusion_accounting.action_fusion_followup_send_wizard)d"
                            type="action"
                            string="Preview &amp; Send"
                            class="btn-secondary"
                            invisible="followup_status == 'no_action_needed'"/>
                    <field name="followup_status" widget="statusbar"
                           statusbar_visible="no_action_needed,with_overdue,in_need"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="partner_id" readonly="1" options="{'no_open': True}"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Follow-up Details">
                            <field name="followup_level_id"/>
                            <field name="date"/>
                            <field name="next_followup_date"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                        <group string="Outstanding Amounts">
                            <field name="overdue_amount" widget="monetary"/>
                            <field name="overdue_count"/>
                            <field name="currency_id" invisible="1"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Action: Follow-up Reports (Dashboard) -->
    <record id="action_fusion_followup_line" model="ir.actions.act_window">
        <field name="name">Follow-up Reports</field>
        <field name="res_model">fusion.followup.line</field>
        <field name="view_mode">list,form</field>
        <field name="context">{'search_default_filter_in_need': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No partners require payment follow-up
            </p>
            <p>
                Partners with overdue invoices will appear here automatically.
                Use the scheduled action or click "Refresh Status" to update.
            </p>
        </field>
    </record>

    <!-- ============================================================ -->
    <!--         PARTNER FORM: Follow-up Button                        -->
    <!-- ============================================================ -->

    <record id="fusion_partner_view_form_followup" model="ir.ui.view">
        <field name="name">res.partner.form.followup</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="priority">99</field>
        <field name="arch" type="xml">
            <xpath expr="//div[@name='button_box']" position="inside">
                <button name="action_open_followup"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-envelope"
                        groups="account.group_account_invoice">
                    <div class="o_field_widget o_stat_info">
                        <span class="o_stat_value">
                            <field name="fusion_overdue_amount" widget="monetary" options="{'currency_field': 'currency_id'}"/>
                        </span>
                        <span class="o_stat_text">Overdue</span>
                    </div>
                </button>
            </xpath>
        </field>
    </record>

    <!-- ============================================================ -->
    <!--         MENU ITEMS                                            -->
    <!-- ============================================================ -->

    <!-- Sub-menu under Accounting > Customers -->
    <menuitem id="menu_fusion_followup_root"
              name="Payment Follow-up"
              parent="account.menu_finance_receivables"
              sequence="30"/>

    <menuitem id="menu_fusion_followup_reports"
              name="Follow-up Reports"
              parent="menu_fusion_followup_root"
              action="action_fusion_followup_line"
              sequence="10"/>

    <!-- Configuration menu -->
    <menuitem id="menu_fusion_followup_config"
              name="Follow-up Levels"
              parent="account.menu_finance_configuration"
              action="action_fusion_followup_level"
              sequence="45"
              groups="account.group_account_manager"/>

</odoo>
