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

    <!-- ===== Tree View ===== -->
    <record id="woo_instance_view_tree" model="ir.ui.view">
        <field name="name">woo.instance.tree</field>
        <field name="model">woo.instance</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                <field name="url"/>
                <field name="company_id" groups="base.group_multi_company"/>
                <field name="last_sync"/>
                <field name="state" widget="badge"
                       decoration-success="state == 'connected'"
                       decoration-warning="state == 'draft'"
                       decoration-danger="state == 'error'"/>
            </tree>
        </field>
    </record>

    <!-- ===== Form View ===== -->
    <record id="woo_instance_view_form" model="ir.ui.view">
        <field name="name">woo.instance.form</field>
        <field name="model">woo.instance</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_test_connection" type="object"
                            string="Test Connection" class="oe_highlight"/>
                    <button name="action_generate_api_key" type="object"
                            string="Generate API Key"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,connected"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button class="oe_stat_button" icon="fa-link" type="object"
                                name="action_view_product_maps">
                            <field name="mapped_count" widget="statinfo"
                                   string="Mapped"/>
                        </button>
                        <button class="oe_stat_button" icon="fa-chain-broken" type="object"
                                name="action_view_product_maps">
                            <field name="unmapped_count" widget="statinfo"
                                   string="Unmapped"/>
                        </button>
                        <button class="oe_stat_button" icon="fa-exclamation-triangle" type="object"
                                name="action_view_sync_errors">
                            <field name="error_count" widget="statinfo"
                                   string="Errors (24h)"/>
                        </button>
                    </div>
                    <div class="oe_title">
                        <h1><field name="name" placeholder="Instance Name"/></h1>
                    </div>
                    <group>
                        <group string="Connection">
                            <field name="url" placeholder="https://store.example.com"/>
                            <field name="consumer_key" password="True"/>
                            <field name="consumer_secret" password="True"/>
                            <field name="webhook_secret" password="True"/>
                            <field name="wc_api_version"/>
                            <field name="odoo_api_key" readonly="1" widget="CopyClipboardChar"/>
                        </group>
                        <group string="Company">
                            <field name="company_id" groups="base.group_multi_company"/>
                            <field name="last_sync"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Sync Settings" name="sync_settings">
                            <group>
                                <group>
                                    <field name="sync_interval"/>
                                    <field name="default_warehouse_id"/>
                                </group>
                                <group>
                                    <field name="sync_products"/>
                                    <field name="sync_orders"/>
                                    <field name="sync_invoices"/>
                                    <field name="sync_inventory"/>
                                    <field name="sync_customers"/>
                                </group>
                            </group>
                        </page>
                        <page string="Notifications" name="notifications">
                            <group>
                                <field name="notify_on_failure"/>
                                <field name="notify_user_ids" widget="many2many_tags"
                                       invisible="not notify_on_failure"/>
                            </group>
                        </page>
                        <page string="Product Mappings" name="product_maps">
                            <field name="product_map_ids">
                                <tree editable="bottom">
                                    <field name="product_id"/>
                                    <field name="woo_product_name"/>
                                    <field name="woo_sku"/>
                                    <field name="woo_product_type"/>
                                    <field name="sync_price"/>
                                    <field name="sync_inventory"/>
                                    <field name="last_synced"/>
                                    <field name="state" widget="badge"
                                           decoration-success="state == 'mapped'"
                                           decoration-warning="state == 'conflict'"
                                           decoration-danger="state == 'error'"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Orders" name="orders">
                            <field name="order_ids">
                                <tree>
                                    <field name="woo_order_number"/>
                                    <field name="sale_order_id"/>
                                    <field name="woo_status"/>
                                    <field name="state" widget="badge"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Sync Log" name="sync_log">
                            <field name="sync_log_ids" readonly="1">
                                <tree>
                                    <field name="create_date"/>
                                    <field name="sync_type"/>
                                    <field name="direction"/>
                                    <field name="record_ref"/>
                                    <field name="state" widget="badge"
                                           decoration-success="state == 'success'"
                                           decoration-danger="state == 'failed'"
                                           decoration-warning="state == 'conflict'"/>
                                    <field name="message"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ===== Search View ===== -->
    <record id="woo_instance_view_search" model="ir.ui.view">
        <field name="name">woo.instance.search</field>
        <field name="model">woo.instance</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="url"/>
                <filter name="connected" string="Connected"
                        domain="[('state', '=', 'connected')]"/>
                <filter name="draft" string="Draft"
                        domain="[('state', '=', 'draft')]"/>
                <filter name="error" string="Error"
                        domain="[('state', '=', 'error')]"/>
                <group expand="1" string="Group By">
                    <filter name="group_state" string="State"
                            context="{'group_by': 'state'}"/>
                    <filter name="group_company" string="Company"
                            context="{'group_by': 'company_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- ===== Action ===== -->
    <record id="action_woo_instance" model="ir.actions.act_window">
        <field name="name">WooCommerce Instances</field>
        <field name="res_model">woo.instance</field>
        <field name="view_mode">tree,form</field>
        <field name="search_view_id" ref="woo_instance_view_search"/>
    </record>

</odoo>
