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

    <!-- Usage Log: Tree View -->
    <record id="view_api_usage_tree" model="ir.ui.view">
        <field name="name">fusion.api.usage.tree</field>
        <field name="model">fusion.api.usage</field>
        <field name="arch" type="xml">
            <list create="false" edit="false" default_order="create_date desc">
                <field name="create_date" string="Timestamp"/>
                <field name="consumer_id"/>
                <field name="provider_id"/>
                <field name="user_id"/>
                <field name="feature"/>
                <field name="model_name"/>
                <field name="tokens_in" optional="show"/>
                <field name="tokens_out" optional="show"/>
                <field name="total_tokens"/>
                <field name="estimated_cost_usd" string="Cost ($)" widget="float" digits="[10,6]"/>
                <field name="response_time_ms" string="Time (ms)" optional="hide"/>
                <field name="status" widget="badge"
                       decoration-success="status == 'success'"
                       decoration-danger="status == 'error'"
                       decoration-warning="status in ('rate_limited', 'budget_exceeded')"/>
            </list>
        </field>
    </record>

    <!-- Usage Log: Form View -->
    <record id="view_api_usage_form" model="ir.ui.view">
        <field name="name">fusion.api.usage.form</field>
        <field name="model">fusion.api.usage</field>
        <field name="arch" type="xml">
            <form create="false" edit="false">
                <sheet>
                    <group>
                        <group>
                            <field name="create_date"/>
                            <field name="consumer_id"/>
                            <field name="provider_id"/>
                            <field name="user_id"/>
                        </group>
                        <group>
                            <field name="feature"/>
                            <field name="model_name"/>
                            <field name="status"/>
                            <field name="response_time_ms"/>
                        </group>
                    </group>
                    <group>
                        <group string="Token Usage">
                            <field name="tokens_in"/>
                            <field name="tokens_out"/>
                            <field name="total_tokens"/>
                        </group>
                        <group string="Cost">
                            <field name="estimated_cost_usd"/>
                        </group>
                    </group>
                    <group string="Error Details" invisible="not error_message">
                        <field name="error_message" nolabel="1"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Usage Log: Pivot View -->
    <record id="view_api_usage_pivot" model="ir.ui.view">
        <field name="name">fusion.api.usage.pivot</field>
        <field name="model">fusion.api.usage</field>
        <field name="arch" type="xml">
            <pivot string="API Usage Analysis">
                <field name="consumer_id" type="row"/>
                <field name="provider_id" type="col"/>
                <field name="estimated_cost_usd" type="measure"/>
                <field name="total_tokens" type="measure"/>
            </pivot>
        </field>
    </record>

    <!-- Usage Log: Graph View -->
    <record id="view_api_usage_graph" model="ir.ui.view">
        <field name="name">fusion.api.usage.graph</field>
        <field name="model">fusion.api.usage</field>
        <field name="arch" type="xml">
            <graph string="API Usage" type="bar">
                <field name="create_date" interval="day" type="row"/>
                <field name="estimated_cost_usd" type="measure"/>
            </graph>
        </field>
    </record>

    <!-- Usage Log: Search View -->
    <record id="view_api_usage_search" model="ir.ui.view">
        <field name="name">fusion.api.usage.search</field>
        <field name="model">fusion.api.usage</field>
        <field name="arch" type="xml">
            <search string="Search Usage">
                <field name="consumer_id"/>
                <field name="provider_id"/>
                <field name="user_id"/>
                <field name="feature"/>
                <field name="model_name"/>
                <separator/>
                <filter name="filter_success" string="Success"
                        domain="[('status', '=', 'success')]"/>
                <filter name="filter_errors" string="Errors"
                        domain="[('status', '=', 'error')]"/>
                <filter name="filter_rate_limited" string="Rate Limited"
                        domain="[('status', '=', 'rate_limited')]"/>
                <filter name="filter_budget_exceeded" string="Budget Exceeded"
                        domain="[('status', '=', 'budget_exceeded')]"/>
                <separator/>
                <filter name="group_consumer" string="Consumer" context="{'group_by': 'consumer_id'}"/>
                <filter name="group_provider" string="Provider" context="{'group_by': 'provider_id'}"/>
                <filter name="group_user" string="User" context="{'group_by': 'user_id'}"/>
                <filter name="group_feature" string="Feature" context="{'group_by': 'feature'}"/>
                <filter name="group_model" string="AI Model" context="{'group_by': 'model_name'}"/>
                <filter name="group_status" string="Status" context="{'group_by': 'status'}"/>
                <filter name="group_day" string="Day" context="{'group_by': 'create_date:day'}"/>
                <filter name="group_month" string="Month" context="{'group_by': 'create_date:month'}"/>
            </search>
        </field>
    </record>

    <!-- Usage Log: Action -->
    <record id="action_api_usage" model="ir.actions.act_window">
        <field name="name">Usage Log</field>
        <field name="res_model">fusion.api.usage</field>
        <field name="view_mode">list,pivot,graph,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No API calls recorded yet
            </p>
            <p>Usage data appears here as Fusion modules make API calls through the Fusion API service.</p>
        </field>
    </record>

    <!-- Daily Summary: Tree View -->
    <record id="view_api_usage_daily_tree" model="ir.ui.view">
        <field name="name">fusion.api.usage.daily.tree</field>
        <field name="model">fusion.api.usage.daily</field>
        <field name="arch" type="xml">
            <list create="false" edit="false" default_order="date desc">
                <field name="date"/>
                <field name="consumer_id"/>
                <field name="provider_id"/>
                <field name="user_id" optional="hide"/>
                <field name="feature" optional="hide"/>
                <field name="model_name" optional="show"/>
                <field name="request_count"/>
                <field name="error_count" optional="show"/>
                <field name="total_tokens"/>
                <field name="total_cost_usd" string="Cost ($)" widget="float" digits="[10,4]"/>
                <field name="avg_response_time_ms" string="Avg Time (ms)" optional="hide"/>
            </list>
        </field>
    </record>

    <!-- Daily Summary: Pivot View -->
    <record id="view_api_usage_daily_pivot" model="ir.ui.view">
        <field name="name">fusion.api.usage.daily.pivot</field>
        <field name="model">fusion.api.usage.daily</field>
        <field name="arch" type="xml">
            <pivot string="Daily Usage Summary">
                <field name="date" interval="month" type="row"/>
                <field name="consumer_id" type="row"/>
                <field name="provider_id" type="col"/>
                <field name="total_cost_usd" type="measure"/>
                <field name="request_count" type="measure"/>
                <field name="total_tokens" type="measure"/>
            </pivot>
        </field>
    </record>

    <!-- Daily Summary: Graph View -->
    <record id="view_api_usage_daily_graph" model="ir.ui.view">
        <field name="name">fusion.api.usage.daily.graph</field>
        <field name="model">fusion.api.usage.daily</field>
        <field name="arch" type="xml">
            <graph string="Daily Usage Trends" type="line">
                <field name="date" type="row"/>
                <field name="total_cost_usd" type="measure"/>
            </graph>
        </field>
    </record>

    <!-- Daily Summary: Search View -->
    <record id="view_api_usage_daily_search" model="ir.ui.view">
        <field name="name">fusion.api.usage.daily.search</field>
        <field name="model">fusion.api.usage.daily</field>
        <field name="arch" type="xml">
            <search string="Search Daily Summary">
                <field name="consumer_id"/>
                <field name="provider_id"/>
                <field name="user_id"/>
                <field name="feature"/>
                <field name="model_name"/>
                <separator/>
                <filter name="group_consumer" string="Consumer" context="{'group_by': 'consumer_id'}"/>
                <filter name="group_provider" string="Provider" context="{'group_by': 'provider_id'}"/>
                <filter name="group_user" string="User" context="{'group_by': 'user_id'}"/>
                <filter name="group_model" string="AI Model" context="{'group_by': 'model_name'}"/>
                <filter name="group_month" string="Month" context="{'group_by': 'date:month'}"/>
            </search>
        </field>
    </record>

    <!-- Daily Summary: Action -->
    <record id="action_api_usage_daily" model="ir.actions.act_window">
        <field name="name">Daily Summary</field>
        <field name="res_model">fusion.api.usage.daily</field>
        <field name="view_mode">list,pivot,graph,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No daily summaries yet
            </p>
            <p>Daily usage summaries are aggregated automatically each night by a scheduled action.</p>
        </field>
    </record>

</odoo>
