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

    <!-- ================================================================== -->
    <!-- External Tax Provider - Form View                                   -->
    <!-- ================================================================== -->
    <record id="fusion_external_tax_provider_form" model="ir.ui.view">
        <field name="name">fusion.external.tax.provider.form</field>
        <field name="model">fusion.external.tax.provider</field>
        <field name="arch" type="xml">
            <form string="External Tax Provider">
                <header>
                    <button name="action_test_connection"
                            string="Test Connection"
                            type="object"
                            class="btn-primary"
                            icon="fa-plug"
                            confirm="This will attempt to connect to the external tax service. Continue?"/>
                    <button name="action_activate"
                            string="Activate"
                            type="object"
                            class="btn-success"
                            invisible="is_active"
                            icon="fa-check-circle"/>
                    <button name="action_deactivate"
                            string="Deactivate"
                            type="object"
                            class="btn-warning"
                            invisible="not is_active"
                            icon="fa-times-circle"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,test"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box"/>
                    <widget name="web_ribbon" title="Active"
                            bg_color="text-bg-success"
                            invisible="not is_active"/>
                    <widget name="web_ribbon" title="Inactive"
                            bg_color="text-bg-secondary"
                            invisible="is_active"/>
                    <div class="oe_title">
                        <h1>
                            <field name="name" placeholder="e.g. AvaTax Production"/>
                        </h1>
                    </div>

                    <group>
                        <group string="General">
                            <field name="code"/>
                            <field name="provider_type"/>
                            <field name="company_id"
                                   options="{'no_create': True}"
                                   groups="base.group_multi_company"/>
                            <field name="sequence"/>
                            <field name="is_active" invisible="1"/>
                        </group>
                        <group string="Connection">
                            <field name="api_url" widget="url"/>
                            <field name="api_key" password="True"/>
                            <field name="log_requests"/>
                        </group>
                    </group>

                    <!-- AvaTax-Specific Configuration -->
                    <group string="Avalara AvaTax Configuration"
                           invisible="provider_type != 'avatax'">
                        <group>
                            <field name="avatax_account_number"/>
                            <field name="avatax_license_key" password="True"/>
                            <field name="avatax_company_code"/>
                        </group>
                        <group>
                            <field name="avatax_environment" widget="radio"/>
                            <field name="avatax_commit_on_post"/>
                            <field name="avatax_address_validation"/>
                            <field name="avatax_default_tax_code"/>
                        </group>
                    </group>

                    <!-- Last Test Result -->
                    <group string="Diagnostics"
                           invisible="state == 'draft'">
                        <field name="last_test_message"
                               readonly="1"
                               widget="text"/>
                    </group>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- External Tax Provider - List View                                   -->
    <!-- ================================================================== -->
    <record id="fusion_external_tax_provider_list" model="ir.ui.view">
        <field name="name">fusion.external.tax.provider.list</field>
        <field name="model">fusion.external.tax.provider</field>
        <field name="arch" type="xml">
            <list string="External Tax Providers"
                  decoration-success="is_active"
                  decoration-muted="not is_active">
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="code"/>
                <field name="provider_type"/>
                <field name="company_id" groups="base.group_multi_company"/>
                <field name="is_active" widget="boolean_toggle"/>
                <field name="state"
                       widget="badge"
                       decoration-success="state == 'test'"
                       decoration-danger="state == 'error'"
                       decoration-info="state == 'draft'"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- External Tax Provider - Search View                                 -->
    <!-- ================================================================== -->
    <record id="fusion_external_tax_provider_search" model="ir.ui.view">
        <field name="name">fusion.external.tax.provider.search</field>
        <field name="model">fusion.external.tax.provider</field>
        <field name="arch" type="xml">
            <search string="Search Tax Providers">
                <field name="name"/>
                <field name="code"/>
                <field name="company_id"/>
                <filter name="filter_active"
                        string="Active"
                        domain="[('is_active', '=', True)]"/>
                <filter name="filter_inactive"
                        string="Inactive"
                        domain="[('is_active', '=', False)]"/>
                <separator/>
                <filter name="filter_avatax"
                        string="AvaTax"
                        domain="[('provider_type', '=', 'avatax')]"/>
                <separator/>
                <filter name="filter_test_passed"
                        string="Test Passed"
                        domain="[('state', '=', 'test')]"/>
                <filter name="filter_error"
                        string="Connection Error"
                        domain="[('state', '=', 'error')]"/>
                <group>
                    <filter name="group_company"
                            string="Company"
                            context="{'group_by': 'company_id'}"/>
                    <filter name="group_type"
                            string="Provider Type"
                            context="{'group_by': 'provider_type'}"/>
                    <filter name="group_state"
                            string="State"
                            context="{'group_by': 'state'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- External Tax Provider - Action                                      -->
    <!-- ================================================================== -->
    <record id="fusion_external_tax_provider_action" model="ir.actions.act_window">
        <field name="name">External Tax Providers</field>
        <field name="res_model">fusion.external.tax.provider</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="fusion_external_tax_provider_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Configure your first external tax provider
            </p>
            <p>
                External tax providers integrate with services like Avalara AvaTax
                to automatically compute taxes on invoices based on jurisdiction,
                product type, and customer exemption status.
            </p>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- Menu Item: Accounting > Configuration > External Tax Providers      -->
    <!-- ================================================================== -->
    <menuitem id="menu_fusion_external_tax_providers"
              name="External Tax Providers"
              parent="account.account_account_menu"
              action="fusion_external_tax_provider_action"
              sequence="25"
              groups="account.group_account_manager"/>

    <!-- ================================================================== -->
    <!-- Invoice Form Extension - External Tax Status                        -->
    <!-- ================================================================== -->
    <record id="view_move_form_external_tax" model="ir.ui.view">
        <field name="name">account.move.form.external.tax</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            <!-- External tax status in the button box -->
            <xpath expr="//div[hasclass('oe_button_box')]" position="inside">
                <field name="fusion_is_tax_computed_externally" invisible="1"/>
                <field name="fusion_use_external_tax" invisible="1"/>
                <button name="action_compute_external_taxes"
                        class="oe_stat_button"
                        icon="fa-calculator"
                        type="object"
                        invisible="not fusion_use_external_tax or state == 'posted'"
                        string="Compute Tax"
                        title="Compute taxes via external provider"
                        groups="account.group_account_invoice"/>
                <button class="oe_stat_button"
                        icon="fa-check-circle text-success"
                        invisible="not fusion_is_tax_computed_externally"
                        disabled="1">
                    <div class="o_stat_info">
                        <span class="o_stat_text">External Tax</span>
                        <span class="o_stat_text text-success">Computed</span>
                    </div>
                </button>
            </xpath>

            <!-- External tax fields in Other Info tab -->
            <xpath expr="//page[@id='other_tab']//group[@name='accounting_info_group']" position="inside">
                <field name="fusion_tax_provider_id"
                       readonly="1"
                       invisible="not fusion_is_tax_computed_externally"/>
                <field name="fusion_external_doc_code"
                       readonly="1"
                       invisible="not fusion_is_tax_computed_externally"/>
                <field name="fusion_external_tax_amount"
                       readonly="1"
                       invisible="not fusion_is_tax_computed_externally"/>
                <field name="fusion_external_tax_date"
                       readonly="1"
                       invisible="not fusion_is_tax_computed_externally"/>
            </xpath>
        </field>
    </record>

</odoo>
