<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <!-- ===================================================================
         EXTENSIÓN: Vista de Pedido de Venta — Integración Shopify
         Agrega botón de fulfillment e indicadores de estado en sale.order
    =================================================================== -->
    <record id="sale_order_shopify_fulfillment_view" model="ir.ui.view">
        <field name="name">sale.order.shopify.fulfillment.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">

            <!-- ── Botón de fulfillment en el header del pedido ── -->
            <xpath expr="//div[@name='button_box']" position="inside">
                <button name="action_notify_shopify_fulfillment"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-truck"
                        groups="hazard_shopify.group_hazard_warehouse,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance"
                        attrs="{'invisible': [('shopify_order_id', '=', False)]}"
                        help="Notificar a Shopify que este pedido ha sido enviado">
                    <div class="o_field_widget o_stat_info">
                        <span class="o_stat_value">
                            <field name="shopify_fulfillment_status"
                                   widget="badge"
                                   decoration-success="shopify_fulfillment_status == 'fulfilled'"
                                   decoration-warning="shopify_fulfillment_status == 'partial'"
                                   decoration-danger="shopify_fulfillment_status == 'error'"
                                   decoration-muted="not shopify_fulfillment_status"/>
                        </span>
                        <span class="o_stat_text">Shopify</span>
                    </div>
                </button>
                <button name="action_view_shopify_refunds"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-undo"
                        groups="hazard_shopify.group_hazard_warehouse,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance"
                        attrs="{'invisible': ['|', ('shopify_order_id', '=', False), ('shopify_refund_status', '=', 'no_refund')]}"
                        help="Estado de Reembolsos de Shopify">
                    <div class="o_field_widget o_stat_info">
                        <span class="o_stat_value">
                            <field name="shopify_refund_status"
                                   widget="badge"
                                   decoration-danger="shopify_refund_status == 'refunded'"
                                   decoration-warning="shopify_refund_status == 'partially_refunded'"/>
                        </span>
                        <span class="o_stat_text">Reembolso</span>
                    </div>
                </button>
            </xpath>

            <!-- ── Campos de Shopify en pestaña "Otra información" ── -->
            <xpath expr="//page[@name='other_information']" position="inside">
                <group string="🛒 Shopify" 
                       groups="hazard_shopify.group_hazard_warehouse,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance"
                       attrs="{'invisible': [('shopify_order_id', '=', False)]}">
                    <group>
                        <field name="shopify_order_id" readonly="1" string="ID Orden Shopify"/>
                        <field name="shopify_order_number" readonly="1" string="# Orden Shopify"/>
                    </group>
                    <group>
                        <field name="shopify_fulfillment_status" readonly="1" string="Estado Fulfillment"/>
                        <field name="shopify_fulfillment_id" readonly="1" string="ID Fulfillment Shopify"
                               attrs="{'invisible': [('shopify_fulfillment_id', '=', False)]}"/>
                    </group>
                    <!-- ── Campos de reembolsos y devoluciones ── -->
                    <group string="🔄 Reembolsos y Devoluciones" col="2">
                        <field name="shopify_financial_status" readonly="1" string="Estado Financiero"/>
                        <field name="shopify_refund_status" readonly="1" string="Estado Reembolso"/>
                        <field name="shopify_refund_amount" readonly="1" string="Monto Reembolsado" widget="monetary"/>
                        <field name="shopify_refund_ids" readonly="1" string="IDs Reembolsos" attrs="{'invisible': [('shopify_refund_ids', '=', False)]}"/>
                    </group>
                    <!-- ── Campos de guía de envío ── -->
                    <group string="📦 Datos de Envío" col="2">
                        <field name="shopify_shipping_title" readonly="1" string="Método Envío Shopify"/>
                        <field name="shopify_carrier_name"
                               string="Paquetería"
                               attrs="{'readonly': [('shopify_fulfillment_status', '=', 'fulfilled')]}"/>
                        <field name="shopify_tracking_number"
                               string="Número de Guía"
                               placeholder="Ej: 1234567890"
                               attrs="{'readonly': [('shopify_fulfillment_status', '=', 'fulfilled')]}"/>
                    </group>
                    <div class="col-12 mt-1" attrs="{'invisible': [('shopify_fulfillment_status', '!=', False)]}">
                        <button name="action_notify_shopify_fulfillment"
                                type="object"
                                string="📦 Marcar como Enviado en Shopify"
                                class="btn btn-primary"
                                groups="hazard_shopify.group_hazard_warehouse,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance"
                                attrs="{'invisible': [('shopify_order_id', '=', False)]}"/>
                        <span class="text-muted ml-2" style="font-size:0.85em;">
                            Solo disponible cuando haya entregas validadas en Odoo.
                        </span>
                    </div>
                </group>
            </xpath>

        </field>
    </record>

    <!-- ===================================================================
         EXTENSIÓN: Vista de Árbol/Lista de Pedidos y Cotizaciones
         Agrega la columna con el número de pedido de Shopify directamente
         en la tabla principal.
    =================================================================== -->
    <record id="view_quotation_tree_shopify" model="ir.ui.view">
        <field name="name">sale.quotation.tree.shopify.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_quotation_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='name']" position="after">
                <field name="shopify_order_number" string="# Shopify" optional="show"/>
                <field name="shopify_financial_status" string="Pago (Shopify)" optional="show" widget="badge" 
                       decoration-success="shopify_financial_status == 'paid'" 
                       decoration-warning="shopify_financial_status in ('pending', 'partially_paid')"
                       decoration-danger="shopify_financial_status in ('refunded', 'voided')"/>
                <field name="shopify_refund_status" string="Reembolso (Shopify)" optional="show" widget="badge"
                       decoration-danger="shopify_refund_status == 'refunded'"
                       decoration-warning="shopify_refund_status == 'partially_refunded'"
                       decoration-muted="shopify_refund_status in ('no_refund', False)"/>
                <field name="shopify_fulfillment_status" string="Envío (Shopify)" optional="show" widget="badge" 
                       decoration-success="shopify_fulfillment_status == 'fulfilled'" 
                       decoration-warning="shopify_fulfillment_status == 'partial'" 
                       decoration-info="shopify_fulfillment_status == 'pending'"/>
            </xpath>
        </field>
    </record>

    <record id="view_order_tree_shopify" model="ir.ui.view">
        <field name="name">sale.order.tree.shopify.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='name']" position="after">
                <field name="shopify_order_number" string="# Shopify" optional="show"/>
                <field name="shopify_financial_status" string="Pago (Shopify)" optional="show" widget="badge" 
                       decoration-success="shopify_financial_status == 'paid'" 
                       decoration-warning="shopify_financial_status in ('pending', 'partially_paid')"
                       decoration-danger="shopify_financial_status in ('refunded', 'voided')"/>
                <field name="shopify_refund_status" string="Reembolso (Shopify)" optional="show" widget="badge"
                       decoration-danger="shopify_refund_status == 'refunded'"
                       decoration-warning="shopify_refund_status == 'partially_refunded'"
                       decoration-muted="shopify_refund_status in ('no_refund', False)"/>
                <field name="shopify_fulfillment_status" string="Envío (Shopify)" optional="show" widget="badge" 
                       decoration-success="shopify_fulfillment_status == 'fulfilled'" 
                       decoration-warning="shopify_fulfillment_status == 'partial'" 
                       decoration-info="shopify_fulfillment_status == 'pending'"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================
         EXTENSIÓN: Vista de Árbol/Lista de Órdenes de Entrega (Stock Picking)
         Agrega la columna con el número de pedido de Shopify directamente
         en la tabla de albaranes de almacén.
    =================================================================== -->
    <record id="view_picking_tree_shopify" model="ir.ui.view">
        <field name="name">stock.picking.tree.shopify.inherit</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.vpicktree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='origin']" position="after">
                <field name="shopify_order_number" string="# Shopify" optional="show"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================
         EXTENSIÓN: Vista de Formulario de Albarán (Stock Picking)
         Agrega los badges visuales muy obvios para Bodega sobre el tipo de envío.
    =================================================================== -->
    <record id="view_picking_form_shopify" model="ir.ui.view">
        <field name="name">stock.picking.form.shopify.inherit</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.view_picking_form"/>
        <field name="arch" type="xml">
            <xpath expr="//header" position="inside">
                <button name="action_shopify_ready_for_pickup" 
                        string="Notificar Listo para Retiro" 
                        type="object" 
                        class="oe_highlight btn-primary" 
                        attrs="{'invisible': ['|', '|', '|', ('shopify_order_number', '=', False), ('shopify_delivery_type', '!=', 'pickup'), ('state', '!=', 'assigned'), ('shopify_ready_for_pickup_notified', '=', True)]}"/>
            </xpath>

            <xpath expr="//header" position="after">
                <!-- Banner de advertencia visual interactivo para Almacén -->
                <div class="alert alert-info text-center o_form_header" 
                     role="alert" 
                     style="margin-bottom:0px; font-weight:bold; font-size:1.1em; padding:8px 5px;"
                     attrs="{'invisible': ['|', ('shopify_order_number', '=', False), ('shopify_delivery_type', '!=', 'pickup')]}">
                    🔵 <strong>[ RETIRO EN OFICINA ]</strong> Este pedido de Shopify se entregará en mano. NO requiere Número de Guía.
                </div>
                <div class="alert alert-success text-center o_form_header" 
                     role="alert" 
                     style="margin-bottom:0px; font-weight:bold; font-size:1.1em; padding:8px 5px;"
                     attrs="{'invisible': ['|', ('shopify_order_number', '=', False), ('shopify_delivery_type', '!=', 'delivery')]}">
                    🚚 <strong>[ ENVÍO CON GUÍA ]</strong> Este pedido se envía por paquetería. <strong>Requiere Número de Guía obligatorio.</strong>
                </div>
            </xpath>

            <xpath expr="//field[@name='partner_id']" position="after">
                <field name="shopify_delivery_address" 
                       string="Dirección Completa" 
                       attrs="{'invisible': [('shopify_order_number', '=', False)]}" 
                       readonly="1"/>
                <field name="shopify_delivery_phone" 
                       string="Teléfono" 
                       attrs="{'invisible': ['|', ('shopify_order_number', '=', False), ('shopify_delivery_phone', '=', False)]}" 
                       readonly="1"/>
                <field name="shopify_delivery_mobile" 
                       string="Celular" 
                       attrs="{'invisible': ['|', ('shopify_order_number', '=', False), ('shopify_delivery_mobile', '=', False)]}" 
                       readonly="1"/>
            </xpath>

            <xpath expr="//field[@name='origin']" position="after">
                <field name="shopify_order_number" readonly="1" string="# Shopify" attrs="{'invisible': [('shopify_order_number', '=', False)]}"/>
                <field name="shopify_shipping_title" readonly="1" string="Método Envío Shopify" attrs="{'invisible': [('shopify_order_number', '=', False)]}"/>
                <field name="shopify_delivery_type" invisible="1"/>
                <field name="shopify_ready_for_pickup_notified" invisible="1"/>
            </xpath>
        </field>
    </record>

</odoo>
