<?php
if (!defined('ABSPATH')) exit;

if (!function_exists('custausu_admin_render_settings_transfer_preview_section')) {
  function custausu_admin_render_settings_transfer_preview_section(callable $T): string {
    ob_start();

    echo wp_kses( custausu_admin_gate_wrap_start('settings_export_import', $T), custausu_admin_allowed_output_html() );
    echo '<table class="widefat striped custausu-settings-transfer">';
    echo '<colgroup><col class="custausu-st-col-label"><col class="custausu-st-col-content"></colgroup>';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-settings-transfer__head-cell"><h2 class="custausu-settings-transfer__title"><span class="dashicons dashicons-database-export"></span> | ' . esc_html($T('Settings Export / Import', 'Esporta / Importa impostazioni')) . '</h2></th></tr></thead>';
    echo '<tbody>';

    echo '<tr><th scope="row">' . esc_html($T('Export settings', 'Esporta impostazioni')) . '</th><td>';
    echo '<button type="button" class="button custausu-settings-transfer__export-button" disabled aria-disabled="true">' . esc_html($T('Export Settings', 'Esporta impostazioni')) . '</button>';
    echo '<p class="description custausu-transfer-note custausu-settings-transfer__export-note">' . esc_html($T('Exports CAS settings as a versioned JSON payload, grouped by sections.', 'Esporta le impostazioni CAS come payload JSON versionato, raggruppato per sezioni.')) . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row">' . esc_html($T('Import settings', 'Importa impostazioni')) . '</th><td>';
    echo '<div class="custausu-import-settings-form custausu-import-settings-form--preview" aria-disabled="true">';
    echo '<p class="description custausu-transfer-note">' . esc_html($T('Upload a CAS settings export JSON. First run Analyze, then Import.', 'Carica un JSON di export CAS. Esegui prima Analizza, poi Importa.')) . '</p>';
    echo '<div class="custausu-import-file"><input type="file" accept=".json,application/json" disabled aria-disabled="true"></div>';
    echo '<div class="custausu-import-sections">';
    foreach ([
      $T('Protected paths', 'Percorsi protetti'),
      $T('Default language', 'Lingua predefinita'),
      $T('Routes & templates', 'Percorsi e template'),
      $T('Assets', 'Assets'),
      $T('Email theme', 'Tema email'),
      $T('Email templates', 'Template email'),
      $T('reCAPTCHA', 'reCAPTCHA'),
      $T('reCAPTCHA thresholds', 'Soglie reCAPTCHA'),
      $T('CF7', 'CF7'),
      $T('Debug', 'Debug'),
      $T('WebAuthn', 'WebAuthn'),
      $T('Maintenance / uninstall', 'Manutenzione / disinstallazione'),
    ] as $label) {
      echo '<label><input type="checkbox" checked disabled aria-disabled="true"><span>' . esc_html($label) . '</span></label>';
    }
    echo '</div>';
    echo '<div class="custausu-import-actions">';
    echo '<button type="button" class="button" disabled aria-disabled="true">' . esc_html($T('Analyze import', 'Analizza import')) . '</button>';
    echo '<button type="button" class="button button-primary custausu-import-submit-confirm" disabled aria-disabled="true">' . esc_html($T('Import selected sections', 'Importa sezioni selezionate')) . '</button>';
    echo '</div></div></td></tr>';
    echo '</tbody></table>';
    echo wp_kses( custausu_admin_gate_wrap_end(), custausu_admin_allowed_output_html() );
    return (string) ob_get_clean();
  }
}

if (!function_exists('custausu_admin_render_settings_transfer_section')) {
  function custausu_admin_render_settings_transfer_section(callable $T): string {
    return custausu_admin_render_settings_transfer_preview_section($T);
  }
}

