<?php

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

function wnc_render_pro_page() {
    if (!current_user_can('manage_options')) {
        return;
    }

    $pro_active = function_exists('wnc_is_pro_active') && wnc_is_pro_active();

    if (!$pro_active) {
        $license_url = function_exists('wnc_get_license_activation_url')
            ? wnc_get_license_activation_url()
            : admin_url('admin.php?page=where-next');
        ?>
        <div class="wrap">
            <h1>Where Next Connector Pro</h1>

            <div class="card" style="max-width: 720px; margin-top: 20px; padding: 24px;">
                <h2>Pro is not active on this site</h2>
                <p>
                    Activate a valid Pro licence to unlock automatic button placement,
                    display and device controls, desktop QR codes, and click analytics.
                </p>
                <p>
                    <a class="button button-primary" href="<?php echo esc_url($license_url); ?>">
                        Enter licence key
                    </a>
                    <a class="button" href="<?php echo esc_url(wnc_get_upgrade_url()); ?>">
                        View Pro plans
                    </a>
                </p>
                <p class="description">
                    If you have already generated a licence, select <strong>Enter licence key</strong>
                    and activate it for this WordPress site.
                </p>
            </div>
        </div>
        <?php
        return;
    }

    $options = wnc_get_options();
    ?>
    <div class="wrap">
        <h1>Where Next Pro Features</h1>

        <p>Configure premium display, placement, device and QR features.</p>

        <form method="post" action="options.php">
            <?php settings_fields('wnc_settings'); ?>
            <input type="hidden" name="wnc_options[_form_scope]" value="pro">

            <h2>Automatic Placement</h2>

            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Enable automatic placement</th>
                    <td>
                        <label>
                            <input type="checkbox" name="wnc_options[pro_auto_place]" value="1" <?php checked(!empty($options['pro_auto_place'])); ?>>
                            Automatically add buttons
                        </label>
                    </td>
                </tr>

                <tr>
                    <th scope="row">Content Types</th>
                    <td>
                        <label><input type="checkbox" name="wnc_options[pro_auto_locations]" value="1" <?php checked(!empty($options['pro_auto_locations'])); ?>> Locations</label><br>
                        <label><input type="checkbox" name="wnc_options[pro_auto_posts]" value="1" <?php checked(!empty($options['pro_auto_posts'])); ?>> Posts</label><br>
                        <label><input type="checkbox" name="wnc_options[pro_auto_pages]" value="1" <?php checked(!empty($options['pro_auto_pages'])); ?>> Pages</label><br>
                        <label><input type="checkbox" name="wnc_options[pro_auto_events]" value="1" <?php checked(!empty($options['pro_auto_events'])); ?>> Events</label>
                    </td>
                </tr>
            </table>

            <h2>Display</h2>

            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Button position</th>
                    <td>
                        <select name="wnc_options[pro_button_position]">
                            <option value="inline" <?php selected($options['pro_button_position'] ?? '', 'inline'); ?>>Inline</option>
                            <option value="sticky-bottom" <?php selected($options['pro_button_position'] ?? '', 'sticky-bottom'); ?>>Sticky bottom</option>
                            <option value="floating-left" <?php selected($options['pro_button_position'] ?? '', 'floating-left'); ?>>Floating left</option>
                            <option value="floating-right" <?php selected($options['pro_button_position'] ?? '', 'floating-right'); ?>>Floating right</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th scope="row">Template</th>
                    <td>
                        <select name="wnc_options[pro_template]">
                            <option value="default" <?php selected($options['pro_template'] ?? '', 'default'); ?>>Default</option>
                            <option value="compact" <?php selected($options['pro_template'] ?? '', 'compact'); ?>>Compact</option>
                            <option value="wide" <?php selected($options['pro_template'] ?? '', 'wide'); ?>>Wide</option>
                            <option value="card" <?php selected($options['pro_template'] ?? '', 'card'); ?>>Card</option>
                        </select>
                    </td>
                </tr>
            </table>

            <h2>Device Targeting</h2>

            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Show button on</th>
                    <td>
                        <select name="wnc_options[pro_device_targeting]">
                            <option value="all" <?php selected($options['pro_device_targeting'] ?? '', 'all'); ?>>All devices</option>
                            <option value="mobile" <?php selected($options['pro_device_targeting'] ?? '', 'mobile'); ?>>Mobile only</option>
                            <option value="desktop" <?php selected($options['pro_device_targeting'] ?? '', 'desktop'); ?>>Desktop only</option>
                        </select>
                    </td>
                </tr>
            </table>

            <h2>Desktop QR</h2>

            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">QR code</th>
                    <td>
                        <label>
                            <input type="checkbox" name="wnc_options[pro_show_desktop_qr]" value="1" <?php checked(!empty($options['pro_show_desktop_qr'])); ?>>
                            Show QR code on desktop
                        </label>

                        <br><br>

                        <select name="wnc_options[pro_qr_size]">
                            <option value="120" <?php selected($options['pro_qr_size'] ?? '', '120'); ?>>120px</option>
                            <option value="180" <?php selected($options['pro_qr_size'] ?? '', '180'); ?>>180px</option>
                            <option value="240" <?php selected($options['pro_qr_size'] ?? '', '240'); ?>>240px</option>
                        </select>
                    </td>
                </tr>
            </table>

            <h2>Analytics</h2>

            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Click tracking</th>
                    <td>
                        <label>
                            <input type="checkbox" name="wnc_options[pro_enable_analytics]" value="1" <?php checked(!empty($options['pro_enable_analytics'])); ?>>
                            Enable click tracking
                        </label>
                        <p class="description">Records button clicks, content ID, page URL, time, device category and button mode. It does not store IP addresses, cookies or full user-agent strings.</p>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Data retention</th>
                    <td>
                        <select name="wnc_options[pro_analytics_retention_days]">
                            <option value="90" <?php selected((int) ($options['pro_analytics_retention_days'] ?? 365), 90); ?>>90 days</option>
                            <option value="180" <?php selected((int) ($options['pro_analytics_retention_days'] ?? 365), 180); ?>>180 days</option>
                            <option value="365" <?php selected((int) ($options['pro_analytics_retention_days'] ?? 365), 365); ?>>1 year</option>
                            <option value="730" <?php selected((int) ($options['pro_analytics_retention_days'] ?? 365), 730); ?>>2 years</option>
                            <option value="0" <?php selected((int) ($options['pro_analytics_retention_days'] ?? 365), 0); ?>>Keep indefinitely</option>
                        </select>
                    </td>
                </tr>
            </table>

            <?php submit_button('Save Pro Settings'); ?>
        </form>
    </div>
    <?php
}
