<?php
/**
 * WooCommerce Settings tab for Factur-X configuration.
 *
 * @package FacturX_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

class GOFACTOX_Settings {

    public static function init(): void {
        add_filter( 'woocommerce_settings_tabs_array', [ __CLASS__, 'add_settings_tab' ], 50 );
        add_action( 'woocommerce_settings_tabs_facturx', [ __CLASS__, 'output_settings' ] );
        add_action( 'woocommerce_update_options_facturx', [ __CLASS__, 'save_settings' ] );

        // Custom field type: media (image) picker backed by an attachment ID.
        // WC's save_fields() doesn't know our custom type, so we persist it manually.
        add_action( 'woocommerce_admin_field_gofactox_logo', [ __CLASS__, 'render_logo_field' ] );
        add_action( 'woocommerce_update_options_facturx', [ __CLASS__, 'save_logo_field' ] );
        add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_media_assets' ] );
    }

    /**
     * Persist the gofactox_company_logo option from the custom field.
     */
    public static function save_logo_field(): void {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- woocommerce_update_options runs after WC's own nonce check on the settings form.
        if ( isset( $_POST['gofactox_company_logo'] ) ) {
            // phpcs:ignore WordPress.Security.NonceVerification.Missing
            $raw = sanitize_text_field( wp_unslash( $_POST['gofactox_company_logo'] ) );
            update_option( 'gofactox_company_logo', (int) $raw );
        }
    }

    /**
     * Enqueue WP media library JS on the Factur-X settings page only.
     */
    public static function enqueue_media_assets( string $hook ): void {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if ( ! isset( $_GET['tab'] ) || $_GET['tab'] !== 'facturx' ) {
            return;
        }
        wp_enqueue_media();
    }

    /**
     * Render the custom "gofactox_logo" field (media picker + preview).
     *
     * Uses jQuery + event delegation so the picker keeps working even when WP
     * reorders the DOM and `wp.media` finishes loading after this markup is printed.
     */
    public static function render_logo_field( array $value ): void {
        $option_id   = $value['id'];
        $attachment_id = (int) get_option( $option_id, 0 );
        $preview_url   = $attachment_id ? wp_get_attachment_image_url( $attachment_id, 'medium' ) : '';
        ?>
        <tr valign="top">
            <th scope="row" class="titledesc">
                <label for="<?php echo esc_attr( $option_id ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
            </th>
            <td class="forminp">
                <div class="gofactox-logo-field">
                    <div class="gofactox-logo-preview" style="margin-bottom:6px; min-height:40px;">
                        <?php if ( $preview_url ) : ?>
                            <img src="<?php echo esc_url( $preview_url ); ?>" style="max-height:80px; max-width:200px; display:block;" alt="">
                        <?php endif; ?>
                    </div>
                    <input type="hidden" name="<?php echo esc_attr( $option_id ); ?>" id="<?php echo esc_attr( $option_id ); ?>" class="gofactox-logo-id" value="<?php echo esc_attr( $attachment_id ?: '' ); ?>">
                    <button type="button" class="button gofactox-logo-pick"><?php esc_html_e( 'Upload / Choose logo', 'gofactox-e-invoicing-for-woocommerce' ); ?></button>
                    <button type="button" class="button gofactox-logo-clear" <?php echo $attachment_id ? '' : 'style="display:none;"'; ?>><?php esc_html_e( 'Remove', 'gofactox-e-invoicing-for-woocommerce' ); ?></button>
                    <p class="description"><?php echo wp_kses_post( $value['desc'] ?? '' ); ?></p>
                </div>
            </td>
        </tr>
        <?php
        // Print the binding script once per page load (idempotent guard).
        static $printed = false;
        if ( $printed ) {
            return;
        }
        $printed = true;
        $title = esc_js( __( 'Choose company logo', 'gofactox-e-invoicing-for-woocommerce' ) );
        ?>
        <script>
        jQuery(function($){
            var frames = {};
            // Force-enable WooCommerce's "smart" save button once our hidden input changes.
            // WC's React save button watches visible inputs and doesn't always pick up
            // programmatic changes on type=hidden, so we unblock it explicitly.
            function markFormDirty(){
                $('button[name="save"]').prop('disabled', false).removeAttr('disabled');
            }
            $(document).on('click', '.gofactox-logo-pick', function(e){
                e.preventDefault();
                if (typeof wp === 'undefined' || !wp.media) {
                    alert('WordPress media library not loaded. Reload the page and try again.');
                    return;
                }
                var $field = $(this).closest('.gofactox-logo-field');
                var key    = $field.find('.gofactox-logo-id').attr('id');
                if (!frames[key]) {
                    frames[key] = wp.media({
                        title: '<?php echo $title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- already escaped via esc_js() above ?>',
                        library: { type: 'image' },
                        multiple: false,
                        button: { text: '<?php echo $title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- already escaped via esc_js() above ?>' }
                    });
                    frames[key].on('select', function(){
                        var att = frames[key].state().get('selection').first().toJSON();
                        // Triggering change tells WC's save-button watcher that the form is dirty.
                        $field.find('.gofactox-logo-id').val(att.id).trigger('change').trigger('input');
                        $field.find('.gofactox-logo-preview').html('<img src="'+att.url+'" style="max-height:80px;max-width:200px;display:block;" alt="">');
                        $field.find('.gofactox-logo-clear').show();
                        markFormDirty();
                    });
                }
                frames[key].open();
            });
            $(document).on('click', '.gofactox-logo-clear', function(e){
                e.preventDefault();
                var $field = $(this).closest('.gofactox-logo-field');
                $field.find('.gofactox-logo-id').val('').trigger('change').trigger('input');
                $field.find('.gofactox-logo-preview').empty();
                $(this).hide();
                markFormDirty();
            });
        });
        </script>
        <?php
    }

    /**
     * Add the Factur-X tab to WooCommerce settings.
     */
    public static function add_settings_tab( array $tabs ): array {
        $tabs['facturx'] = __( 'Factur-X', 'gofactox-e-invoicing-for-woocommerce' );
        return $tabs;
    }

    /**
     * Output the settings fields.
     */
    public static function output_settings(): void {
        woocommerce_admin_fields( self::get_settings() );
    }

    /**
     * Save the settings.
     */
    public static function save_settings(): void {
        woocommerce_update_options( self::get_settings() );
    }

    /**
     * Define all settings fields.
     */
    private static function get_settings(): array {
        return [
            // --- Company Information ---
            [
                'title' => __( 'Company Information', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'desc'  => __( 'These details appear on your invoices and in the Factur-X XML.', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_company_section',
            ],
            [
                'title' => __( 'Company Logo', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_company_logo',
                'type'  => 'gofactox_logo',
                'desc'  => __( 'Displayed in the header of every invoice and credit note PDF. Recommended size: 300×100 px, PNG or JPG.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'    => __( 'Company Name', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_company_name',
                'type'     => 'text',
                'css'      => 'min-width: 400px;',
                'desc_tip' => __( 'Legal company name (dénomination sociale).', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'    => __( 'Legal Form', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_legal_form',
                'type'     => 'text',
                'desc_tip' => __( 'e.g., SAS, SARL, EURL, EI, Auto-entrepreneur', 'gofactox-e-invoicing-for-woocommerce' ),
                'placeholder' => 'SAS',
            ],
            [
                'title'       => __( 'Share Capital', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_share_capital',
                'type'        => 'text',
                'desc_tip'    => __( 'Capital social, e.g., "10 000 €". Leave empty for sole proprietors.', 'gofactox-e-invoicing-for-woocommerce' ),
                'placeholder' => '10 000 €',
            ],
            [
                'title' => __( 'Address Line 1', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_address_line1',
                'type'  => 'text',
                'css'   => 'min-width: 400px;',
            ],
            [
                'title' => __( 'Address Line 2', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_address_line2',
                'type'  => 'text',
                'css'   => 'min-width: 400px;',
            ],
            [
                'title' => __( 'Postal Code', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_postal_code',
                'type'  => 'text',
            ],
            [
                'title' => __( 'City', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_city',
                'type'  => 'text',
            ],
            [
                'title'   => __( 'Country', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_country_code',
                'type'    => 'select',
                'options' => WC()->countries ? WC()->countries->get_countries() : [ 'FR' => 'France' ],
                'default' => 'FR',
            ],
            [
                'title'    => __( 'SIRET', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_siret',
                'type'     => 'text',
                'desc_tip' => __( '14-digit SIRET number.', 'gofactox-e-invoicing-for-woocommerce' ),
                'placeholder' => '12345678901234',
                'custom_attributes' => [ 'maxlength' => '14', 'pattern' => '[0-9]{14}' ],
            ],
            [
                'title'    => __( 'VAT Number', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_vat_number',
                'type'     => 'text',
                'desc_tip' => __( 'Intra-community VAT number, e.g., FR12345678901', 'gofactox-e-invoicing-for-woocommerce' ),
                'placeholder' => 'FR12345678901',
            ],
            [
                'title'       => __( 'RCS City', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_rcs_city',
                'type'        => 'text',
                'desc_tip'    => __( 'City of the trade registry (RCS). Leave empty for auto-entrepreneurs.', 'gofactox-e-invoicing-for-woocommerce' ),
                'placeholder' => 'Paris',
            ],
            [
                'title'    => __( 'VAT Exempt (Article 293 B)', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_vat_exempt',
                'type'     => 'checkbox',
                'desc'     => __( 'Check if you are exempt from VAT (franchise en base de TVA).', 'gofactox-e-invoicing-for-woocommerce' ),
                'default'  => 'no',
            ],
            [
                'title'    => __( 'VAT paid on debits', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_vat_on_debits',
                'type'     => 'checkbox',
                'desc'     => __( 'Check if you have opted to pay VAT on debits (option pour le paiement de la TVA d\'après les débits). Adds the corresponding legal mention to invoices. Leave unchecked if you pay VAT on collection (encaissements), the default for most service providers.', 'gofactox-e-invoicing-for-woocommerce' ),
                'default'  => 'no',
            ],
            [
                'title'       => __( 'Electronic Address (e-invoicing email)', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_seller_email',
                'type'        => 'email',
                'css'         => 'min-width: 400px;',
                'placeholder' => 'invoicing@your-company.com',
                'desc_tip'    => __( 'Your professional email used by PDPs to send invoice receipt acknowledgments and rejection notices. Required by some platforms (EN 16931 BT-34).', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'       => __( 'IBAN', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_iban',
                'type'        => 'text',
                'css'         => 'min-width: 400px; font-family: monospace;',
                'placeholder' => 'FR76 1234 5678 9012 3456 7890 123',
                'desc_tip'    => __( 'Your bank IBAN. Displayed on invoices to enable wire transfers and included in the Factur-X XML (BG-17).', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'       => __( 'BIC / SWIFT', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_bic',
                'type'        => 'text',
                'placeholder' => 'BNPAFRPP',
                'desc_tip'    => __( 'Your bank BIC/SWIFT code. Optional but recommended for international wire transfers.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'    => __( 'Bank Name', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_bank_name',
                'type'     => 'text',
                'desc_tip' => __( 'Name of your bank. Displayed alongside the IBAN on invoices.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'   => __( 'SEPA QR code (scan to pay) — Pro', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_sepa_qr',
                'type'    => 'checkbox',
                'desc'    => __( 'Add a SEPA EPC QR code next to the IBAN on invoices (Pro feature). Customers scan it with their banking app and the wire transfer is pre-filled (amount, IBAN, invoice number). Only generated on EUR invoices.', 'gofactox-e-invoicing-for-woocommerce' ),
                'default' => 'no',
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_company_section',
            ],

            // --- Invoice Settings ---
            [
                'title' => __( 'Invoice Settings', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'id'    => 'gofactox_invoice_section',
            ],
            [
                'title'    => __( 'Number Prefix', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_invoice_prefix',
                'type'     => 'text',
                'default'  => 'F-',
                'desc_tip' => __( 'Prefix for invoice numbers. Year is added automatically.', 'gofactox-e-invoicing-for-woocommerce' ),
                'desc'     => sprintf(
                    /* translators: %s: next invoice number preview */
                    __( 'Next invoice: %s', 'gofactox-e-invoicing-for-woocommerce' ),
                    '<strong>' . GOFACTOX_Numbering::preview_next() . '</strong>'
                ),
            ],
            [
                'title'             => __( 'Number Digits', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'                => 'gofactox_invoice_digits',
                'type'              => 'number',
                'default'           => 4,
                'custom_attributes' => [ 'min' => '3', 'max' => '8' ],
                'desc_tip'          => __( 'Number of digits for the sequential part (e.g., 4 = 0001).', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'             => __( 'Next Invoice Number', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'                => 'gofactox_next_invoice_number',
                'type'              => 'number',
                'default'           => 1,
                'custom_attributes' => [ 'min' => '1' ],
                'desc_tip'          => __( 'Warning: changing this may create gaps in your numbering sequence.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'   => __( 'Output Format', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_output_format',
                'type'    => 'select',
                'options' => [
                    'facturx'         => 'Factur-X Basic (PDF/A-3 + XML)',
                    'facturx-comfort' => 'Factur-X Comfort / EN 16931 (PDF/A-3 + XML, structured discounts and references)',
                    'cii'             => 'CII (XML only, EN 16931)',
                    'ubl'             => 'UBL (XML only, EN 16931)',
                ],
                'default' => 'facturx',
                'desc_tip' => __( 'Factur-X Comfort exposes structured fields (allowances, charges, references) for sophisticated B2B buyers.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'   => __( 'Operation Category', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_operation_category',
                'type'    => 'select',
                'options' => [
                    'auto'     => __( 'Auto-detect from products (recommended)', 'gofactox-e-invoicing-for-woocommerce' ),
                    'goods'    => __( 'Supply of goods (livraison de biens)', 'gofactox-e-invoicing-for-woocommerce' ),
                    'services' => __( 'Supply of services (prestation de services)', 'gofactox-e-invoicing-for-woocommerce' ),
                    'mixed'    => __( 'Mixed (mixte)', 'gofactox-e-invoicing-for-woocommerce' ),
                ],
                'default'  => 'auto',
                'desc_tip' => __( 'New mandatory mention of the French e-invoicing reform. Auto-detect treats virtual/downloadable products as services and shippable products as goods; a mixed order is reported as "mixte".', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'   => __( 'Cadre de facturation (Chorus Pro)', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_cadre_facturation',
                'type'    => 'select',
                'options' => [
                    'A1'  => __( 'A1 - Standard supplier invoice', 'gofactox-e-invoicing-for-woocommerce' ),
                    'A2'  => __( 'A2 - Invoice already paid', 'gofactox-e-invoicing-for-woocommerce' ),
                    'A9'  => __( 'A9 - Subcontractor invoice (sous-traitance)', 'gofactox-e-invoicing-for-woocommerce' ),
                    'A12' => __( 'A12 - Co-contractor invoice (cotraitance)', 'gofactox-e-invoicing-for-woocommerce' ),
                    ''    => __( 'Do not emit (BT-23 omitted)', 'gofactox-e-invoicing-for-woocommerce' ),
                ],
                'default'  => 'A1',
                'desc_tip' => __( 'Invoicing framework code (EN 16931 BT-23). Only relevant when depositing to a Public Administration via Chorus Pro; A1 is the safe default for ordinary sales and harmless for B2B/B2C.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_invoice_section',
            ],

            // --- Payment Terms ---
            [
                'title' => __( 'Payment Terms', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'desc'  => __( 'Default payment terms shown on invoices. French law caps B2B terms at 60 days or 45 days end of month.', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_payment_terms_section',
            ],
            [
                'title'   => __( 'Payment Terms', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_payment_terms',
                'type'    => 'select',
                'options' => [
                    'immediate'   => __( 'Due upon receipt', 'gofactox-e-invoicing-for-woocommerce' ),
                    'net_15'      => __( 'Net 15 days', 'gofactox-e-invoicing-for-woocommerce' ),
                    'net_30'      => __( 'Net 30 days', 'gofactox-e-invoicing-for-woocommerce' ),
                    'net_45'      => __( 'Net 45 days', 'gofactox-e-invoicing-for-woocommerce' ),
                    'net_60'      => __( 'Net 60 days (max B2B in France)', 'gofactox-e-invoicing-for-woocommerce' ),
                    'eom_30'      => __( '30 days end of month — Pro', 'gofactox-e-invoicing-for-woocommerce' ),
                    'eom_45'      => __( '45 days end of month — Pro', 'gofactox-e-invoicing-for-woocommerce' ),
                    'custom'      => __( 'Custom (specify text below) — Pro', 'gofactox-e-invoicing-for-woocommerce' ),
                ],
                'default' => 'immediate',
                'desc_tip' => __( 'Default applied to all invoices. End-of-month variants and custom text require a Pro plan.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'       => __( 'Custom Payment Terms Text', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_payment_terms_custom',
                'type'        => 'text',
                'css'         => 'min-width: 400px;',
                'placeholder' => __( 'Free-form text shown when "Custom" is selected.', 'gofactox-e-invoicing-for-woocommerce' ),
                'desc_tip'    => __( 'Only used when Payment Terms is set to Custom.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_payment_terms_section',
            ],

            // --- Credit Note Numbering ---
            [
                'title' => __( 'Credit Notes — Pro', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'desc'  => __( 'Credit notes are issued for WooCommerce refunds. They use a separate sequence from invoices. Generating Factur-X credit notes (UN/CEFACT type 381) requires a Pro plan.', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_credit_note_section',
            ],
            [
                'title'    => __( 'Credit Note Prefix', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'       => 'gofactox_credit_note_prefix',
                'type'     => 'text',
                'default'  => 'AV-',
                'desc_tip' => __( 'Prefix for credit note numbers. Year is added automatically.', 'gofactox-e-invoicing-for-woocommerce' ),
                'desc'     => sprintf(
                    /* translators: %s: next credit note number preview */
                    __( 'Next credit note: %s', 'gofactox-e-invoicing-for-woocommerce' ),
                    '<strong>' . GOFACTOX_Numbering::preview_next( 'credit_note' ) . '</strong>'
                ),
            ],
            [
                'title'             => __( 'Credit Note Digits', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'                => 'gofactox_credit_note_digits',
                'type'              => 'number',
                'default'           => 4,
                'custom_attributes' => [ 'min' => '3', 'max' => '8' ],
                'desc_tip'          => __( 'Number of digits for the sequential part of credit note numbers.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'             => __( 'Next Credit Note Number', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'                => 'gofactox_next_credit_note_number',
                'type'              => 'number',
                'default'           => 1,
                'custom_attributes' => [ 'min' => '1' ],
                'desc_tip'          => __( 'Warning: changing this may create gaps in your credit note sequence.', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'title'   => __( 'AVOIR Watermark on PDF — Pro', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_credit_note_watermark',
                'type'    => 'checkbox',
                'desc'    => __( 'Add a discreet "AVOIR" watermark on the generated credit note PDF (Pro feature).', 'gofactox-e-invoicing-for-woocommerce' ),
                'default' => 'yes',
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_credit_note_section',
            ],

            // --- Automation ---
            [
                'title' => __( 'Automation', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'desc'  => __( 'Automation features (auto-generation on order completion, automatic credit notes on refunds, email attachments) require a Pro or Business plan. Settings below are visible for all users but only effective once a paid plan is activated.', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_automation_section',
            ],
            [
                'title'   => __( 'Auto-Generate Invoices', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_auto_generate',
                'type'    => 'checkbox',
                'desc'    => __( 'Automatically generate an invoice when an order reaches the selected status.', 'gofactox-e-invoicing-for-woocommerce' ),
                'default' => 'yes',
            ],
            [
                'title'   => __( 'Trigger Status', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_auto_generate_status',
                'type'    => 'select',
                'options' => [
                    'completed'  => __( 'Completed', 'gofactox-e-invoicing-for-woocommerce' ),
                    'processing' => __( 'Processing', 'gofactox-e-invoicing-for-woocommerce' ),
                ],
                'default' => 'completed',
            ],
            [
                'title'   => __( 'Auto-Generate Credit Notes', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_auto_generate_credit_notes',
                'type'    => 'checkbox',
                'desc'    => __( 'Automatically generate a Factur-X credit note when a refund is created on an order.', 'gofactox-e-invoicing-for-woocommerce' ),
                'default' => 'yes',
            ],
            [
                'title'   => __( 'Attach to Email', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_attach_to_email',
                'type'    => 'checkbox',
                'desc'    => __( 'Attach the invoice PDF to the order confirmation email.', 'gofactox-e-invoicing-for-woocommerce' ),
                'default' => 'yes',
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_automation_section',
            ],

            // --- B2B Checkout ---
            [
                'title' => __( 'B2B Checkout Fields', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'id'    => 'gofactox_b2b_section',
            ],
            [
                'title'   => __( 'Enable B2B Fields', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_enable_b2b_fields',
                'type'    => 'checkbox',
                'desc'    => __( 'Add SIREN, SIRET and VAT number fields to the checkout page.', 'gofactox-e-invoicing-for-woocommerce' ),
                'default' => 'yes',
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_b2b_section',
            ],

            // --- Legal ---
            [
                'title' => __( 'Payment Terms', 'gofactox-e-invoicing-for-woocommerce' ),
                'type'  => 'title',
                'desc'  => __( 'French law requires late payment penalties and recovery indemnity on all invoices.', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_legal_section',
            ],
            [
                'title'       => __( 'Late Payment Interest Rate', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_late_payment_rate',
                'type'        => 'text',
                'desc_tip'    => __( 'Annual interest rate for late payments. Default: 3x the legal interest rate. Example: "12.47%"', 'gofactox-e-invoicing-for-woocommerce' ),
                'placeholder' => __( '3x the legal interest rate', 'gofactox-e-invoicing-for-woocommerce' ),
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_legal_section',
            ],
        ];
    }
}
