<?php
/**
 * Privacy / GDPR view: tracking & retention settings + privacy snippet.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$keys = array(
    'tracking_enabled', 'tracking_mode', 'tracking_exclude_admins', 'tracking_exclude_bots',
    'tracking_respect_dnt', 'tracking_exclude_paths',
    'privacy_anonymize_ip', 'privacy_hash_rotate_daily', 'privacy_store_full_url',
    'privacy_store_user_agent', 'visit_retention_days', 'session_retention_days',
    'log_404_retention_days', 'daily_retention_days', 'audit_retention_days',
    'delete_on_uninstall', 'persist_settings',
);

// German-market privacy-policy template (legal wording) — intentionally not
// translated/altered; the target audience for this snippet reads German.
$snippet = sprintf(
    "Diese Website nutzt Brainwerk SEO Suite zur datenschutzfreundlichen Reichweitenanalyse. " .
    "Es werden keine Wiedererkennungs-/Tracking-Cookies gesetzt und keine Daten an Dritte (z. B. Google) übertragen. " .
    "IP-Adressen werden vor der Speicherung anonymisiert (letztes Oktett bzw. /64-Präfix). " .
    "Ziel ist eine weitgehende Vermeidung des Personenbezugs. Speicherdauer: %d Tage. " .
    "Bitte prüfen Sie eigenverantwortlich (ggf. rechtlich), ob in Ihrem Fall eine Einwilligung erforderlich ist.",
    (int) Seoforge_Settings::get( 'visit_retention_days', 365 )
);
?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Privacy / GDPR', 'brainwerk-seo-suite' ); ?></h2>
    <div class="seoforge-callout seoforge-callout-good">
        <strong>✓ <?php esc_html_e( 'Designed to work without tracking cookies', 'brainwerk-seo-suite' ); ?></strong>
        <p style="margin:4px 0 0;"><?php esc_html_e( 'With the default settings, Brainwerk SEO Suite minimizes personal data: IPs are anonymized, the visitor hash rotates daily, and no third party is contacted. Whether a consent banner is required in your case depends on your configuration and jurisdiction and should be legally reviewed.', 'brainwerk-seo-suite' ); ?></p>
    </div>
    <p class="description"><?php esc_html_e( 'Below you can fine-tune what is stored and for how long.', 'brainwerk-seo-suite' ); ?></p>

    <form method="post" action="">
        <?php wp_nonce_field( Seoforge_Admin::NONCE ); ?>
        <input type="hidden" name="seoforge_action" value="save_settings">
        <?php foreach ( $keys as $k ) : ?>
            <input type="hidden" name="seoforge_tab_keys[]" value="<?php echo esc_attr( $k ); ?>">
        <?php endforeach; ?>

        <h3><?php esc_html_e( 'Tracking', 'brainwerk-seo-suite' ); ?></h3>
        <table class="form-table">
            <tr><th><?php esc_html_e( 'Enable visitor tracking', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[tracking_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'tracking_enabled', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Tracking mode', 'brainwerk-seo-suite' ); ?></th>
                <td><select name="seoforge[tracking_mode]">
                    <option value="server" <?php selected( Seoforge_Settings::get( 'tracking_mode' ), 'server' ); ?>>Server-side (no JS)</option>
                    <option value="beacon" <?php selected( Seoforge_Settings::get( 'tracking_mode' ), 'beacon' ); ?>>JS Beacon</option>
                    <option value="hybrid" <?php selected( Seoforge_Settings::get( 'tracking_mode' ), 'hybrid' ); ?>>Hybrid (server + beacon)</option>
                </select></td></tr>
            <tr><th><?php esc_html_e( 'Exclude logged-in editors', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[tracking_exclude_admins]" value="1"
                    <?php checked( Seoforge_Settings::get( 'tracking_exclude_admins', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Exclude bots', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[tracking_exclude_bots]" value="1"
                    <?php checked( Seoforge_Settings::get( 'tracking_exclude_bots', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Respect Do-Not-Track header', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[tracking_respect_dnt]" value="1"
                    <?php checked( Seoforge_Settings::get( 'tracking_respect_dnt', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Exclude paths', 'brainwerk-seo-suite' ); ?></th>
                <td><textarea name="seoforge[tracking_exclude_paths]" rows="4" cols="40" class="regular-text"><?php
                    echo esc_textarea( implode( "\n", (array) Seoforge_Settings::get( 'tracking_exclude_paths', array() ) ) );
                ?></textarea>
                <p class="description"><?php esc_html_e( 'One path per line. Substring match.', 'brainwerk-seo-suite' ); ?></p></td></tr>
        </table>

        <h3><?php esc_html_e( 'Anonymization', 'brainwerk-seo-suite' ); ?></h3>
        <table class="form-table">
            <tr><th><?php esc_html_e( 'Anonymize IP', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[privacy_anonymize_ip]" value="1"
                    <?php checked( Seoforge_Settings::get( 'privacy_anonymize_ip', true ) ); ?>><span></span></label>
                    <p class="description"><?php esc_html_e( 'IPv4: zero last octet. IPv6: keep first /64.', 'brainwerk-seo-suite' ); ?></p></td></tr>
            <tr><th><?php esc_html_e( 'Rotate visitor hash daily', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[privacy_hash_rotate_daily]" value="1"
                    <?php checked( Seoforge_Settings::get( 'privacy_hash_rotate_daily', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Store full URL', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[privacy_store_full_url]" value="1"
                    <?php checked( Seoforge_Settings::get( 'privacy_store_full_url', true ) ); ?>><span></span></label>
                    <p class="description"><?php esc_html_e( 'When off, only the path is stored (no query strings).', 'brainwerk-seo-suite' ); ?></p></td></tr>
            <tr><th><?php esc_html_e( 'Store user-agent string', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[privacy_store_user_agent]" value="1"
                    <?php checked( Seoforge_Settings::get( 'privacy_store_user_agent', true ) ); ?>><span></span></label></td></tr>
        </table>

        <h3><?php esc_html_e( 'Retention', 'brainwerk-seo-suite' ); ?></h3>
        <table class="form-table">
            <tr><th><?php esc_html_e( 'Visits (days)', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="number" name="seoforge[visit_retention_days]" value="<?php echo esc_attr( Seoforge_Settings::get( 'visit_retention_days', 365 ) ); ?>" min="1" class="small-text"></td></tr>
            <tr><th><?php esc_html_e( 'Sessions (days)', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="number" name="seoforge[session_retention_days]" value="<?php echo esc_attr( Seoforge_Settings::get( 'session_retention_days', 730 ) ); ?>" min="1" class="small-text"></td></tr>
            <tr><th><?php esc_html_e( '404 log (days)', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="number" name="seoforge[log_404_retention_days]" value="<?php echo esc_attr( Seoforge_Settings::get( 'log_404_retention_days', 90 ) ); ?>" min="1" class="small-text"></td></tr>
            <tr><th><?php esc_html_e( 'Pre-aggregated daily stats (days)', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="number" name="seoforge[daily_retention_days]" value="<?php echo esc_attr( Seoforge_Settings::get( 'daily_retention_days', 730 ) ); ?>" min="1" class="small-text"></td></tr>
            <tr><th><?php esc_html_e( 'Audit log (days)', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="number" name="seoforge[audit_retention_days]" value="<?php echo esc_attr( Seoforge_Settings::get( 'audit_retention_days', 180 ) ); ?>" min="1" class="small-text"></td></tr>
            <tr><th><?php esc_html_e( 'Delete all data on uninstall', 'brainwerk-seo-suite' ); ?></th>
                <td><label><input type="checkbox" name="seoforge[delete_on_uninstall]" value="1"
                    <?php checked( Seoforge_Settings::get( 'delete_on_uninstall', true ) ); ?>>
                    <?php esc_html_e( 'Drop tables and options when the plugin is uninstalled.', 'brainwerk-seo-suite' ); ?></label></td></tr>
            <tr><th><?php esc_html_e( 'Remember settings for reinstall', 'brainwerk-seo-suite' ); ?></th>
                <td><label><input type="checkbox" name="seoforge[persist_settings]" value="1"
                    <?php checked( Seoforge_Settings::get( 'persist_settings', true ) ); ?>>
                    <?php esc_html_e( 'Keep a private backup of your configuration so it is restored automatically if you reinstall the plugin.', 'brainwerk-seo-suite' ); ?></label>
                    <p class="description"><?php esc_html_e( 'The backup is stored in the database (never in a public folder) and contains only your settings — never visitor data. It survives even the "delete all data" option above. Turn this off for a complete wipe on uninstall.', 'brainwerk-seo-suite' ); ?></p></td></tr>
        </table>

        <?php submit_button( __( 'Save privacy settings', 'brainwerk-seo-suite' ) ); ?>
    </form>
</div>

<div class="seoforge-card">
    <h2><?php esc_html_e( 'Hub registration', 'brainwerk-seo-suite' ); ?></h2>
    <div class="seoforge-callout seoforge-callout-info">
        <p style="margin:0;"><?php esc_html_e( 'Two optional features — the SEO benchmark network (Tools → SEO network) and external notarization (Verify → External notarization) — talk to the same external hub. Turning either one on for the first time, if this site isn\'t registered yet, triggers a single one-time registration: the only thing it sends is this site\'s web address, used as a label. The hub replies with a pseudonymous site ID and a secret, stored locally, which authenticate all further requests from either feature — no personal data, no visitor data.', 'brainwerk-seo-suite' ); ?></p>
    </div>
    <p class="description"><?php esc_html_e( 'Both features share that one registration, but each has its own opt-in and its own data path from there. The benchmark network additionally sends a daily aggregate snapshot of anonymised SEO/traffic figures. Notarization additionally sends a daily chain-head hash, the date range it covers and a row count — never a measurement. A site that only notarizes, without ever opting into the benchmark network, never sends anything beyond that one-time registration and the daily chain-head checkpoints — no traffic or SEO measurement ever leaves the site through that path.', 'brainwerk-seo-suite' ); ?></p>
</div>

<div class="seoforge-card">
    <h2><?php esc_html_e( 'Privacy-policy snippet', 'brainwerk-seo-suite' ); ?></h2>
    <p class="description"><?php esc_html_e( 'Copy this paragraph into your privacy policy.', 'brainwerk-seo-suite' ); ?></p>
    <textarea readonly rows="6" class="large-text code" id="sf-privacy-snippet"><?php echo esc_textarea( $snippet ); ?></textarea>
    <p>
        <button type="button" class="button" onclick="navigator.clipboard.writeText(document.getElementById('sf-privacy-snippet').value);this.textContent='<?php echo esc_js( __( 'Copied!', 'brainwerk-seo-suite' ) ); ?>';">
            <?php esc_html_e( 'Copy to clipboard', 'brainwerk-seo-suite' ); ?>
        </button>
    </p>
</div>
