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

    <!-- Call History Form View -->
    <record id="view_rc_call_history_form" model="ir.ui.view">
        <field name="name">rc.call.history.form</field>
        <field name="model">rc.call.history</field>
        <field name="arch" type="xml">
            <form string="Call Record">
                <header>
                    <button name="action_make_call" type="object"
                            string="Call" icon="fa-phone"
                            class="btn-primary"/>
                    <button name="action_send_sms" type="object"
                            string="Text" icon="fa-comment"
                            class="btn-secondary"/>
                    <button name="action_send_fax" type="object"
                            string="Fax" icon="fa-fax"
                            class="btn-secondary"
                            invisible="not partner_id"/>
                </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"/></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>

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

                    <div class="mb-3">
                        <field name="direction" widget="badge"
                               decoration-info="direction == 'outbound'"
                               decoration-success="direction == 'inbound'"
                               readonly="1"/>
                        <field name="status" widget="badge"
                               decoration-success="status == 'answered'"
                               decoration-danger="status in ('missed', 'no_answer')"
                               decoration-warning="status == 'busy'"
                               readonly="1" class="ms-2"/>
                    </div>

                    <group>
                        <group string="Call Details">
                            <field name="from_number" widget="phone"/>
                            <field name="to_number" widget="phone"/>
                            <field name="start_time"/>
                            <field name="duration_display" string="Duration"/>
                            <field name="duration" invisible="1"/>
                        </group>
                        <group string="Linking">
                            <field name="partner_id"/>
                            <field name="user_id"/>
                            <field name="sale_order_id" options="{'no_create': True}"/>
                        </group>
                    </group>

                    <!-- Recording section -->
                    <group string="Recording" invisible="not has_recording">
                        <div>
                            <button name="action_play_recording" type="object"
                                    string="Play Recording" icon="fa-play-circle"
                                    class="btn-primary"/>
                        </div>
                        <field name="has_recording" invisible="1"/>
                        <field name="recording_content_uri" invisible="1"/>
                    </group>

                    <!-- Transcript section -->
                    <group string="Transcript" invisible="not has_transcript">
                        <field name="transcript_text" nolabel="1" readonly="1"/>
                        <field name="has_transcript" invisible="1"/>
                    </group>

                    <!-- Notes -->
                    <group string="Notes">
                        <field name="notes" nolabel="1" placeholder="Add notes about this call..."/>
                    </group>

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

    <!-- Call History List View -->
    <record id="view_rc_call_history_list" model="ir.ui.view">
        <field name="name">rc.call.history.list</field>
        <field name="model">rc.call.history</field>
        <field name="arch" type="xml">
            <list string="Call History"
                  decoration-danger="status in ('missed', 'no_answer')"
                  decoration-success="status == 'answered'"
                  decoration-warning="status == 'busy'">
                <field name="name"/>
                <field name="direction" widget="badge"
                       decoration-info="direction == 'outbound'"
                       decoration-success="direction == 'inbound'"/>
                <field name="start_time"/>
                <field name="from_number"/>
                <field name="to_number"/>
                <field name="partner_id"/>
                <field name="duration_display" string="Duration"/>
                <field name="status" widget="badge"
                       decoration-success="status == 'answered'"
                       decoration-danger="status in ('missed', 'no_answer')"
                       decoration-warning="status == 'busy'"/>
                <field name="has_recording" string="Rec" widget="boolean"/>
                <field name="has_transcript" string="Trans" widget="boolean" optional="show"/>
                <field name="user_id" optional="hide"/>
            </list>
        </field>
    </record>

    <!-- Call History Search View -->
    <record id="view_rc_call_history_search" model="ir.ui.view">
        <field name="name">rc.call.history.search</field>
        <field name="model">rc.call.history</field>
        <field name="arch" type="xml">
            <search string="Search Calls">
                <field name="name"/>
                <field name="partner_id"/>
                <field name="from_number"/>
                <field name="to_number"/>
                <separator/>
                <filter string="Inbound" name="filter_inbound"
                        domain="[('direction', '=', 'inbound')]"/>
                <filter string="Outbound" name="filter_outbound"
                        domain="[('direction', '=', 'outbound')]"/>
                <separator/>
                <filter string="Answered" name="filter_answered"
                        domain="[('status', '=', 'answered')]"/>
                <filter string="Missed" name="filter_missed"
                        domain="[('status', 'in', ('missed', 'no_answer'))]"/>
                <filter string="Has Recording" name="filter_recording"
                        domain="[('has_recording', '=', True)]"/>
                <filter string="Has Transcript" name="filter_transcript"
                        domain="[('has_transcript', '=', True)]"/>
                <separator/>
                <filter string="Today" name="filter_today"
                        domain="[('start_time', '>=', (context_today()).strftime('%%Y-%%m-%%d'))]"/>
                <filter string="This Week" name="filter_week"
                        domain="[('start_time', '>=', (context_today() - relativedelta(weeks=1)).strftime('%%Y-%%m-%%d'))]"/>
                <filter string="This Month" name="filter_month"
                        domain="[('start_time', '>=', (context_today() - relativedelta(months=1)).strftime('%%Y-%%m-%%d'))]"/>
                <separator/>
                <filter string="Direction" name="group_direction"
                        context="{'group_by': 'direction'}"/>
                <filter string="Status" name="group_status"
                        context="{'group_by': 'status'}"/>
                <filter string="Contact" name="group_partner"
                        context="{'group_by': 'partner_id'}"/>
                <filter string="Date" name="group_date"
                        context="{'group_by': 'start_time:month'}"/>
            </search>
        </field>
    </record>

    <!-- Graph View -->
    <record id="view_rc_call_history_graph" model="ir.ui.view">
        <field name="name">rc.call.history.graph</field>
        <field name="model">rc.call.history</field>
        <field name="arch" type="xml">
            <graph string="Call Analytics" type="bar">
                <field name="start_time" interval="month"/>
                <field name="direction"/>
                <field name="duration" type="measure"/>
            </graph>
        </field>
    </record>

    <!-- Pivot View -->
    <record id="view_rc_call_history_pivot" model="ir.ui.view">
        <field name="name">rc.call.history.pivot</field>
        <field name="model">rc.call.history</field>
        <field name="arch" type="xml">
            <pivot string="Call Analytics">
                <field name="partner_id" type="row"/>
                <field name="direction" type="col"/>
                <field name="duration" type="measure"/>
            </pivot>
        </field>
    </record>

    <!-- Actions -->
    <record id="action_rc_call_history" model="ir.actions.act_window">
        <field name="name">Call History</field>
        <field name="res_model">rc.call.history</field>
        <field name="path">calls</field>
        <field name="view_mode">list,form,graph,pivot</field>
        <field name="search_view_id" ref="view_rc_call_history_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No call history yet
            </p>
            <p>
                Call history is automatically synced from RingCentral every 15 minutes.
                Connect your RingCentral account in Configuration to start.
            </p>
        </field>
    </record>

    <record id="action_rc_call_inbound" model="ir.actions.act_window">
        <field name="name">Inbound Calls</field>
        <field name="res_model">rc.call.history</field>
        <field name="view_mode">list,form,graph</field>
        <field name="domain">[('direction', '=', 'inbound')]</field>
        <field name="search_view_id" ref="view_rc_call_history_search"/>
    </record>

    <record id="action_rc_call_outbound" model="ir.actions.act_window">
        <field name="name">Outbound Calls</field>
        <field name="res_model">rc.call.history</field>
        <field name="view_mode">list,form,graph</field>
        <field name="domain">[('direction', '=', 'outbound')]</field>
        <field name="search_view_id" ref="view_rc_call_history_search"/>
    </record>

    <!-- Phone menu items (under Fusion RingCentral root) -->
    <menuitem id="menu_rc_phone"
              name="Phone"
              parent="menu_rc_root"
              sequence="20"/>

    <menuitem id="menu_rc_call_history"
              name="Call History"
              parent="menu_rc_phone"
              action="action_rc_call_history"
              sequence="10"/>

    <menuitem id="menu_rc_call_inbound"
              name="Inbound Calls"
              parent="menu_rc_phone"
              action="action_rc_call_inbound"
              sequence="20"/>

    <menuitem id="menu_rc_call_outbound"
              name="Outbound Calls"
              parent="menu_rc_phone"
              action="action_rc_call_outbound"
              sequence="30"/>

</odoo>
