<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Pivot View -->
    <record id="view_sale_order_kpi_pivot" model="ir.ui.view">
        <field name="name">sale.order.kpi.pivot</field>
        <field name="model">sale.order</field>
        <field name="arch" type="xml">
            <pivot string="Análisis de Ventas (KPIs)" sample="1">
                <field name="date_order" type="row" interval="month"/>
                <field name="partner_id" type="row"/>
                <field name="amount_total" type="measure"/>
                <field name="amount_untaxed" type="measure"/>
            </pivot>
        </field>
    </record>

    <!-- Graph View -->
    <record id="view_sale_order_kpi_graph" model="ir.ui.view">
        <field name="name">sale.order.kpi.graph</field>
        <field name="model">sale.order</field>
        <field name="arch" type="xml">
            <graph string="Ventas por Canal" type="bar" sample="1">
                <field name="date_order" interval="month"/>
                <field name="amount_total" type="measure"/>
            </graph>
        </field>
    </record>

    <!-- Search View Extension to add Shopify vs B2B filters -->
    <record id="view_sale_order_kpi_search" model="ir.ui.view">
        <field name="name">sale.order.kpi.search</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_sales_order_filter"/>
        <field name="arch" type="xml">
            <xpath expr="//filter[@name='my_sale_orders_filter']" position="before">
                <filter string="Canal: Retail (Shopify)" name="channel_shopify" domain="[('shopify_order_id', '!=', False)]"/>
                <filter string="Canal: Custom (B2B)" name="channel_b2b" domain="[('shopify_order_id', '=', False)]"/>
                <separator/>
            </xpath>
        </field>
    </record>

    <!-- Action -->
    <record id="action_hazard_kpi_reports" model="ir.actions.act_window">
        <field name="name">Análisis y KPIs (B2B / Retail)</field>
        <field name="res_model">sale.order</field>
        <field name="view_mode">pivot,graph,tree,form</field>
        <field name="domain">[('state', 'in', ['sale', 'done'])]</field>
        <field name="search_view_id" ref="sale.view_sales_order_filter"/>
    </record>

    <!-- Let's map specific view modes to the action -->
    <record id="action_hazard_kpi_reports_pivot" model="ir.actions.act_window.view">
        <field name="sequence" eval="1"/>
        <field name="view_mode">pivot</field>
        <field name="view_id" ref="view_sale_order_kpi_pivot"/>
        <field name="act_window_id" ref="action_hazard_kpi_reports"/>
    </record>
    <record id="action_hazard_kpi_reports_graph" model="ir.actions.act_window.view">
        <field name="sequence" eval="2"/>
        <field name="view_mode">graph</field>
        <field name="view_id" ref="view_sale_order_kpi_graph"/>
        <field name="act_window_id" ref="action_hazard_kpi_reports"/>
    </record>

    <!-- Menu Item -->
    <menuitem id="menu_hazard_kpi_reports"
              name="Análisis KPIs (Retail / B2B)"
              parent="sale.menu_sale_report"
              action="action_hazard_kpi_reports"
              sequence="5"/>
</odoo>
