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

    <!-- Kanban View -->
    <record id="view_api_provider_kanban" model="ir.ui.view">
        <field name="name">fusion.api.provider.kanban</field>
        <field name="model">fusion.api.provider</field>
        <field name="arch" type="xml">
            <kanban class="o_kanban_dashboard" create="false">
                <field name="name"/>
                <field name="provider_type"/>
                <field name="status"/>
                <field name="active_key_count"/>
                <field name="total_month_cost"/>
                <field name="total_month_requests"/>
                <field name="color"/>
                <field name="icon_class"/>
                <field name="website_url"/>
                <templates>
                    <t t-name="card">
                        <div class="d-flex align-items-start mb-2">
                            <div class="me-3">
                                <i t-att-class="'fa fa-2x text-primary ' + (record.icon_class.value or 'fa-plug')"/>
                            </div>
                            <div class="flex-grow-1">
                                <strong><field name="name"/></strong>
                                <div class="text-muted small">
                                    <field name="provider_type"/>
                                </div>
                            </div>
                            <div>
                                <field name="status" widget="badge"
                                       decoration-success="status == 'active'"
                                       decoration-muted="status == 'inactive'"
                                       decoration-danger="status == 'error'"/>
                            </div>
                        </div>
                        <div class="row g-2 mt-2">
                            <div class="col-4 text-center">
                                <div class="fw-bold"><field name="active_key_count"/></div>
                                <div class="text-muted small">Keys</div>
                            </div>
                            <div class="col-4 text-center">
                                <div class="fw-bold">$<field name="total_month_cost" widget="float" digits="[10,2]"/></div>
                                <div class="text-muted small">Month</div>
                            </div>
                            <div class="col-4 text-center">
                                <div class="fw-bold"><field name="total_month_requests"/></div>
                                <div class="text-muted small">Requests</div>
                            </div>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <!-- Tree View -->
    <record id="view_api_provider_tree" model="ir.ui.view">
        <field name="name">fusion.api.provider.tree</field>
        <field name="model">fusion.api.provider</field>
        <field name="arch" type="xml">
            <list>
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="provider_type"/>
                <field name="status" widget="badge"
                       decoration-success="status == 'active'"
                       decoration-muted="status == 'inactive'"
                       decoration-danger="status == 'error'"/>
                <field name="active_key_count"/>
                <field name="total_month_cost" string="Month Cost ($)"/>
                <field name="total_month_requests" string="Month Requests"/>
                <field name="website_url" widget="url" string="Dashboard"/>
            </list>
        </field>
    </record>

    <!-- Form View -->
    <record id="view_api_provider_form" model="ir.ui.view">
        <field name="name">fusion.api.provider.form</field>
        <field name="model">fusion.api.provider</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_activate" type="object" string="Activate"
                            class="btn-primary"
                            invisible="status == 'active'"/>
                    <button name="action_deactivate" type="object" string="Deactivate"
                            class="btn-secondary"
                            invisible="status != 'active'"/>
                    <field name="status" widget="statusbar"
                           statusbar_visible="inactive,active"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1><field name="name" placeholder="Provider Name"/></h1>
                    </div>
                    <group>
                        <group>
                            <field name="provider_type"/>
                            <field name="website_url" widget="url"/>
                            <field name="icon_class"/>
                        </group>
                        <group>
                            <field name="active_key_count"/>
                            <field name="total_month_cost"/>
                            <field name="total_month_requests"/>
                            <field name="sequence"/>
                        </group>
                    </group>
                    <field name="description" placeholder="Description of this API provider..."/>
                    <notebook>
                        <page string="API Keys" name="keys">
                            <field name="key_ids" context="{'default_provider_id': id}">
                                <list>
                                    <field name="name"/>
                                    <field name="masked_key" string="API Key"/>
                                    <field name="environment"/>
                                    <field name="is_default"/>
                                    <field name="is_active"/>
                                    <field name="validation_status" widget="badge"
                                           decoration-success="validation_status == 'valid'"
                                           decoration-danger="validation_status == 'invalid'"
                                           decoration-muted="validation_status == 'unknown'"/>
                                    <field name="last_validated_at"/>
                                </list>
                                <form>
                                    <header>
                                        <button name="action_validate" type="object"
                                                string="Validate Key" class="btn-primary"
                                                icon="fa-check-circle"/>
                                    </header>
                                    <sheet>
                                        <group>
                                            <group>
                                                <field name="name" placeholder="e.g. Production Key"/>
                                                <field name="api_key" password="True"
                                                       placeholder="Enter your API key"/>
                                                <field name="environment"/>
                                                <field name="is_default"/>
                                                <field name="is_active"/>
                                            </group>
                                            <group>
                                                <field name="provider_id" invisible="1"/>
                                                <field name="provider_type" invisible="1"/>
                                                <field name="validation_status" widget="badge"
                                                       decoration-success="validation_status == 'valid'"
                                                       decoration-danger="validation_status == 'invalid'"
                                                       decoration-muted="validation_status == 'unknown'"/>
                                                <field name="last_validated_at"/>
                                                <field name="company_id"
                                                       groups="base.group_multi_company"/>
                                            </group>
                                        </group>
                                        <group string="OAuth Credentials"
                                               invisible="provider_type not in ('google_oauth', 'microsoft_oauth')">
                                            <group>
                                                <field name="client_id" password="True"/>
                                                <field name="client_secret" password="True"/>
                                                <field name="redirect_uri"/>
                                            </group>
                                            <group>
                                                <field name="access_token" password="True"/>
                                                <field name="refresh_token" password="True"/>
                                                <field name="token_expiry"/>
                                            </group>
                                        </group>
                                        <field name="notes"
                                               placeholder="Optional notes about this key..."/>
                                    </sheet>
                                </form>
                            </field>
                        </page>
                        <page string="Access Rules" name="access">
                            <field name="access_ids">
                                <list>
                                    <field name="consumer_id"/>
                                    <field name="is_enabled"/>
                                    <field name="monthly_budget_usd"/>
                                    <field name="max_rpm"/>
                                    <field name="max_rpd"/>
                                    <field name="current_month_cost"/>
                                    <field name="budget_usage_pct" widget="progressbar"/>
                                </list>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Action -->
    <record id="action_api_provider" model="ir.actions.act_window">
        <field name="name">API Providers</field>
        <field name="res_model">fusion.api.provider</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Configure your first API provider
            </p>
            <p>Add API keys for OpenAI, Anthropic, Google Maps, and other services used by Fusion modules.</p>
        </field>
    </record>

</odoo>
