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

    <!-- Call Dashboard Form View -->
    <record id="view_rc_call_dashboard_form" model="ir.ui.view">
        <field name="name">rc.call.dashboard.form</field>
        <field name="model">rc.call.dashboard</field>
        <field name="arch" type="xml">
            <form string="Call Dashboard" create="0" delete="0" edit="0">
                <sheet>
                    <field name="name" invisible="1"/>
                    <div class="mb-4">
                        <span class="text-muted">RingCentral call activity overview and analytics</span>
                    </div>

                    <!-- KPI Stat Cards -->
                    <div class="d-flex flex-nowrap gap-3 mb-4">

                        <div class="flex-fill">
                            <button name="action_open_all" type="object"
                                    class="btn btn-primary w-100 p-0 border-0 rounded-3">
                                <div class="text-center py-3 px-2">
                                    <div class="fw-bold" style="font-size: 2rem;">
                                        <field name="total_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Total Calls</div>
                                </div>
                            </button>
                        </div>

                        <div class="flex-fill">
                            <button name="action_open_inbound" type="object"
                                    class="btn btn-success w-100 p-0 border-0 rounded-3">
                                <div class="text-center py-3 px-2">
                                    <div class="fw-bold" style="font-size: 2rem;">
                                        <field name="inbound_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Inbound</div>
                                </div>
                            </button>
                        </div>

                        <div class="flex-fill">
                            <button name="action_open_outbound" type="object"
                                    class="btn btn-info w-100 p-0 border-0 rounded-3">
                                <div class="text-center py-3 px-2">
                                    <div class="fw-bold" style="font-size: 2rem;">
                                        <field name="outbound_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Outbound</div>
                                </div>
                            </button>
                        </div>

                        <div class="flex-fill">
                            <button name="action_open_missed" type="object"
                                    class="btn btn-danger w-100 p-0 border-0 rounded-3"
                                    invisible="missed_count == 0">
                                <div class="text-center py-3 px-2">
                                    <div class="fw-bold" style="font-size: 2rem;">
                                        <field name="missed_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Missed</div>
                                </div>
                            </button>
                        </div>

                    </div>

                    <!-- Secondary stats -->
                    <div class="d-flex flex-nowrap gap-3 mb-4">
                        <div class="flex-fill border rounded-3 p-3 text-center">
                            <div class="text-muted" style="font-size: 0.85rem;">Success Rate</div>
                            <div class="fw-bold" style="font-size: 1.5rem;">
                                <field name="success_rate"/>%
                            </div>
                        </div>
                        <div class="flex-fill border rounded-3 p-3 text-center">
                            <div class="text-muted" style="font-size: 0.85rem;">Avg Duration</div>
                            <div class="fw-bold" style="font-size: 1.5rem;">
                                <field name="avg_duration"/> min
                            </div>
                        </div>
                        <div class="flex-fill border rounded-3 p-3 text-center">
                            <div class="text-muted" style="font-size: 0.85rem;">Today</div>
                            <div class="fw-bold" style="font-size: 1.5rem;">
                                <field name="today_count"/>
                            </div>
                        </div>
                        <div class="flex-fill border rounded-3 p-3 text-center">
                            <div class="text-muted" style="font-size: 0.85rem;">This Week</div>
                            <div class="fw-bold" style="font-size: 1.5rem;">
                                <field name="week_count"/>
                            </div>
                        </div>
                        <div class="flex-fill border rounded-3 p-3 text-center">
                            <div class="text-muted" style="font-size: 0.85rem;">This Month</div>
                            <div class="fw-bold" style="font-size: 1.5rem;">
                                <field name="month_count"/>
                            </div>
                        </div>

                        <div class="flex-fill">
                            <button name="action_open_unread_voicemails" type="object"
                                    class="btn btn-warning w-100 p-0 border-0 rounded-3">
                                <div class="text-center py-3 px-2">
                                    <div class="fw-bold" style="font-size: 1.5rem;">
                                        <field name="unread_voicemail_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Voicemails</div>
                                    <div class="text-muted" style="font-size: 0.75rem;">
                                        <field name="voicemail_count"/> total
                                    </div>
                                </div>
                            </button>
                        </div>
                    </div>

                    <!-- Quick Actions -->
                    <div class="d-flex gap-2 mb-4">
                        <button name="action_quick_call" type="object"
                                class="btn btn-primary" icon="fa-phone">
                            Call
                        </button>
                        <button name="action_quick_sms" type="object"
                                class="btn btn-success" icon="fa-comment">
                            Message
                        </button>
                        <button name="action_quick_fax" type="object"
                                class="btn btn-info" icon="fa-fax">
                            Fax
                        </button>
                        <div class="ms-auto d-flex gap-2">
                            <button name="action_open_all" type="object"
                                    class="btn btn-secondary" icon="fa-list">
                                View All Calls
                            </button>
                            <button name="action_open_graph" type="object"
                                    class="btn btn-secondary" icon="fa-bar-chart">
                                Analytics
                            </button>
                        </div>
                    </div>

                    <!-- Recent Call History -->
                    <separator string="Recent Calls"/>
                    <field name="recent_call_ids" nolabel="1" readonly="1">
                        <list decoration-danger="status in ('missed', 'no_answer')"
                              decoration-success="status == 'answered'">
                            <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')"/>
                            <field name="has_recording" string="Rec" widget="boolean"/>
                        </list>
                    </field>

                </sheet>
            </form>
        </field>
    </record>

    <!-- Voicemail Dashboard Form View -->
    <record id="view_rc_voicemail_dashboard_form" model="ir.ui.view">
        <field name="name">rc.call.dashboard.voicemail.form</field>
        <field name="model">rc.call.dashboard</field>
        <field name="arch" type="xml">
            <form string="Voicemails" create="0" delete="0" edit="0">
                <sheet>
                    <field name="name" invisible="1"/>

                    <!-- Quick Action Buttons -->
                    <div class="d-flex gap-2 mb-4">
                        <button name="action_quick_call" type="object"
                                class="btn btn-primary" icon="fa-phone">
                            Call
                        </button>
                        <button name="action_quick_sms" type="object"
                                class="btn btn-success" icon="fa-comment">
                            Message
                        </button>
                        <button name="action_quick_fax" type="object"
                                class="btn btn-info" icon="fa-fax">
                            Fax
                        </button>
                        <div class="ms-auto d-flex gap-2">
                            <button name="action_open_voicemails" type="object"
                                    class="btn btn-secondary" icon="fa-list">
                                All Voicemails
                            </button>
                        </div>
                    </div>

                    <!-- KPI Row -->
                    <div class="d-flex flex-nowrap gap-3 mb-4">
                        <div class="flex-fill border rounded-3 p-3 text-center">
                            <div class="text-muted" style="font-size: 0.85rem;">Total</div>
                            <div class="fw-bold" style="font-size: 1.5rem;">
                                <field name="voicemail_count"/>
                            </div>
                        </div>
                        <div class="flex-fill border rounded-3 p-3 text-center"
                             style="border-color: #dc3545 !important;">
                            <div class="text-muted" style="font-size: 0.85rem;">Unread</div>
                            <div class="fw-bold text-danger" style="font-size: 1.5rem;">
                                <field name="unread_voicemail_count"/>
                            </div>
                        </div>
                    </div>

                    <!-- Recent Voicemails (Past Week) -->
                    <separator string="Last 7 Days"/>
                    <field name="recent_voicemail_ids" nolabel="1" readonly="1">
                        <list decoration-danger="read_status == 'Unread'"
                              decoration-muted="read_status == 'Read'"
                              open_form_view="True">
                            <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'"/>
                        </list>
                    </field>

                    <!-- Older Voicemails -->
                    <separator string="Older Voicemails"/>
                    <field name="older_voicemail_ids" nolabel="1" readonly="1">
                        <list decoration-danger="read_status == 'Unread'"
                              decoration-muted="read_status == 'Read'"
                              open_form_view="True">
                            <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'"/>
                        </list>
                    </field>

                </sheet>
            </form>
        </field>
    </record>

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

    <!-- Dashboard Menu -->
    <menuitem id="menu_rc_dashboard"
              name="Dashboard"
              parent="menu_rc_root"
              action="action_rc_call_dashboard"
              sequence="1"/>

</odoo>
