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

    <!-- Sync Config Form View -->
    <record id="view_fusion_sync_config_form" model="ir.ui.view">
        <field name="name">fusion.sync.config.form</field>
        <field name="model">fusion.sync.config</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_test_connection" type="object"
                            string="Test Connection" class="btn-primary"
                            invisible="state == 'connected'"/>
                    <button name="action_test_connection" type="object"
                            string="Re-Test Connection"
                            invisible="state != 'connected'"/>
                    <button name="action_sync_now" type="object"
                            string="Sync Now" class="btn-primary"
                            invisible="state != 'connected'"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,connected"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1><field name="name" placeholder="e.g., Mobility Specialties"/></h1>
                    </div>
                    <group>
                        <group string="Connection">
                            <field name="url" placeholder="https://erp.mobilityspecialties.com"/>
                            <field name="db_name" placeholder="mobility-prod"/>
                            <field name="username" placeholder="admin"/>
                            <field name="api_key" password="True"/>
                        </group>
                        <group string="Sync Settings">
                            <field name="sync_products"/>
                            <field name="sync_stock"/>
                            <field name="sync_interval"/>
                            <field name="remote_warehouse_name"
                                   placeholder="Leave empty for all warehouses"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                    </group>
                    <group>
                        <group string="Inter-Company">
                            <field name="remote_partner_id"
                                   placeholder="Select the partner representing the remote company"/>
                            <field name="local_company_name"
                                   placeholder="e.g., Mobility Specialties Inc"/>
                        </group>
                        <group string="Shared Warehouse">
                            <field name="is_shared_warehouse"/>
                            <field name="warehouse_location_id"
                                   invisible="not is_shared_warehouse"/>
                        </group>
                    </group>
                    <group string="Status">
                        <field name="last_sync"/>
                        <field name="last_sync_status"/>
                        <field name="remote_uid" invisible="state != 'connected'"/>
                    </group>
                    <notebook>
                        <page string="Product Mappings" name="mappings">
                            <field name="active" invisible="1"/>
                            <p class="text-muted" invisible="state == 'connected'">
                                Connect and sync to see product mappings here.
                            </p>
                        </page>
                        <page string="Remote Warehouses" name="warehouses">
                            <field name="sync_warehouse_ids" readonly="1">
                                <list>
                                    <field name="name"/>
                                    <field name="code"/>
                                    <field name="company_name"/>
                                    <field name="remote_warehouse_id"/>
                                    <field name="remote_lot_stock_id"/>
                                    <field name="active" widget="boolean"/>
                                </list>
                            </field>
                            <p class="text-muted" invisible="sync_warehouse_count > 0">
                                Run "Sync Now" to discover remote warehouses.
                            </p>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Sync Config List View -->
    <record id="view_fusion_sync_config_list" model="ir.ui.view">
        <field name="name">fusion.sync.config.list</field>
        <field name="model">fusion.sync.config</field>
        <field name="arch" type="xml">
            <list>
                <field name="name"/>
                <field name="url"/>
                <field name="is_shared_warehouse" widget="boolean"/>
                <field name="state" widget="badge"
                       decoration-success="state == 'connected'"
                       decoration-danger="state == 'error'"
                       decoration-info="state == 'draft'"/>
                <field name="last_sync"/>
                <field name="last_sync_status"/>
            </list>
        </field>
    </record>

    <!-- Product Mapping List View -->
    <record id="view_fusion_product_sync_mapping_list" model="ir.ui.view">
        <field name="name">fusion.product.sync.mapping.list</field>
        <field name="model">fusion.product.sync.mapping</field>
        <field name="arch" type="xml">
            <list editable="bottom">
                <field name="remote_product_name"/>
                <field name="remote_default_code"/>
                <field name="remote_barcode" optional="hide"/>
                <field name="local_product_id"/>
                <field name="auto_matched" widget="boolean"/>
                <field name="remote_qty_available"
                       decoration-danger="remote_qty_available == 0"
                       decoration-success="remote_qty_available > 0"/>
                <field name="remote_qty_forecast"/>
                <field name="owner_config_id" optional="show"/>
                <field name="last_stock_sync"/>
                <field name="config_id" column_invisible="1"/>
            </list>
        </field>
    </record>

    <!-- Product Mapping Search View -->
    <record id="view_fusion_product_sync_mapping_search" model="ir.ui.view">
        <field name="name">fusion.product.sync.mapping.search</field>
        <field name="model">fusion.product.sync.mapping</field>
        <field name="arch" type="xml">
            <search>
                <field name="remote_product_name"/>
                <field name="remote_default_code"/>
                <field name="local_product_id"/>
                <separator/>
                <filter name="mapped" string="Mapped"
                        domain="[('local_product_id', '!=', False)]"/>
                <filter name="unmapped" string="Unmapped"
                        domain="[('local_product_id', '=', False)]"/>
                <filter name="in_stock" string="Remote In Stock"
                        domain="[('remote_qty_available', '&gt;', 0)]"/>
            </search>
        </field>
    </record>

    <!-- Sync Log List View -->
    <record id="view_fusion_sync_log_list" model="ir.ui.view">
        <field name="name">fusion.sync.log.list</field>
        <field name="model">fusion.sync.log</field>
        <field name="arch" type="xml">
            <list>
                <field name="create_date" string="Date"/>
                <field name="config_id"/>
                <field name="direction"/>
                <field name="sync_type"/>
                <field name="status" widget="badge"
                       decoration-success="status == 'success'"
                       decoration-warning="status == 'partial'"
                       decoration-danger="status == 'error'"/>
                <field name="summary"/>
                <field name="product_count"/>
            </list>
        </field>
    </record>

    <!-- Sync Config Action -->
    <record id="action_fusion_sync_config" model="ir.actions.act_window">
        <field name="name">Remote Connections</field>
        <field name="res_model">fusion.sync.config</field>
        <field name="view_mode">list,form</field>
    </record>

    <!-- Product Mapping Action -->
    <record id="action_fusion_product_mapping" model="ir.actions.act_window">
        <field name="name">Product Mappings</field>
        <field name="res_model">fusion.product.sync.mapping</field>
        <field name="view_mode">list</field>
        <field name="context">{'search_default_mapped': 1}</field>
    </record>

    <!-- Sync Log Action -->
    <record id="action_fusion_sync_log" model="ir.actions.act_window">
        <field name="name">Sync Log</field>
        <field name="res_model">fusion.sync.log</field>
        <field name="view_mode">list</field>
    </record>

    <!-- Inter-Company Transfer Views -->
    <record id="view_inter_company_transfer_form" model="ir.ui.view">
        <field name="name">fusion.inter.company.transfer.form</field>
        <field name="model">fusion.inter.company.transfer</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_execute_transfer" type="object"
                            string="Execute Transfer (One-Click)" class="btn-primary"
                            invisible="state not in ('draft', 'requested')"
                            confirm="This will create remote SO, remote Invoice, local PO, and local Vendor Bill automatically. Continue?"/>
                    <button name="action_request" type="object"
                            string="Request (Manual)" class="btn-secondary"
                            invisible="state != 'draft'"/>
                    <button name="action_create_remote_so" type="object"
                            string="Create Remote SO" class="btn-primary"
                            invisible="state != 'requested'"/>
                    <button name="action_create_local_po" type="object"
                            string="Create Local PO" class="btn-primary"
                            invisible="state != 'so_created'"/>
                    <button name="action_create_invoice" type="object"
                            string="Create Invoice" class="btn-primary"
                            invisible="state != 'po_created'"/>
                    <button name="action_create_vendor_bill" type="object"
                            string="Create Vendor Bill" class="btn-secondary"
                            invisible="state != 'invoiced'"/>
                    <button name="action_create_delivery_task" type="object"
                            string="Create Delivery Task" class="btn-secondary"
                            invisible="state not in ('po_created', 'invoiced')"/>
                    <button name="action_mark_transferred" type="object"
                            string="Mark Transferred" class="btn-primary"
                            invisible="state != 'invoiced'"/>
                    <button name="action_complete" type="object"
                            string="Complete" class="btn-success"
                            invisible="state != 'transferred'"/>
                    <button name="action_retry" type="object"
                            string="Retry" class="btn-warning"
                            invisible="state != 'error'"/>
                    <button name="action_cancel" type="object"
                            string="Cancel" class="btn-secondary"
                            invisible="state in ('done', 'cancelled')"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,requested,so_created,po_created,invoiced,transferred,done"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="%(action_inter_company_transfer)d"
                                type="action" class="oe_stat_button"
                                icon="fa-shopping-cart"
                                invisible="not local_po_id">
                            <field name="local_po_id" widget="statinfo" string="Local PO"/>
                        </button>
                    </div>
                    <div class="alert alert-danger" role="alert"
                         invisible="state != 'error'">
                        <strong>Transfer failed at step: </strong>
                        <field name="error_step" readonly="1"/>
                    </div>
                    <group>
                        <group string="Transfer Details">
                            <field name="config_id"/>
                            <field name="product_id"/>
                            <field name="product_mapping_id" readonly="1"/>
                            <field name="quantity"/>
                            <field name="requested_by" readonly="1"/>
                        </group>
                        <group string="References">
                            <field name="remote_so_id" readonly="1"/>
                            <field name="remote_so_name" readonly="1"/>
                            <field name="local_po_id" readonly="1"/>
                            <field name="remote_invoice_id" readonly="1"/>
                            <field name="local_bill_id" readonly="1"/>
                            <field name="task_id" readonly="1"/>
                        </group>
                    </group>
                    <field name="notes" placeholder="Notes about this transfer..."/>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <record id="view_inter_company_transfer_list" model="ir.ui.view">
        <field name="name">fusion.inter.company.transfer.list</field>
        <field name="model">fusion.inter.company.transfer</field>
        <field name="arch" type="xml">
            <list>
                <field name="product_id"/>
                <field name="config_id"/>
                <field name="quantity"/>
                <field name="state" widget="badge"
                       decoration-info="state in ('draft', 'requested')"
                       decoration-primary="state in ('so_created', 'po_created')"
                       decoration-warning="state == 'invoiced'"
                       decoration-success="state in ('transferred', 'done')"
                       decoration-danger="state == 'cancelled'"/>
                <field name="local_po_id"/>
                <field name="requested_by"/>
                <field name="create_date"/>
            </list>
        </field>
    </record>

    <record id="action_inter_company_transfer" model="ir.actions.act_window">
        <field name="name">Inter-Company Transfers</field>
        <field name="res_model">fusion.inter.company.transfer</field>
        <field name="view_mode">list,form</field>
    </record>

    <!-- Warehouse Inventory Views -->
    <record id="view_warehouse_inventory_list" model="ir.ui.view">
        <field name="name">fusion.warehouse.inventory.list</field>
        <field name="model">fusion.warehouse.inventory</field>
        <field name="arch" type="xml">
            <list editable="bottom">
                <field name="product_id"/>
                <field name="lot_id"/>
                <field name="owner_config_id"/>
                <field name="quantity"/>
                <field name="location_bin"/>
                <field name="state" widget="badge"
                       decoration-success="state == 'available'"
                       decoration-warning="state == 'reserved'"
                       decoration-info="state == 'in_transit'"
                       decoration-muted="state == 'transferred'"/>
            </list>
        </field>
    </record>

    <record id="action_warehouse_inventory" model="ir.actions.act_window">
        <field name="name">Shared Warehouse Inventory</field>
        <field name="res_model">fusion.warehouse.inventory</field>
        <field name="view_mode">list</field>
    </record>

    <!-- Discrepancy Views -->
    <record id="view_inventory_discrepancy_list" model="ir.ui.view">
        <field name="name">fusion.inventory.discrepancy.list</field>
        <field name="model">fusion.inventory.discrepancy</field>
        <field name="arch" type="xml">
            <list>
                <field name="scan_date"/>
                <field name="product_id"/>
                <field name="discrepancy_type"/>
                <field name="missing_serials"/>
                <field name="expected_qty"/>
                <field name="actual_qty"/>
                <field name="difference"
                       decoration-danger="difference != 0"/>
                <field name="source"/>
                <field name="state" widget="badge"
                       decoration-danger="state == 'detected'"
                       decoration-warning="state == 'reviewed'"
                       decoration-success="state == 'resolved'"
                       decoration-muted="state == 'ignored'"/>
            </list>
        </field>
    </record>

    <record id="view_inventory_discrepancy_form" model="ir.ui.view">
        <field name="name">fusion.inventory.discrepancy.form</field>
        <field name="model">fusion.inventory.discrepancy</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_mark_reviewed" type="object"
                            string="Mark Reviewed" class="btn-primary"
                            invisible="state != 'detected'"/>
                    <button name="action_mark_resolved" type="object"
                            string="Resolve" class="btn-success"
                            invisible="state != 'reviewed'"/>
                    <button name="action_ignore" type="object"
                            string="Ignore" class="btn-secondary"
                            invisible="state in ('resolved', 'ignored')"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="detected,reviewed,resolved"/>
                </header>
                <sheet>
                    <group>
                        <group>
                            <field name="product_id"/>
                            <field name="discrepancy_type"/>
                            <field name="scan_date"/>
                            <field name="reviewed_by"/>
                        </group>
                        <group>
                            <field name="expected_qty"/>
                            <field name="actual_qty"/>
                            <field name="difference"/>
                            <field name="source"/>
                        </group>
                    </group>
                    <group string="Serial Numbers">
                        <field name="missing_serials" nolabel="1"/>
                    </group>
                    <group string="Resolution">
                        <field name="resolution_notes" nolabel="1"
                               placeholder="Describe how this was resolved..."/>
                    </group>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <record id="action_inventory_discrepancy" model="ir.actions.act_window">
        <field name="name">Discrepancies</field>
        <field name="res_model">fusion.inventory.discrepancy</field>
        <field name="view_mode">list,form</field>
        <field name="context">{'search_default_state': 'detected'}</field>
    </record>

</odoo>
