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

        <!-- ================================================================
             CRON 1: Importación Automática de Pedidos (Shopify → Odoo)
             Activo solo cuando 'auto_import_orders' = True en la config.
             ================================================================ -->
        <record id="ir_cron_shopify_import_orders" model="ir.cron">
            <field name="name">Shopify: Importar Pedidos Nuevos</field>
            <field name="model_id" ref="model_shopify_config"/>
            <field name="state">code</field>
            <field name="code">model.cron_import_orders()</field>
            <field name="interval_number">5</field>
            <field name="interval_type">minutes</field>
            <field name="numbercall">-1</field>
            <field name="active">True</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">5</field>
        </record>

        <!-- ================================================================
             CRON 2: Sincronización de Inventario (Odoo ➔ Shopify)
             Odoo manda. Odoo es la Fuente de Verdad.
             Por defecto INACTIVO: se activa cuando estén listos.
             ================================================================ -->
        <record id="ir_cron_shopify_sync_inventory" model="ir.cron">
            <field name="name">Shopify: Sincronizar Inventario (Odoo ➔ Shopify)</field>
            <field name="model_id" ref="model_shopify_config"/>
            <field name="state">code</field>
            <field name="code">model.cron_sync_inventory_to_shopify()</field>
            <field name="interval_number">12</field>
            <field name="interval_type">hours</field>
            <field name="numbercall">-1</field>
            <field name="active">False</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">10</field>
        </record>

        <!-- ================================================================
             CRON 3: Renovación Preventiva de Tokens (Shopify OAuth)
             Shopify emite tokens con duración de 24h. Este cron los renueva
             automáticamente cada 20 horas para evitar interrupciones de servicio
             sin intervención manual del cliente.
             ================================================================ -->
        <record id="ir_cron_shopify_refresh_tokens" model="ir.cron">
            <field name="name">Shopify: Renovar Tokens de Acceso (Automático)</field>
            <field name="model_id" ref="model_shopify_config"/>
            <field name="state">code</field>
            <field name="code">model.cron_refresh_tokens()</field>
            <field name="interval_number">20</field>
            <field name="interval_type">hours</field>
            <field name="numbercall">-1</field>
            <field name="active">True</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">1</field>
        </record>

        <!-- ================================================================
             CRON 4: Descarga de Imágenes por Lotes (Shopify → Odoo)
             Procesa 15 productos sin imagen por ejecución.
             Se ACTIVA desde el botón y se auto-DESACTIVA al terminar.
             ================================================================ -->
        <record id="ir_cron_shopify_download_images" model="ir.cron">
            <field name="name">Shopify: Descargar Imágenes de Productos</field>
            <field name="model_id" ref="model_shopify_config"/>
            <field name="state">code</field>
            <field name="code">model._cron_download_images_batch()</field>
            <field name="interval_number">1</field>
            <field name="interval_type">minutes</field>
            <field name="numbercall">-1</field>
            <field name="active">False</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">5</field>
        </record>

        <!-- ================================================================
             CRON 5: Sincronización Permanente y Automática de Nuevas Imágenes
             Busca productos sin foto en Odoo, descarga la portada y NUNCA se desactiva.
             Se ejecuta cada 2 horas de forma 100% transparente para el usuario.
             ================================================================ -->
        <record id="ir_cron_shopify_auto_image_sync" model="ir.cron">
            <field name="name">Shopify: Auto-Descargar Imágenes Nuevas (Permanente)</field>
            <field name="model_id" ref="model_shopify_config"/>
            <field name="state">code</field>
            <field name="code">model.cron_auto_download_images()</field>
            <field name="interval_number">2</field>
            <field name="interval_type">hours</field>
            <field name="numbercall">-1</field>
            <field name="active">True</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">8</field>
        </record>

        <!-- ================================================================
             CRON 6: Procesar Cola de Sincronización de Inventario (Shopify Stock Queue)
             ================================================================ -->
        <record id="ir_cron_shopify_process_stock_queue" model="ir.cron">
            <field name="name">Shopify: Procesar Cola de Inventario</field>
            <field name="model_id" ref="model_shopify_stock_queue"/>
            <field name="state">code</field>
            <field name="code">model.process_queue_batch()</field>
            <field name="interval_number">5</field>
            <field name="interval_type">minutes</field>
            <field name="numbercall">-1</field>
            <field name="active">True</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">5</field>
        </record>

        <!-- ================================================================
             CRON 7: Limpiar Cola de Inventario (Shopify Stock Queue)
             ================================================================ -->
        <record id="ir_cron_shopify_clean_stock_queue" model="ir.cron">
            <field name="name">Shopify: Limpiar Cola de Inventario</field>
            <field name="model_id" ref="model_shopify_stock_queue"/>
            <field name="state">code</field>
            <field name="code">model.clean_done_queue()</field>
            <field name="interval_number">1</field>
            <field name="interval_type">days</field>
            <field name="numbercall">-1</field>
            <field name="active">True</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="priority">15</field>
        </record>

    </data>
</odoo>
