<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- =============================================================
         Fusion EDI Document – List View
         ============================================================= -->
    <record id="fusion_edi_document_view_list" model="ir.ui.view">
        <field name="name">fusion.edi.document.list</field>
        <field name="model">fusion.edi.document</field>
        <field name="arch" type="xml">
            <list string="EDI Documents"
                  decoration-danger="blocking_level == 'error'"
                  decoration-warning="blocking_level == 'warning'"
                  decoration-muted="state == 'cancelled'">
                <field name="move_name" string="Invoice"/>
                <field name="edi_format_id"/>
                <field name="partner_id" optional="show"/>
                <field name="state"
                       widget="badge"
                       decoration-info="state == 'to_send'"
                       decoration-success="state == 'sent'"
                       decoration-warning="state == 'to_cancel'"
                       decoration-muted="state == 'cancelled'"/>
                <field name="blocking_level" column_invisible="True"/>
                <field name="error_message" optional="hide"/>
                <field name="create_date" string="Created" optional="show"/>
            </list>
        </field>
    </record>

    <!-- =============================================================
         Fusion EDI Document – Form View
         ============================================================= -->
    <record id="fusion_edi_document_view_form" model="ir.ui.view">
        <field name="name">fusion.edi.document.form</field>
        <field name="model">fusion.edi.document</field>
        <field name="arch" type="xml">
            <form string="EDI Document">
                <header>
                    <button name="action_send"
                            string="Generate &amp; Send"
                            type="object"
                            class="btn-primary"
                            invisible="state != 'to_send'"/>
                    <button name="action_cancel"
                            string="Cancel"
                            type="object"
                            invisible="state not in ('sent', 'to_cancel')"
                            confirm="Are you sure you want to cancel this EDI document?"/>
                    <button name="action_retry"
                            string="Retry"
                            type="object"
                            class="btn-warning"
                            invisible="not error_message"/>
                    <field name="state"
                           widget="statusbar"
                           statusbar_visible="to_send,sent,cancelled"/>
                </header>
                <sheet>
                    <div class="oe_alert oe_alert_warning"
                         invisible="not error_message">
                        <strong>Error: </strong>
                        <field name="error_message" nolabel="1"/>
                    </div>
                    <group>
                        <group string="Document">
                            <field name="move_id"/>
                            <field name="edi_format_id"/>
                            <field name="attachment_id" filename="display_name"/>
                        </group>
                        <group string="Details">
                            <field name="partner_id"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                            <field name="blocking_level" invisible="not error_message"/>
                            <field name="create_date" string="Created On"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- =============================================================
         Fusion EDI Document – Search View
         ============================================================= -->
    <record id="fusion_edi_document_view_search" model="ir.ui.view">
        <field name="name">fusion.edi.document.search</field>
        <field name="model">fusion.edi.document</field>
        <field name="arch" type="xml">
            <search string="EDI Documents">
                <field name="move_name" string="Invoice"/>
                <field name="partner_id"/>
                <field name="edi_format_id"/>
                <separator/>
                <filter name="to_send" string="To Send"
                        domain="[('state', '=', 'to_send')]"/>
                <filter name="sent" string="Sent"
                        domain="[('state', '=', 'sent')]"/>
                <filter name="errors" string="With Errors"
                        domain="[('error_message', '!=', False)]"/>
                <separator/>
                <group>
                    <filter name="group_format" string="Format"
                            context="{'group_by': 'edi_format_id'}"/>
                    <filter name="group_state" string="Status"
                            context="{'group_by': 'state'}"/>
                    <filter name="group_partner" string="Partner"
                            context="{'group_by': 'partner_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- =============================================================
         Fusion EDI Document – Action
         ============================================================= -->
    <record id="fusion_edi_document_action" model="ir.actions.act_window">
        <field name="name">EDI Documents</field>
        <field name="res_model">fusion.edi.document</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="fusion_edi_document_view_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No EDI documents yet
            </p>
            <p>
                EDI documents are automatically created when you send
                invoices electronically. You can also generate them
                manually from the invoice form.
            </p>
        </field>
    </record>


    <!-- =============================================================
         Fusion EDI Format – List View
         ============================================================= -->
    <record id="fusion_edi_format_view_list" model="ir.ui.view">
        <field name="name">fusion.edi.format.list</field>
        <field name="model">fusion.edi.format</field>
        <field name="arch" type="xml">
            <list string="EDI Formats" editable="bottom">
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="code"/>
                <field name="applicable_to"/>
                <field name="active" widget="boolean_toggle"/>
            </list>
        </field>
    </record>

    <!-- =============================================================
         Fusion EDI Format – Form View
         ============================================================= -->
    <record id="fusion_edi_format_view_form" model="ir.ui.view">
        <field name="name">fusion.edi.format.form</field>
        <field name="model">fusion.edi.format</field>
        <field name="arch" type="xml">
            <form string="EDI Format">
                <sheet>
                    <group>
                        <group string="General">
                            <field name="name"/>
                            <field name="code"/>
                            <field name="applicable_to"/>
                            <field name="active"/>
                        </group>
                        <group string="Options">
                            <field name="sequence"/>
                        </group>
                    </group>
                    <group string="Description">
                        <field name="description" nolabel="1" colspan="2"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- =============================================================
         Fusion EDI Format – Action
         ============================================================= -->
    <record id="fusion_edi_format_action" model="ir.actions.act_window">
        <field name="name">EDI Formats</field>
        <field name="res_model">fusion.edi.format</field>
        <field name="view_mode">list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Configure EDI formats
            </p>
            <p>
                Define the electronic document interchange formats your
                company supports, such as UBL 2.1 or Factur-X.
            </p>
        </field>
    </record>


    <!-- =============================================================
         Menu Items
         ============================================================= -->
    <menuitem id="menu_edi_root"
              name="EDI"
              parent="account.account_account_menu"
              sequence="90"/>

    <menuitem id="menu_edi_formats"
              name="EDI Formats"
              parent="menu_edi_root"
              action="fusion_edi_format_action"
              sequence="10"
              groups="account.group_account_manager"/>

    <menuitem id="menu_edi_documents"
              name="EDI Documents"
              parent="menu_edi_root"
              action="fusion_edi_document_action"
              sequence="20"
              groups="account.group_account_user"/>


    <!-- =============================================================
         Account Move – Inherit form to add EDI tab & buttons
         ============================================================= -->
    <record id="view_move_form_edi_inherit" model="ir.ui.view">
        <field name="name">account.move.form.edi.inherit</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="arch" type="xml">
            <!-- EDI stat button in the button box -->
            <xpath expr="//div[hasclass('oe_button_box')]" position="inside">
                <field name="edi_document_count" invisible="1"/>
                <button name="action_view_edi_documents"
                        class="oe_stat_button"
                        icon="fa-file-code-o"
                        type="object"
                        invisible="edi_document_count == 0">
                    <field name="edi_document_count"
                           string="EDI Docs"
                           widget="statinfo"/>
                </button>
            </xpath>

            <!-- EDI action buttons in the header -->
            <xpath expr="//header" position="inside">
                <button name="action_send_edi"
                        string="Send EDI"
                        type="object"
                        class="btn-secondary"
                        invisible="state != 'posted'"
                        groups="account.group_account_invoice"/>
                <button name="action_export_edi_xml"
                        string="Export XML"
                        type="object"
                        class="btn-secondary"
                        invisible="edi_document_count == 0"
                        groups="account.group_account_invoice"/>
            </xpath>

            <!-- EDI tab after existing notebook pages -->
            <xpath expr="//page[@id='other_tab']" position="after">
                <page id="edi_tab" string="EDI"
                      invisible="edi_document_count == 0">
                    <div class="oe_alert oe_alert_warning"
                         invisible="not edi_error_message"
                         style="margin-bottom: 8px;">
                        <field name="edi_blocking_level" invisible="1"/>
                        <strong>EDI Error: </strong>
                        <field name="edi_error_message" nolabel="1"/>
                    </div>
                    <field name="edi_state" invisible="1"/>
                    <field name="edi_document_ids" nolabel="1" readonly="1">
                        <list decoration-danger="blocking_level == 'error'"
                              decoration-warning="blocking_level == 'warning'"
                              decoration-muted="state == 'cancelled'">
                            <field name="edi_format_id" string="Format"/>
                            <field name="state"
                                   widget="badge"
                                   decoration-info="state == 'to_send'"
                                   decoration-success="state == 'sent'"
                                   decoration-warning="state == 'to_cancel'"
                                   decoration-muted="state == 'cancelled'"/>
                            <field name="attachment_id" string="File"/>
                            <field name="blocking_level" column_invisible="True"/>
                            <field name="error_message" optional="hide"/>
                            <button name="action_send"
                                    string="Send"
                                    type="object"
                                    icon="fa-paper-plane"
                                    invisible="state != 'to_send'"/>
                            <button name="action_retry"
                                    string="Retry"
                                    type="object"
                                    icon="fa-refresh"
                                    invisible="not error_message"/>
                        </list>
                    </field>
                </page>
            </xpath>
        </field>
    </record>


    <!-- =============================================================
         EDI Import Wizard – Form View
         ============================================================= -->
    <record id="fusion_edi_import_wizard_view_form" model="ir.ui.view">
        <field name="name">fusion.edi.import.wizard.form</field>
        <field name="model">fusion.edi.import.wizard</field>
        <field name="arch" type="xml">
            <form string="Import EDI Invoice">
                <group>
                    <field name="xml_file" filename="xml_filename"/>
                    <field name="xml_filename" invisible="1"/>
                    <field name="move_type"/>
                </group>
                <footer>
                    <button name="action_import"
                            string="Import"
                            type="object"
                            class="btn-primary"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <record id="fusion_edi_import_wizard_action" model="ir.actions.act_window">
        <field name="name">Import EDI Invoice</field>
        <field name="res_model">fusion.edi.import.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
    </record>


    <!-- =============================================================
         EDI Format – Seed Data (default formats)
         ============================================================= -->
    <record id="edi_format_ubl_21" model="fusion.edi.format" forcecreate="0">
        <field name="name">UBL 2.1</field>
        <field name="code">ubl_21</field>
        <field name="applicable_to">both</field>
        <field name="sequence">10</field>
        <field name="description">OASIS Universal Business Language 2.1 – generates Invoice and CreditNote XML documents conforming to the UBL 2.1 specification. Widely used for Peppol e-invoicing across Europe and beyond.</field>
    </record>

    <record id="edi_format_cii" model="fusion.edi.format" forcecreate="0">
        <field name="name">UN/CEFACT CII</field>
        <field name="code">cii</field>
        <field name="applicable_to">both</field>
        <field name="sequence">20</field>
        <field name="description">UN/CEFACT Cross-Industry Invoice – generates CrossIndustryInvoice XML documents. Used as the basis for Factur-X / ZUGFeRD hybrid PDF invoices in France and Germany.</field>
    </record>

    <record id="edi_format_facturx" model="fusion.edi.format" forcecreate="0">
        <field name="name">Factur-X (CII)</field>
        <field name="code">facturx</field>
        <field name="applicable_to">both</field>
        <field name="sequence">30</field>
        <field name="description">Factur-X / ZUGFeRD profile – generates CII XML and can embed it into a PDF/A-3 container for hybrid human/machine-readable invoices. Supports Minimum, Basic, and EN 16931 profiles.</field>
    </record>

</odoo>
