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

    <!-- Dashboard Form View -->
    <record id="view_fusion_fax_dashboard_form" model="ir.ui.view">
        <field name="name">fusion.fax.dashboard.form</field>
        <field name="model">fusion.fax.dashboard</field>
        <field name="arch" type="xml">
            <form string="Fax Dashboard" create="0" delete="0" edit="0">
                <sheet>
                    <field name="name" invisible="1"/>
                    <!-- Title -->
                    <div class="mb-4">
                        <span class="text-muted">Overview of fax activity and quick actions</span>
                    </div>

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

                        <!-- Total Faxes -->
                        <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 Faxes</div>
                                </div>
                            </button>
                        </div>

                        <!-- Sent -->
                        <div class="flex-fill">
                            <button name="action_open_sent" 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="sent_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Sent</div>
                                </div>
                            </button>
                        </div>

                        <!-- Received -->
                        <div class="flex-fill">
                            <button name="action_open_received" 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="received_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Received</div>
                                </div>
                            </button>
                        </div>

                        <!-- Failed -->
                        <div class="flex-fill" invisible="failed_count == 0">
                            <button name="action_open_failed" type="object"
                                    class="btn btn-danger 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="failed_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Failed</div>
                                </div>
                            </button>
                        </div>

                        <!-- Draft -->
                        <div class="flex-fill" invisible="draft_count == 0">
                            <button name="action_open_draft" 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: 2rem;">
                                        <field name="draft_count"/>
                                    </div>
                                    <div style="font-size: 0.85rem;">Draft</div>
                                </div>
                            </button>
                        </div>

                    </div>

                    <!-- Quick Action Buttons -->
                    <div class="d-flex gap-2 mb-4">
                        <button name="action_send_fax" type="object"
                                class="btn btn-primary"
                                icon="fa-fax">
                            Send New Fax
                        </button>
                        <button name="action_open_all" type="object"
                                class="btn btn-secondary"
                                icon="fa-list">
                            View All Faxes
                        </button>
                        <button name="action_open_received" type="object"
                                class="btn btn-secondary"
                                icon="fa-download">
                            View Received
                        </button>
                        <button name="action_open_failed" type="object"
                                class="btn btn-secondary"
                                icon="fa-exclamation-triangle"
                                invisible="failed_count == 0">
                            View Failed
                        </button>
                    </div>

                    <!-- Recent Fax History -->
                    <separator string="Recent Fax History"/>
                    <field name="recent_fax_ids" nolabel="1" readonly="1">
                        <list 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"/>
                        </list>
                    </field>

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

    <!-- Dashboard Action -->
    <record id="action_fusion_fax_dashboard" model="ir.actions.act_window">
        <field name="name">Dashboard</field>
        <field name="res_model">fusion.fax.dashboard</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="view_fusion_fax_dashboard_form"/>
        <field name="target">current</field>
    </record>

    <!-- Dashboard Menu (first item under Faxes root) -->
    <menuitem id="menu_fusion_fax_dashboard"
              name="Dashboard"
              parent="menu_fusion_faxes_root"
              action="action_fusion_fax_dashboard"
              sequence="1"/>

</odoo>
