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

    <!-- Fax Form View -->
    <record id="view_fusion_fax_form" model="ir.ui.view">
        <field name="name">fusion.fax.form</field>
        <field name="model">fusion.fax</field>
        <field name="arch" type="xml">
            <form string="Fax">
                <header>
                    <!-- Outbound buttons only -->
                    <button name="action_send" string="Send Fax"
                            type="object" class="btn-primary"
                            icon="fa-fax"
                            invisible="state != 'draft' or direction == 'inbound'"/>
                    <button name="action_resend" string="Resend Fax"
                            type="object" class="btn-primary"
                            icon="fa-repeat"
                            invisible="state != 'sent' or direction == 'inbound'"
                            confirm="This will resend the fax with all attachments. Continue?"/>
                    <button name="action_retry" string="Retry"
                            type="object" class="btn-primary"
                            icon="fa-refresh"
                            invisible="state != 'failed' or direction == 'inbound'"/>
                    <button name="action_reset_to_draft" string="Reset to Draft"
                            type="object"
                            invisible="state != 'failed' or direction == 'inbound'"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,sending,sent,received"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_open_sale_order" type="object"
                                class="oe_stat_button" icon="fa-shopping-cart"
                                invisible="not sale_order_id">
                            <field name="sale_order_id" widget="statinfo" string="Sale Order"/>
                        </button>
                    </div>
                    <div class="oe_title">
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>

                    <!-- Direction badge -->
                    <div class="mb-3">
                        <field name="direction" widget="badge"
                               decoration-info="direction == 'outbound'"
                               decoration-success="direction == 'inbound'"
                               readonly="1"/>
                    </div>

                    <!-- Outbound fields -->
                    <group invisible="direction == 'inbound'">
                        <group>
                            <field name="partner_id"/>
                            <field name="fax_number"/>
                            <field name="sent_by_id"/>
                        </group>
                        <group>
                            <field name="sent_date"/>
                            <field name="page_count"/>
                            <field name="ringcentral_message_id"/>
                        </group>
                    </group>

                    <!-- Inbound fields -->
                    <group invisible="direction == 'outbound'">
                        <group>
                            <field name="sender_number"/>
                            <field name="partner_id" string="Matched Contact"/>
                            <field name="sale_order_id" string="Link to Sale Order"
                                   options="{'no_create': True}"/>
                        </group>
                        <group>
                            <field name="received_date"/>
                            <field name="page_count"/>
                            <field name="rc_read_status"/>
                            <field name="ringcentral_message_id"/>
                        </group>
                    </group>

                    <group invisible="direction == 'inbound'">
                        <field name="cover_page_text" placeholder="Optional cover page text..."/>
                    </group>

                    <!-- Ordered Documents with preview -->
                    <separator string="Documents"/>
                    <field name="document_ids" nolabel="1">
                        <list editable="bottom">
                            <field name="sequence" widget="handle"/>
                            <field name="attachment_id"/>
                            <field name="file_name" readonly="1"/>
                            <field name="mimetype" readonly="1"/>
                            <button name="action_preview" type="object"
                                    string="Preview" icon="fa-eye"
                                    class="btn-link"/>
                        </list>
                    </field>

                    <group string="Linked Records"
                           invisible="direction == 'inbound'">
                        <group>
                            <field name="sale_order_id"/>
                        </group>
                        <group>
                            <field name="account_move_id"/>
                        </group>
                    </group>
                    <group string="Error Details" invisible="state != 'failed'">
                        <field name="error_message" readonly="1" nolabel="1"/>
                    </group>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- Fax List View -->
    <record id="view_fusion_fax_list" model="ir.ui.view">
        <field name="name">fusion.fax.list</field>
        <field name="model">fusion.fax</field>
        <field name="arch" type="xml">
            <list string="Faxes" decoration-danger="state == 'failed'"
                  decoration-success="state in ('sent', 'received')"
                  decoration-info="state == 'sending'">
                <field name="name"/>
                <field name="direction" widget="badge"
                       decoration-info="direction == 'outbound'"
                       decoration-success="direction == 'inbound'"/>
                <field name="display_date" string="Date"/>
                <field name="partner_id"/>
                <field name="display_number" string="Fax Number"/>
                <field name="page_count"/>
                <field name="state" widget="badge"
                       decoration-success="state in ('sent', 'received')"
                       decoration-danger="state == 'failed'"
                       decoration-info="state == 'sending'"
                       decoration-warning="state == 'draft'"/>
                <field name="sent_by_id" optional="show"/>
                <field name="sale_order_id" optional="hide"/>
                <field name="account_move_id" optional="hide"/>
            </list>
        </field>
    </record>

    <!-- Fax Search View -->
    <record id="view_fusion_fax_search" model="ir.ui.view">
        <field name="name">fusion.fax.search</field>
        <field name="model">fusion.fax</field>
        <field name="arch" type="xml">
            <search string="Search Faxes">
                <field name="name"/>
                <field name="partner_id"/>
                <field name="fax_number"/>
                <field name="sender_number"/>
                <field name="sale_order_id"/>
                <field name="account_move_id"/>
                <separator/>
                <filter string="Outbound" name="filter_outbound"
                        domain="[('direction', '=', 'outbound')]"/>
                <filter string="Inbound" name="filter_inbound"
                        domain="[('direction', '=', 'inbound')]"/>
                <separator/>
                <filter string="Draft" name="filter_draft"
                        domain="[('state', '=', 'draft')]"/>
                <filter string="Sent" name="filter_sent"
                        domain="[('state', '=', 'sent')]"/>
                <filter string="Received" name="filter_received"
                        domain="[('state', '=', 'received')]"/>
                <filter string="Failed" name="filter_failed"
                        domain="[('state', '=', 'failed')]"/>
                <separator/>
                <filter string="My Faxes" name="filter_my_faxes"
                        domain="[('sent_by_id', '=', uid)]"/>
                <separator/>
                <filter string="Direction" name="group_direction"
                        context="{'group_by': 'direction'}"/>
                <filter string="Status" name="group_state"
                        context="{'group_by': 'state'}"/>
                <filter string="Recipient" name="group_partner"
                        context="{'group_by': 'partner_id'}"/>
                <filter string="Sent Date" name="group_date"
                        context="{'group_by': 'sent_date:month'}"/>
                <filter string="Sent By" name="group_user"
                        context="{'group_by': 'sent_by_id'}"/>
            </search>
        </field>
    </record>

    <!-- Menu and Actions -->
    <record id="action_fusion_fax" model="ir.actions.act_window">
        <field name="name">All Faxes</field>
        <field name="res_model">fusion.fax</field>
        <field name="path">faxes</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_fax_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No faxes yet
            </p>
            <p>
                Send faxes from Sale Orders or Invoices using the "Send Fax" button,
                or create one manually from here.
            </p>
        </field>
    </record>

    <!-- Received Faxes Action -->
    <record id="action_fusion_fax_received" model="ir.actions.act_window">
        <field name="name">Received Faxes</field>
        <field name="res_model">fusion.fax</field>
        <field name="view_mode">list,form</field>
        <field name="domain">[('direction', '=', 'inbound')]</field>
        <field name="search_view_id" ref="view_fusion_fax_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No received faxes yet
            </p>
            <p>
                Incoming faxes are automatically synced from RingCentral every 5 minutes.
            </p>
        </field>
    </record>

    <!-- Sent Faxes Action -->
    <record id="action_fusion_fax_sent" model="ir.actions.act_window">
        <field name="name">Sent Faxes</field>
        <field name="res_model">fusion.fax</field>
        <field name="view_mode">list,form</field>
        <field name="domain">[('direction', '=', 'outbound')]</field>
        <field name="search_view_id" ref="view_fusion_fax_search"/>
    </record>

    <!-- Top-level Faxes menu (fax users only) -->
    <menuitem id="menu_fusion_faxes_root"
              name="Faxes"
              web_icon="fusion_faxes,static/description/icon.png"
              groups="group_fax_user"
              sequence="45"/>

    <menuitem id="menu_fusion_fax_list"
              name="All Faxes"
              parent="menu_fusion_faxes_root"
              action="action_fusion_fax"
              sequence="10"/>

    <menuitem id="menu_fusion_fax_received"
              name="Received Faxes"
              parent="menu_fusion_faxes_root"
              action="action_fusion_fax_received"
              sequence="15"/>

    <menuitem id="menu_fusion_fax_sent"
              name="Sent Faxes"
              parent="menu_fusion_faxes_root"
              action="action_fusion_fax_sent"
              sequence="20"/>

</odoo>