if (!function_exists('custausu_admin_render_logs_tools_section')) {
  function custausu_admin_render_logs_tools_section(callable $T): string {
    ob_start();

    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_start('advanced_debug_tools', $T), custausu_admin_allowed_output_html() );
    echo '<table class="widefat striped custausu-logs-tools custausu-logs-tools--preview">';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-logs-tools__head-cell"><h2 class="custausu-logs-tools__title"><span class="dashicons dashicons-media-text"></span> | Logs</h2></th></tr></thead>';
    echo '<tbody>';

    echo '<tr><th scope="row">' . esc_html($T('Clear logs', 'Svuota log')) . '</th><td>';
    echo '<button type="button" class="button" disabled aria-disabled="true">'
      . esc_html($T('Clear log files', 'Svuota file di log'))
      . '</button>';
    echo '<p class="description custausu-logs-tools__description">'
      . esc_html($T('Clears log files (clears their content).', 'Svuota i file di log (elimina il loro contenuto).'))
      . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row">' . esc_html($T('Export logs', 'Esporta log')) . '</th><td>';
    echo '<button type="button" class="button" disabled aria-disabled="true">'
      . esc_html($T('Export Logs (ZIP)', 'Esporta Log (ZIP)'))
      . '</button>';
    echo '<p class="description custausu-logs-tools__description custausu-logs-tools__description--spaced">'
      . esc_html($T('Export Logs is available with Premium.', 'Esporta Log è disponibile con Premium.'))
      . '</p>';
    echo '</td></tr>';

    echo '</tbody></table>';
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_end(), custausu_admin_allowed_output_html() );

    return (string) ob_get_clean();
  }
}

if (!function_exists('custausu_admin_render_reset_section')) {
  function custausu_admin_render_reset_section(callable $T): string {
    ob_start();

    echo '<table class="widefat striped custausu-reset-tools">';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-reset-tools__head-cell"><h2 class="custausu-reset-tools__title"><span class="dashicons dashicons-warning"></span> | CAS Reset</h2></th></tr></thead>';
    echo '<tbody><tr><th scope="row">' . esc_html($T('Reset CAS settings', 'Reset impostazioni CAS')) . '</th><td>';

    echo '<form id="custausu-reset-settings-form" method="post" action="' . esc_url(admin_url('admin-post.php')) . '" class="custausu-reset-settings-form">';
    echo '<input type="hidden" name="action" value="custausu_reset_settings">';
    wp_nonce_field('custausu_reset_settings');
    echo '<button type="button" id="custausu-reset-settings-btn" class="button button-secondary custausu-reset-settings__button">'
      . esc_html($T('Reset settings', 'Reset impostazioni'))
      . '</button>';
    echo '<p class="description custausu-reset-settings__description">'
      . esc_html($T('Warning: this deletes CAS settings and returns the plugin to a clean state.', 'Attenzione: questa azione elimina le impostazioni CAS e riporta il plugin a uno stato pulito.'))
      . '</p>';
    echo '</form>';

    echo '</td></tr></tbody></table>';

    return (string) ob_get_clean();
  }
}

if (!function_exists('custausu_admin_render_uninstall_section')) {
  function custausu_admin_render_uninstall_section(
    callable $T,
    bool $keep_data_on_uninstall,
    callable $sw
  ): string {
    ob_start();

    echo '<table class="widefat striped custausu-uninstall-tools">';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-uninstall-tools__head-cell"><h2 class="custausu-uninstall-tools__title"><span class="dashicons dashicons-database-remove"></span> | '.esc_html($T('Uninstall & Data Retention','Disinstallazione e conservazione dati')).'</h2></th></tr></thead>';
    echo '<tbody>';

    echo '<tr><th scope="row">'.esc_html($T('Keep data on uninstall','Mantieni i dati alla disinstallazione')).'</th><td>';

    $sw(
      'custausu_global_settings[keep_data_on_uninstall]',
      (bool)$keep_data_on_uninstall,
      $T(
        'When enabled, deleting the plugin from WordPress keeps CAS custom tables, options, user meta and local license data.',
        'Se attivo, l’eliminazione del plugin da WordPress mantiene tabelle custom, opzioni, user meta e dati licenza locali di CAS.'
      ),
      false
    );

    echo '<p class="description custausu-uninstall-tools__description">'
      . esc_html($T(
        'Recommended ON on production sites before uninstall/reinstall cycles. Turn it OFF only if you want a full local cleanup during plugin deletion.',
        'Consigliato ON sui siti in produzione prima di cicli uninstall/reinstall. Impostalo su OFF solo se vuoi una pulizia locale completa durante l’eliminazione del plugin.'
      ))
      . '</p>';

    echo '<p class="description custausu-uninstall-tools__description custausu-uninstall-tools__description--warning">'
      . esc_html($T(
        'Important: with this option OFF, plugin deletion also removes the local CAS license state stored on this website. If you need to free the remote activation on the CAS License Server, deactivate the license before deleting the plugin.',
       'Importante: con questa opzione OFF, l’eliminazione del plugin rimuove anche lo stato licenza CAS locale salvato su questo sito. Se devi liberare l’attivazione remota sul CAS License Server, disattiva la licenza prima di eliminare il plugin.'
      ))
      . '</p>';

    echo '</td></tr>';

    echo '</tbody></table>';

    return (string) ob_get_clean();
  }
}
