<?php
/**
 * Admin Tab: Settings
 * Main configuration fields
 */

if (! defined( 'ABSPATH' ) ) {
    exit;
}

// === Auto-import data (no <script> here) ===
 // Determine effective cache hours. Default comes from core helper which
 // reads `newssync_cache_duration`. Use a filter so extensions (PRO)
 // can override this value without modifying the free plugin.
 $newssync_cache_hours = (int) ( newssync_get_cache_duration() / HOUR_IN_SECONDS );
 // Allow other code (including PRO) to override the effective cache hours.
 /**
  * Filter the effective cache hours shown in the settings UI.
  *
  * @param int $hours Number of hours used for cache/automatic import
  */
$newssync_cache_hours = apply_filters( 'newssync/effective_cache_hours', $newssync_cache_hours );
 $newssync_carousel_abs_max = 50; // hard upper bound (ajusta se preferires 30, 100, etc.)

/* translators: 1: number of hours (integer), 2: number of hours (integer). Both placeholders refer to hours until import/cache duration. */
$newssync_auto_import_format = esc_html__( 'Automatic import active (from %1$d in %2$d hours)', 'daazsync' );
$newssync_auto_import_text   = sprintf( $newssync_auto_import_format, $newssync_cache_hours, $newssync_cache_hours );

// Prepare cache field description and attributes depending on PRO presence.
if ( defined( 'NEWSSYNC_PRO_DIR' ) ) {
    $newssync_cache_desc = esc_html__( 'How long to cache RSS feeds.', 'daazsync' );
    $newssync_cache_attrs = array( 'disabled' => 'disabled' );
} else {
    $newssync_cache_desc = sprintf( /* translators: %s: link to PRO purchase page */ __( 'How long to cache RSS feeds. More options (30m, 1h, 2h, 3h, etc.) are available in PRO: %s', 'daazsync' ), '<a href="https://daazlabs.com/newssync/" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Activate NewsSync PRO', 'daazsync' ) . '</a>' );
    $newssync_cache_attrs = array();
}

// Strings for JS (will be passed to JS with wp_json_encode in admin_footer)
 $newssync_nonce             = wp_create_nonce( 'newssync_force_import' );
// cria nonce para clear cache (usado pelo inline JS)
 $newssync_clear_cache_nonce = wp_create_nonce( 'newssync_clear_cache_action' );
 $newssync_importing_text    = __( 'Importing...', 'daazsync' );
 $newssync_error_import_text = __( 'Error during import', 'daazsync' );
 $newssync_timeout_msg_text  = __( 'Import timed out or error — check posts for results.', 'daazsync' );
 $newssync_force_label       = __( 'Force Import Now', 'daazsync' );

// Build only the HTML (no <script> tags) — plugin UI will render this HTML block.
// Keep it simple and avoid inline scripts here so sanitizers won't turn scripts into visible text.
ob_start();
?>
<div style="margin-top:20px; padding:15px; background:#e3fcec; border-left:4px solid #00d1b2; border-radius:4px;">
    <p style="margin:0 0 10px; font-weight:600; color:#006b5b;">
        <?php echo esc_html( $newssync_auto_import_text ); ?>
    </p>
    <p style="margin:0 0 15px; font-size:13px; color:#666;">
        <?php echo esc_html__( 'The import only runs when the feed cache expires or when you click the button below.', 'daazsync' ); ?>
    </p>
    <button type="button" id="rsp-force-import-btn" class="button button-primary">
        <?php echo esc_html__( 'Force Import Now', 'daazsync' ); ?>
    </button>
    <p id="rsp-import-status" style="margin-top:10px; font-size:13px;"></p>
</div>
<?php
$newssync_html_block = ob_get_clean();

