<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <!-- ===================================================================
         EXTENSIÓN: Vista de Producto — Integración Shopify
         Agrega panel de publicación en Shopify y controles de sincronización.
         =================================================================== -->
    <record id="product_template_hazard_shopify_view" model="ir.ui.view">
        <field name="name">product.template.hazard.shopify.inherit</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view"/>
        <field name="arch" type="xml">

            <!-- ── Botón de estado Shopify en el header (stat button) ── -->
            <xpath expr="//div[@name='button_box']" position="inside">
                <button name="action_push_to_shopify"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-shopping-bag"
                        groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_finance"
                        attrs="{'invisible': [('shopify_published', '=', True)]}"
                        help="Publicar este producto en Shopify">
                    <div class="o_field_widget o_stat_info">
                        <span class="o_stat_value" style="font-size:12px;">Publicar</span>
                        <span class="o_stat_text">Shopify</span>
                    </div>
                </button>

                <button name="action_push_to_shopify"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-check-circle"
                        groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_finance"
                        attrs="{'invisible': [('shopify_published', '=', False)]}"
                        help="Sincronizar cambios con Shopify">
                    <div class="o_field_widget o_stat_info">
                        <span class="o_stat_value">
                            <field name="shopify_sync_status"
                                   widget="badge"
                                   decoration-success="shopify_sync_status == 'synced'"
                                   decoration-warning="shopify_sync_status == 'pending'"
                                   decoration-danger="shopify_sync_status == 'error'"/>
                        </span>
                        <span class="o_stat_text">Shopify</span>
                    </div>
                </button>
            </xpath>

            <!-- ── Panel de Shopify en pestaña "Información general" ── -->
            <xpath expr="//page[@name='general_information']" position="after">
                <page string="🛒 Shopify"
                      name="shopify_info"
                      groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_finance">
                    <group>
                        <group string="Estado de Publicación">
                            <field name="shopify_published"
                                   string="¿Publicado en Shopify?"
                                   widget="toggle_button"
                                   readonly="1"/>
                            <field name="shopify_status"
                                   string="Estado en Shopify"
                                   widget="selection"/>
                            <field name="shopify_sync_status"
                                   string="Estado de sincronización"
                                   readonly="1"/>
                            <field name="shopify_synced_at"
                                   string="Primera sincronización"
                                   readonly="1"/>
                            <field name="shopify_last_pushed_at"
                                   string="Última actualización a Shopify"
                                   readonly="1"/>
                        </group>
                        <group string="Información Shopify">
                            <field name="shopify_config_id"
                                   string="Tienda Shopify"
                                   domain="[('state','=','confirmed')]"
                                   options="{'no_create': True}"/>
                            <field name="shopify_id"
                                   string="ID Producto Shopify"
                                   readonly="1"
                                   attrs="{'invisible': [('shopify_id', '=', False)]}"/>
                        </group>
                    </group>

                    <!-- Canales de Venta — ancho completo -->
                    <separator string="Canales de Venta Shopify"
                               attrs="{'invisible': [('shopify_published', '=', False)]}"/>
                    <div class="mb-2" attrs="{'invisible': [('shopify_published', '=', False)]}">
                        <button name="action_refresh_shopify_channels"
                                type="object"
                                string="🔄 Leer canales desde Shopify"
                                class="btn btn-sm btn-info"
                                groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_finance"
                                help="Actualiza esta lista leyendo el estado real desde Shopify sin realizar cambios."/>
                    </div>
                    <field name="shopify_publication_ids"
                           nolabel="1"
                           attrs="{'invisible': [('shopify_published', '=', False)]}">
                        <tree editable="bottom" create="false" delete="false">
                            <field name="publication_name" string="Canal de Venta" readonly="1"/>
                            <field name="is_published" string="Publicado" widget="boolean_toggle"/>
                            <field name="publication_id" invisible="1"/>
                        </tree>
                    </field>

                    <!-- Error de sync -->
                    <group attrs="{'invisible': [('shopify_sync_error', '=', False)]}">
                        <div class="alert alert-danger" role="alert" style="margin:8px 0;">
                            <strong>⚠️ Último error de sincronización:</strong>
                            <field name="shopify_sync_error" readonly="1" nolabel="1"/>
                        </div>
                    </group>

                    <!-- Botones de acción manuales -->
                    <div class="o_field_widget mt-3">
                        <button name="action_push_to_shopify"
                                type="object"
                                string="📤 Publicar / Actualizar en Shopify"
                                class="btn btn-primary me-2"
                                groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_finance"/>
                        <button name="action_unpublish_from_shopify"
                                type="object"
                                string="🚫 Despublicar de Shopify"
                                class="btn btn-secondary"
                                attrs="{'invisible': [('shopify_published', '=', False)]}"
                                groups="hazard_shopify.group_hazard_finance"
                                confirm="¿Seguro que quieres ocultar este producto de la tienda Shopify? No se eliminará, solo quedará como borrador."/>
                    </div>

                    <!-- Instrucciones para el equipo de diseño -->
                    <div class="alert alert-info mt-3" role="alert"
                         attrs="{'invisible': [('shopify_published', '=', True)]}">
                        <strong>ℹ️ ¿Cómo publicar este producto?</strong>
                        <ol style="margin-top:6px; margin-bottom:0;">
                            <li>Completa el nombre, descripción, precio y variantes del producto.</li>
                            <li>Sube la imagen principal del producto.</li>
                            <li>Presiona el botón <strong>"📤 Publicar / Actualizar en Shopify"</strong>.</li>
                            <li>El producto aparecerá automáticamente en la tienda de Shopify.</li>
                        </ol>
                    </div>
                </page>
            </xpath>

            <!-- ── Restricciones de precios (ya existían, las mantenemos) ── -->
            <xpath expr="//field[@name='list_price']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='standard_price']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='taxes_id']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='supplier_taxes_id']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='default_code']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='barcode']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='barcode']" position="after">
                <button name="action_generate_barcode" string="Generar EAN-13" type="object" class="btn btn-sm btn-link" icon="fa-barcode" attrs="{'invisible': [('barcode', '!=', False)]}" groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance"/>
            </xpath>

        </field>
    </record>

    <!-- EXTENSIÓN: Vista de Variante de Producto — Restricción de Precios para Bodega/Diseñador -->
    <record id="product_product_hazard_shopify_view" model="ir.ui.view">
        <field name="name">product.product.hazard.shopify.inherit</field>
        <field name="model">product.product</field>
        <field name="inherit_id" ref="product.product_normal_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='lst_price']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='standard_price']" position="attributes">
                <attribute name="groups">hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance</attribute>
            </xpath>
            <xpath expr="//field[@name='barcode']" position="after">
                <button name="action_generate_barcode" string="Generar EAN-13" type="object" class="btn btn-sm btn-link" icon="fa-barcode" attrs="{'invisible': [('barcode', '!=', False)]}" groups="hazard_shopify.group_hazard_designer,hazard_shopify.group_hazard_sales,hazard_shopify.group_hazard_finance"/>
            </xpath>
        </field>
    </record>

</odoo>
