<?php
/**
 * GGM Commerce Legal / Compliance Admin Tab
 *
 * @package GGM_Commerce
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Render the Legal / Compliance admin tab
 */
function ggmc_render_legal_tab() {
    $cookies = [
        [
            'name'     => 'ggmc_compare',
            'purpose'  => 'Stores the list of product IDs the visitor has added to the product comparison tool. This allows products to persist in the comparison view across page loads.',
            'duration' => '30 days',
            'category' => 'Functional',
        ],
        [
            'name'     => 'ggmc_wishlist',
            'purpose'  => 'Stores the wishlist for guest visitors (not logged in). Contains product IDs the visitor has saved to their wishlist. Logged-in users\' wishlists are stored in the database instead.',
            'duration' => '30 days',
            'category' => 'Functional',
        ],
        [
            'name'     => 'ggmc_recently_viewed',
            'purpose'  => 'Tracks which products the visitor has recently viewed, so they can be displayed in the "Recently Viewed" widget. Contains only product IDs.',
            'duration' => '30 days',
            'category' => 'Functional',
        ],
    ];

    // Build plain-text version for copy-paste.
    $plain_text = '';
    foreach ( $cookies as $cookie ) {
        $plain_text .= "Cookie Name: {$cookie['name']}\n";
        $plain_text .= "Purpose: {$cookie['purpose']}\n";
        $plain_text .= "Duration: {$cookie['duration']}\n";
        $plain_text .= "Category: {$cookie['category']}\n\n";
    }
    $plain_text = trim( $plain_text );
    ?>
    <div class="ggmc-settings-layout">
        <div class="ggmc-settings-main">

            <div class="card">
                <h2><?php esc_html_e( 'Cookie Information', 'ggm-commerce' ); ?></h2>
                <p class="description">
                    <?php esc_html_e( 'Goose Commerce uses the following cookies for core shopping functionality. All cookies are strictly functional — they contain only product IDs and are not used for tracking, analytics, or advertising.', 'ggm-commerce' ); ?>
                </p>

                <table class="widefat striped" style="margin-top: 15px;">
                    <thead>
                        <tr>
                            <th><?php esc_html_e( 'Cookie Name', 'ggm-commerce' ); ?></th>
                            <th><?php esc_html_e( 'Purpose', 'ggm-commerce' ); ?></th>
                            <th><?php esc_html_e( 'Duration', 'ggm-commerce' ); ?></th>
                            <th><?php esc_html_e( 'Category', 'ggm-commerce' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $cookies as $cookie ) : ?>
                        <tr>
                            <td><code><?php echo esc_html( $cookie['name'] ); ?></code></td>
                            <td><?php echo esc_html( $cookie['purpose'] ); ?></td>
                            <td><?php echo esc_html( $cookie['duration'] ); ?></td>
                            <td><?php echo esc_html( $cookie['category'] ); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <div class="card">
                <h2><?php esc_html_e( 'Copy for Cookie Consent Plugin', 'ggm-commerce' ); ?></h2>
                <p class="description">
                    <?php esc_html_e( 'Copy the text below and paste it into your cookie consent plugin (e.g. CookieYes, Complianz, GDPR Cookie Consent).', 'ggm-commerce' ); ?>
                </p>
                <div style="position: relative; margin-top: 15px;">
                    <textarea id="ggmc-cookie-text" readonly rows="14" style="width: 100%; font-family: monospace; font-size: 13px; background: #f6f7f7; resize: vertical;"><?php echo esc_textarea( $plain_text ); ?></textarea>
                    <p>
                        <button type="button" class="button" id="ggmc-copy-cookie-text">
                            <span class="dashicons dashicons-clipboard" style="margin-top: 4px;"></span>
                            <?php esc_html_e( 'Copy to Clipboard', 'ggm-commerce' ); ?>
                        </button>
                        <span id="ggmc-copy-status" style="margin-left: 10px; color: #00a32a; display: none;">
                            <?php esc_html_e( 'Copied!', 'ggm-commerce' ); ?>
                        </span>
                    </p>
                </div>
            </div>

        </div><!-- .ggmc-settings-main -->

        <div class="ggmc-settings-help">
            <div class="card">
                <h3><?php esc_html_e( 'About These Cookies', 'ggm-commerce' ); ?></h3>
                <p>
                    <?php esc_html_e( 'All three cookies used by Goose Commerce are classified as "Functional" (also called "Necessary" or "Strictly Necessary" in some consent tools).', 'ggm-commerce' ); ?>
                </p>
                <p>
                    <?php esc_html_e( 'Functional cookies are generally exempt from consent requirements under GDPR and similar regulations, as they are essential for features the visitor has explicitly requested (comparing products, saving a wishlist, viewing recently browsed items).', 'ggm-commerce' ); ?>
                </p>
                <p>
                    <?php esc_html_e( 'However, you should still declare them in your cookie policy for transparency. Use the copy button to add them to your consent plugin.', 'ggm-commerce' ); ?>
                </p>
                <h3><?php esc_html_e( 'No Personal Data', 'ggm-commerce' ); ?></h3>
                <p>
                    <?php esc_html_e( 'These cookies store only product ID numbers (e.g. [12, 45, 78]). They do not contain any personal information, session tokens, or tracking identifiers.', 'ggm-commerce' ); ?>
                </p>
            </div>
        </div><!-- .ggmc-settings-help -->

    </div><!-- .ggmc-settings-layout -->

    <script>
    document.getElementById('ggmc-copy-cookie-text').addEventListener('click', function() {
        var textarea = document.getElementById('ggmc-cookie-text');
        var status = document.getElementById('ggmc-copy-status');
        navigator.clipboard.writeText(textarea.value).then(function() {
            status.style.display = 'inline';
            setTimeout(function() { status.style.display = 'none'; }, 2000);
        });
    });
    </script>
    <?php
}
