<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- =============================================================
         Fusion Document Extractor – List View
         ============================================================= -->
    <record id="fusion_document_extractor_view_list" model="ir.ui.view">
        <field name="name">fusion.document.extractor.list</field>
        <field name="model">fusion.document.extractor</field>
        <field name="arch" type="xml">
            <list string="Document Extraction Providers"
                  editable="bottom"
                  decoration-muted="not is_active">
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="provider"/>
                <field name="tesseract_lang"
                       optional="hide"
                       column_invisible="True"/>
                <field name="api_endpoint" optional="hide"/>
                <field name="is_active" widget="boolean_toggle"/>
                <field name="company_id"
                       optional="show"
                       groups="base.group_multi_company"/>
            </list>
        </field>
    </record>

    <!-- =============================================================
         Fusion Document Extractor – Form View
         ============================================================= -->
    <record id="fusion_document_extractor_view_form" model="ir.ui.view">
        <field name="name">fusion.document.extractor.form</field>
        <field name="model">fusion.document.extractor</field>
        <field name="arch" type="xml">
            <form string="Document Extraction Provider">
                <header>
                    <button name="action_test_connection"
                            string="Test Connection"
                            type="object"
                            class="btn-primary"
                            icon="fa-plug"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <field name="is_active" widget="boolean_button"
                               options="{'terminology': 'active'}"/>
                    </div>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" placeholder="e.g. Production OCR"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Provider Settings">
                            <field name="provider" widget="radio"/>
                            <field name="sequence"/>
                            <field name="company_id"
                                   groups="base.group_multi_company"
                                   options="{'no_create': True}"/>
                        </group>
                        <group string="Credentials">
                            <field name="api_key"
                                   password="True"
                                   invisible="provider == 'tesseract'"
                                   required="provider in ('google_vision', 'azure_ai')"/>
                            <field name="api_endpoint"
                                   invisible="provider != 'azure_ai'"
                                   required="provider == 'azure_ai'"
                                   placeholder="https://your-resource.cognitiveservices.azure.com"/>
                            <field name="tesseract_lang"
                                   invisible="provider != 'tesseract'"/>
                        </group>
                    </group>
                    <div class="alert alert-info" role="alert"
                         invisible="provider != 'tesseract'">
                        <p class="mb-0">
                            <i class="fa fa-info-circle"/> Tesseract runs locally on
                            the server. Make sure the <code>tesseract</code> binary
                            and the <code>pytesseract</code> Python package are
                            installed. No internet connection is required.
                        </p>
                    </div>
                    <div class="alert alert-info" role="alert"
                         invisible="provider != 'google_vision'">
                        <p class="mb-0">
                            <i class="fa fa-info-circle"/> Paste your Google Cloud
                            API key above. Ensure the <em>Cloud Vision API</em> is
                            enabled in your GCP project.
                        </p>
                    </div>
                    <div class="alert alert-info" role="alert"
                         invisible="provider != 'azure_ai'">
                        <p class="mb-0">
                            <i class="fa fa-info-circle"/> Enter your Azure AI
                            Document Intelligence endpoint URL and subscription key.
                            The <em>prebuilt-invoice</em> model is used automatically.
                        </p>
                    </div>
                </sheet>
            </form>
        </field>
    </record>

    <!-- =============================================================
         Fusion Document Extractor – Action
         ============================================================= -->
    <record id="fusion_document_extractor_action" model="ir.actions.act_window">
        <field name="name">Document Extraction</field>
        <field name="res_model">fusion.document.extractor</field>
        <field name="view_mode">list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Configure a Document AI extraction provider
            </p>
            <p>
                Set up Tesseract (local), Google Cloud Vision, or Azure AI
                Document Intelligence to automatically extract invoice data
                from scanned documents.
            </p>
        </field>
    </record>

    <!-- =============================================================
         Menu Item: Accounting > Configuration > Document Extraction
         ============================================================= -->
    <menuitem id="fusion_menu_document_extraction"
              name="Document Extraction"
              parent="account.menu_finance_configuration"
              action="fusion_document_extractor_action"
              sequence="100"
              groups="account.group_account_manager"/>

    <!-- =============================================================
         Extend Invoice / Bill Form – Add Extract Button & OCR Fields
         ============================================================= -->
    <record id="fusion_account_move_extraction_form" model="ir.ui.view">
        <field name="name">account.move.form.fusion.extraction</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">99</field>
        <field name="arch" type="xml">
            <!-- Add Extract button in the header -->
            <xpath expr="//header" position="inside">
                <button name="action_extract_from_attachment"
                        string="Extract from Scan"
                        type="object"
                        class="btn-secondary"
                        icon="fa-magic"
                        invisible="state != 'draft'"
                        groups="account.group_account_invoice"/>
            </xpath>

            <!-- Add OCR status and confidence fields after the main header group -->
            <xpath expr="//page[@name='other_info']" position="inside">
                <group string="Document Extraction (OCR)" name="fusion_ocr_group">
                    <group>
                        <field name="fusion_extraction_status"
                               widget="badge"
                               decoration-info="fusion_extraction_status == 'to_extract'"
                               decoration-warning="fusion_extraction_status == 'extracting'"
                               decoration-success="fusion_extraction_status == 'done'"
                               decoration-danger="fusion_extraction_status == 'failed'"
                               readonly="1"/>
                        <field name="fusion_extraction_confidence"
                               widget="progressbar"
                               readonly="1"
                               invisible="not fusion_extraction_status"/>
                    </group>
                    <group>
                        <field name="fusion_extractor_id"
                               readonly="1"
                               invisible="not fusion_extractor_id"/>
                    </group>
                    <field name="fusion_ocr_raw_text"
                           readonly="1"
                           invisible="not fusion_ocr_raw_text"
                           nolabel="1"
                           colspan="2"/>
                </group>
            </xpath>
        </field>
    </record>
</odoo>