$newssync_auto_import_field = [
    'type' => 'html',
    'html' => $newssync_html_block,
];


 $newssync_fields = [

    // === DEFAULT LAYOUT ===
    [
        'id'      => 'newssync_default_layout',
        'name'    => __( 'Default Layout', 'daazsync' ),
        'desc'    => __( 'Choose the default layout for the shortcode when no layout is specified.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            'grid'     => __( 'Grid', 'daazsync' ),
            'list'     => __( 'List', 'daazsync' ),
            'carousel' => __( 'Carousel', 'daazsync' ),
            'hero'     => __( 'Hero (Featured)', 'daazsync' ),
        ],
        'default' => 'grid',
    ],

     // === DEFAULT LIST MODE ===
    [
        'id'      => 'newssync_list_mode',
        'name'    => __( 'Default List Mode', 'daazsync' ),
        'desc'    => __( 'Choose the default style for List layout. Can be overridden in shortcode.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            'standard' => __( 'Standard (with image and text)', 'daazsync' ),
            'minimal'  => __( 'Minimal (title + time only)', 'daazsync' ),
            'custom'   => __( 'Custom (use shortcode parameters)', 'daazsync' ),
        ],
        'default' => 'standard',
    ],

    // === HERO: MAX ITEMS (limited in this distribution) ===
    [
        'id'      => 'newssync_hero_max_items',
        'name'    => __( 'Hero Carousel: Max Items', 'daazsync' ),
        'desc'    => __( 'Maximum number of items in Hero/Carousel layout. Limited to 6 by default.', 'daazsync' ),
        'type'    => 'select',
        'options' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6' ],
        'default' => '6',
    ],

    // === SHOW IMAGES ===
    [
        'id'      => 'newssync_show_image',
        'name'    => __( 'Show Images', 'daazsync' ),
        'desc'    => __( 'Display featured images in the feed.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            '1' => __( 'Yes', 'daazsync' ),
            '0' => __( 'No', 'daazsync' ),
        ],
        'default' => '1',
    ],

    // === USE PLACEHOLDER IMAGES ===
    [
        'id'      => 'newssync_use_placeholders',
        'name'    => __( 'Placeholder Images', 'daazsync' ),
        'desc'    => __( 'Show a placeholder image when a feed item has no image. Disable to show no image at all.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            '1' => __( 'Yes', 'daazsync' ),
            '0' => __( 'No', 'daazsync' ),
        ],
        'default' => '1',
    ],

    // === CACHE DURATION (FREE) ===
    // Free distribution offers a conservative set of choices. Show an
    // upsell link to PRO for shorter/more granular durations (no PRO
    // code or option reads in the free plugin).
    [
        'id'      => 'newssync_cache_duration',
        'name'    => __( 'Cache Duration', 'daazsync' ),
        'desc'    => isset( $newssync_cache_desc ) ? $newssync_cache_desc : __( 'How long to cache RSS feeds.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            '6h'  => __( '6 Hours', 'daazsync' ),
            '24h' => __( '24 Hours', 'daazsync' ),
        ],
        'default' => '6h',
        'attrs'   => isset( $newssync_cache_attrs ) ? $newssync_cache_attrs : array(),
    ],


    // === SAVE RSS ITEMS AS POSTS ===
    [
        'id'      => 'newssync_save_as_posts',
        'name'    => __( 'Save RSS Items as Posts', 'daazsync' ),
        /* Store description parts as plain text to avoid translators introducing HTML.
         * Renderer will assemble safe HTML (code, br, span) when needed. */
        'desc'    => esc_html__( 'Imported RSS items are saved as posts. Not automatic — runs only when page is viewed.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            '1' => __( 'Yes', 'daazsync' ),
            '0' => __( 'No', 'daazsync' ),
        ],
        'default' => '0',
    ],


    // === BUTTON: OPEN FEED PAGE & IMPORT NOW (WITH AJAX) ===
    $newssync_auto_import_field,

    // === POST STATUS ===
    [
        'id'      => 'newssync_publish_status',
        'name'    => __( 'Post Status', 'daazsync' ),
        'desc'    => __( 'Choose if new RSS posts are published immediately or saved as draft.', 'daazsync' ),
        'type'    => 'select',
        'options' => [
            'publish' => __( 'Publish', 'daazsync' ),
            'draft'   => __( 'Draft', 'daazsync' ),
        ],
        'default' => 'publish',
    ],


    // NOTE: 'Max Items per Import', 'Duplicate Similarity Threshold' and
    // 'Show Read More Link' settings removed from free distribution.

];

/**
 * Filter: Settings fields
 * Extensions can add/modify settings fields. For backward compatibility
 * this filter returns a flat array of fields. Here we split the
 * resulting fields into two groups: free and pro. The sections renderer
 * will accept either the legacy flat array or the new structured array
 * with keys 'free' and 'pro'.
 *
 * @param array $fields Array of field definitions
 */
 $newssync_all_fields = apply_filters( 'newssync/settings_fields', $newssync_fields );

 $newssync_free_fields = array();
 $newssync_pro_fields  = array();

 foreach ( (array) $newssync_all_fields as $newssync_f ) {
    $newssync_fid = isset( $newssync_f['id'] ) ? (string) $newssync_f['id'] : '';
    $newssync_is_pro_flag = isset( $newssync_f['pro'] ) && $newssync_f['pro'];
    $newssync_is_pro_id = preg_match( '/^newssync_pro_/', $newssync_fid );

    if ( $newssync_is_pro_flag || $newssync_is_pro_id ) {
        $newssync_pro_fields[] = $newssync_f;
    } else {
        $newssync_free_fields[] = $newssync_f;
    }
 }

// Return structured array (new) but keep legacy combined list available as 'all'
return array(
    'free' => $newssync_free_fields,
    'pro'  => $newssync_pro_fields,
    'all'  => $newssync_all_fields,
);
