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

    <!-- Voicemail Form View -->
    <record id="view_rc_voicemail_form" model="ir.ui.view">
        <field name="name">rc.voicemail.form</field>
        <field name="model">rc.voicemail</field>
        <field name="arch" type="xml">
            <form string="Voicemail">
                <header>
                    <button name="action_make_call" type="object"
                            string="Call" icon="fa-phone"
                            class="btn-primary"/>
                    <button name="action_send_sms" type="object"
                            string="Message" icon="fa-comment"
                            class="btn-success"/>
                    <button name="action_send_fax" type="object"
                            string="Fax" icon="fa-fax"
                            class="btn-secondary"
                            invisible="not partner_id"/>
                    <button name="action_transcribe" type="object"
                            string="Transcribe" icon="fa-file-text"
                            class="btn-secondary"
                            invisible="not audio_attachment_id or transcription_text"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_contact" type="object"
                                class="oe_stat_button" icon="fa-user"
                                invisible="not partner_id">
                            <div class="o_stat_info">
                                <span class="o_stat_text">
                                    <field name="partner_id" readonly="1"
                                           nolabel="1" class="o_text_overflow"/>
                                </span>
                            </div>
                        </button>
                        <button name="action_view_sale_orders" type="object"
                                class="oe_stat_button" icon="fa-shopping-cart"
                                invisible="not partner_id">
                            <field name="sale_order_count" widget="statinfo"
                                   string="Sales Orders"/>
                        </button>
                        <button name="action_view_invoices" type="object"
                                class="oe_stat_button" icon="fa-file-text-o"
                                invisible="not partner_id">
                            <field name="invoice_count" widget="statinfo"
                                   string="Invoices"/>
                        </button>
                    </div>

                    <field name="audio_attachment_id" invisible="1"/>
                    <div class="oe_title">
                        <h1><field name="name" readonly="1"/></h1>
                    </div>

                    <div class="mb-3">
                        <field name="direction" widget="badge"
                               decoration-success="direction == 'inbound'"
                               decoration-info="direction == 'outbound'"
                               readonly="1"/>
                        <field name="read_status" widget="badge"
                               decoration-danger="read_status == 'Unread'"
                               decoration-success="read_status == 'Read'"
                               readonly="1" class="ms-2"/>
                    </div>

                    <group>
                        <group string="Voicemail Details">
                            <field name="caller_number" widget="phone"/>
                            <field name="caller_name"/>
                            <field name="received_date"/>
                            <field name="duration_display" string="Duration"/>
                            <field name="duration" invisible="1"/>
                        </group>
                        <group string="Linking">
                            <field name="partner_id"/>
                            <field name="transcription_status"/>
                        </group>
                    </group>

                    <group string="Transcription"
                           invisible="not transcription_text">
                        <group>
                            <field name="transcription_language"
                                   widget="badge" decoration-info="1"/>
                        </group>
                        <field name="transcription_text" nolabel="1"
                               readonly="1"/>
                    </group>

                    <group>
                        <field name="rc_message_id" readonly="1"
                               string="RingCentral Message ID"/>
                    </group>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- Voicemail List View -->
    <record id="view_rc_voicemail_list" model="ir.ui.view">
        <field name="name">rc.voicemail.list</field>
        <field name="model">rc.voicemail</field>
        <field name="arch" type="xml">
            <list string="Voicemails"
                  decoration-danger="read_status == 'Unread'"
                  decoration-muted="read_status == 'Read'">
                <field name="name"/>
                <field name="direction" widget="badge"
                       decoration-success="direction == 'inbound'"
                       decoration-info="direction == 'outbound'"/>
                <field name="received_date"/>
                <field name="caller_number"/>
                <field name="caller_name" optional="show"/>
                <field name="partner_id"/>
                <field name="duration_display" string="Duration"/>
                <field name="read_status" widget="badge"
                       decoration-danger="read_status == 'Unread'"
                       decoration-success="read_status == 'Read'"/>
                <field name="has_transcription" string="Transcript"
                       widget="boolean"/>
            </list>
        </field>
    </record>

    <!-- Voicemail Search View -->
    <record id="view_rc_voicemail_search" model="ir.ui.view">
        <field name="name">rc.voicemail.search</field>
        <field name="model">rc.voicemail</field>
        <field name="arch" type="xml">
            <search string="Search Voicemails">
                <field name="name"/>
                <field name="partner_id"/>
                <field name="caller_number"/>
                <field name="caller_name"/>
                <separator/>
                <filter string="Unread" name="filter_unread"
                        domain="[('read_status', '=', 'Unread')]"/>
                <filter string="Read" name="filter_read"
                        domain="[('read_status', '=', 'Read')]"/>
                <separator/>
                <filter string="Inbound" name="filter_inbound"
                        domain="[('direction', '=', 'inbound')]"/>
                <filter string="Outbound" name="filter_outbound"
                        domain="[('direction', '=', 'outbound')]"/>
                <separator/>
                <filter string="Today" name="filter_today"
                        domain="[('received_date', '>=', (context_today()).strftime('%%Y-%%m-%%d'))]"/>
                <filter string="This Week" name="filter_week"
                        domain="[('received_date', '>=', (context_today() - relativedelta(weeks=1)).strftime('%%Y-%%m-%%d'))]"/>
                <filter string="This Month" name="filter_month"
                        domain="[('received_date', '>=', (context_today() - relativedelta(months=1)).strftime('%%Y-%%m-%%d'))]"/>
                <separator/>
                <filter string="Contact" name="group_partner"
                        context="{'group_by': 'partner_id'}"/>
                <filter string="Date" name="group_date"
                        context="{'group_by': 'received_date:month'}"/>
                <filter string="Read Status" name="group_read"
                        context="{'group_by': 'read_status'}"/>
            </search>
        </field>
    </record>

    <!-- Actions -->
    <record id="action_rc_voicemail" model="ir.actions.act_window">
        <field name="name">Voicemails</field>
        <field name="res_model">rc.voicemail</field>
        <field name="path">voicemails</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_rc_voicemail_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No voicemails yet
            </p>
            <p>
                Voicemails are automatically synced from RingCentral.
            </p>
        </field>
    </record>

    <!-- Voicemail Dashboard Action -->
    <record id="action_rc_voicemail_dashboard" model="ir.actions.act_window">
        <field name="name">Voicemail Dashboard</field>
        <field name="res_model">rc.call.dashboard</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="view_rc_voicemail_dashboard_form"/>
        <field name="target">current</field>
    </record>

    <!-- Voicemail top-level menu -->
    <menuitem id="menu_rc_voicemail_root"
              name="Voicemail"
              parent="menu_rc_root"
              sequence="30"/>

    <menuitem id="menu_rc_voicemail_dashboard"
              name="Dashboard"
              parent="menu_rc_voicemail_root"
              action="action_rc_voicemail_dashboard"
              sequence="10"/>

    <menuitem id="menu_rc_voicemails"
              name="Voicemails"
              parent="menu_rc_voicemail_root"
              action="action_rc_voicemail"
              sequence="20"/>

    <!-- Keep voicemails accessible under Phone too -->
    <menuitem id="menu_rc_voicemails_phone"
              name="Voicemails"
              parent="menu_rc_phone"
              action="action_rc_voicemail"
              sequence="15"/>

</odoo>
