<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="dicotec" translate="label" sortOrder="200">
            <label>Dicotec</label>
        </tab>
        <section id="proyessync" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Proyes Order Sync</label>
            <tab>dicotec</tab>
            <resource>Dicotec_ProyesSync::config</resource>

            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General / API Connection</label>
                <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Order Sync</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="api_base_url" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>API Base URL</label>
                    <comment>Base URL of the Proyes/Musmar API, e.g. https://proyes.it/api/v1/musmar (do not add a trailing slash).</comment>
                    <depends><field id="enabled">1</field></depends>
                </field>
                <field id="api_token" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>API Bearer Token (JWT)</label>
                    <comment>Provided by Dicotec srl. Sent as "Authorization: Bearer &lt;token&gt;".</comment>
                    <depends><field id="enabled">1</field></depends>
                </field>
                <field id="timeout" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Request Timeout (seconds)</label>
                    <validate>validate-number validate-greater-than-zero</validate>
                    <depends><field id="enabled">1</field></depends>
                </field>
                <field id="log_enabled" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Debug Logging</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <comment>Writes request/response details to var/log/proyessync.log</comment>
                </field>
            </group>

            <group id="mapping" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Field &amp; Attribute Mapping</label>
                <field id="cod_art_attribute_code" translate="label" type="text" sortOrder="10" showInDefault="1">
                    <label>Product Attribute: cod_art</label>
                    <comment>Product attribute code holding the Proyes article code. Falls back to the product SKU if empty or not set on the product.</comment>
                </field>
                <field id="color_attribute_code" translate="label" type="text" sortOrder="20" showInDefault="1">
                    <label>Product Attribute: Colore (color code)</label>
                    <comment>Configurable attribute code used to determine the Proyes "colore" value on the child (simple) product.</comment>
                </field>
                <field id="size_attribute_code" translate="label" type="text" sortOrder="30" showInDefault="1">
                    <label>Product Attribute: Taglia (size code)</label>
                    <comment>Configurable attribute code used to determine the Proyes "taglia" value on the child (simple) product.</comment>
                </field>
                <field id="size_group_attribute_code" translate="label" type="text" sortOrder="40" showInDefault="1">
                    <label>Product Attribute: Gruppo Taglie</label>
                    <comment>Attribute code holding the Proyes size-group code. If not found on the product, the default value below is used.</comment>
                </field>
                <field id="default_gruppo" translate="label" type="text" sortOrder="50" showInDefault="1">
                    <label>Default Gruppo Taglie</label>
                </field>
                <field id="default_unknown_value" translate="label" type="text" sortOrder="55" showInDefault="1">
                    <label>Placeholder for Missing Required Text</label>
                    <comment>Used for nome/cognome/indirizzo/civico/cap/citta when Magento has no value, so required fields are never sent empty. Default: "sconosciuto".</comment>
                </field>
                <field id="default_provincia" translate="label" type="text" sortOrder="57" showInDefault="1">
                    <label>Default Provincia Code</label>
                    <comment>Used when the order address has no region/region_code (e.g. incomplete guest addresses).</comment>
                </field>
                <field id="cpc_attribute_code" translate="label" type="text" sortOrder="60" showInDefault="1">
                    <label>Customer Attribute: cpc</label>
                    <comment>Customer EAV attribute code storing the customer's existing Proyes "cpc" code (if this customer already exists on proyes.it). Leave the attribute value empty for new customers.</comment>
                </field>
                <field id="default_cod_catastale_nazione" translate="label" type="text" sortOrder="70" showInDefault="1">
                    <label>Default cod_catastale_nazione</label>
                    <comment>Used when the order's country is not Italy and no other mapping is configured. Default for Italy (IT) is always Z000.</comment>
                </field>
                <field id="shipping_method_map" translate="label" type="textarea" sortOrder="80" showInDefault="1">
                    <label>Shipping Method &#8594; vettore_cod Map</label>
                    <comment>One mapping per line, format: magento_shipping_method_code=proyes_vettore_cod (e.g. flatrate_flatrate=STD). Use GET /vettori.php to list valid Proyes codes. If a shipping method isn't listed here, the module automatically falls back to the first vettore returned by /vettori.php.</comment>
                </field>
                <field id="payment_method_map" translate="label" type="textarea" sortOrder="90" showInDefault="1">
                    <label>Payment Method &#8594; pagamento_cod Map</label>
                    <comment>One mapping per line, format: magento_payment_method_code=proyes_pagamento_cod (e.g. checkmo=CONTR). Use GET /pagamenti.php to list valid Proyes codes. If a payment method isn't listed here, the module tries keyword matching against the payment names from /pagamenti.php (e.g. "paypal" &#8594; a payment named "PayPal"), then falls back to the first payment method.</comment>
                </field>
            </group>
			<group id="cron" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Catch-up Cron (Hourly Resync)</label>
                <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1">
                    <label>Enable Catch-up Cron</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <comment>Runs on top of the checkout-time event and resends any order not yet marked "sent" — including orders the event observer never ran for at all (module disabled at checkout time, order created through a path that skips the event, etc.) and orders that previously failed. Requires "Enable Order Sync" above to also be Yes.</comment>
                </field>
                <field id="cron_expression" translate="label" type="text" sortOrder="20" showInDefault="1">
                    <label>Cron Schedule</label>
                    <comment>Standard cron expression. Default "0 * * * *" runs once every hour, on the hour.</comment>
                    <validate>validate-cron-string</validate>
                    <depends><field id="enabled">1</field></depends>
                </field>
                <field id="batch_size" translate="label" type="text" sortOrder="30" showInDefault="1">
                    <label>Max Orders per Run</label>
                    <comment>Caps how many candidate orders are processed in a single cron run, so each run stays quick even if a backlog builds up.</comment>
                    <validate>validate-number validate-greater-than-zero</validate>
                    <depends><field id="enabled">1</field></depends>
                </field>
                <field id="max_age_days" translate="label" type="text" sortOrder="40" showInDefault="1">
                    <label>Stop Retrying After (days)</label>
                    <comment>Orders older than this are no longer retried by the cron (0 = no limit). Prevents an order stuck failing indefinitely (e.g. permanently missing mapping) from being retried forever.</comment>
                    <validate>validate-number</validate>
                    <depends><field id="enabled">1</field></depends>
                </field>
            </group>
        </section>
    </system>
</config>
