<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- admin view partial; local variables are not global

if ( ! current_user_can( 'manage_options' ) ) {
    wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'scholar-sync' ) );
}

// ---- Handle revoke consent ----
if ( isset( $_POST['scholar_sync_revoke_consent'] ) ) {
    check_admin_referer( 'scholar_sync_revoke_consent' );
    update_option( 'scholar_sync_service_consent', false );
    require_once SCHOLAR_SYNC_PLUGIN_DIR . '/includes/class-scholar-sync-remote.php';
    delete_option( ScholarSync_Remote::$CUSTOMER_API_KEY );
    echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Consent revoked. Your API key has been removed. The plugin will no longer contact scholar-sync.org.', 'scholar-sync' ) . '</p></div>';
}

// ---- Handle re-sync plan ----
if ( isset( $_POST['scholar_sync_sync_plan'] ) ) {
    check_admin_referer( 'scholar_sync_sync_plan' );
    require_once SCHOLAR_SYNC_PLUGIN_DIR . '/includes/class-scholar-sync-activator.php';
    ScholarSync_Activator::notify_plan_update();
    echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Plan status has been sent to scholar-sync.org. Your new plan limits should take effect immediately.', 'scholar-sync' ) . '</p></div>';
}

// ---- Load API key ----
require_once SCHOLAR_SYNC_PLUGIN_DIR . '/includes/class-scholar-sync-remote.php';
$consent = (bool) get_option( 'scholar_sync_service_consent', false );
$apikey  = get_option( ScholarSync_Remote::$CUSTOMER_API_KEY, '' );
?>
<div class="wrap">
    <h1><?php esc_html_e( 'Scholar Sync Settings', 'scholar-sync' ); ?></h1>

    <!-- ================================================================
         Section 1: External Service Disclosure
         ================================================================ -->
    <h2><?php esc_html_e( 'External Service', 'scholar-sync' ); ?></h2>
    <p>
        <?php
        printf(
            /* translators: 1: scholar-sync.org link, 2: Terms of Service link, 3: Privacy Policy link */
            esc_html__( 'Scholar Sync connects to the %1$s cloud service to sync and display publication data. Upon connection, your site URL and a unique API key are used to authenticate your requests. Please review our %2$s and %3$s before connecting.', 'scholar-sync' ),
            '<a href="https://scholar-sync.org" target="_blank" rel="noopener">scholar-sync.org</a>',
            '<a href="https://scholar-sync.org/terms" target="_blank" rel="noopener">' . esc_html__( 'Terms of Service', 'scholar-sync' ) . '</a>',
            '<a href="https://scholar-sync.org/privacy" target="_blank" rel="noopener">' . esc_html__( 'Privacy Policy', 'scholar-sync' ) . '</a>'
        );
        ?>
    </p>

    <table class="form-table" role="presentation">
        <tbody>
            <tr>
                <th scope="row"><?php esc_html_e( 'Connection Status', 'scholar-sync' ); ?></th>
                <td>
                    <?php if ( $consent ) : ?>
                        <span style="color:green;font-weight:bold;">&#10003; <?php esc_html_e( 'Connected', 'scholar-sync' ); ?></span>
                        <form method="post" action="" style="margin-top:8px;">
                            <?php wp_nonce_field( 'scholar_sync_revoke_consent' ); ?>
                            <button type="submit" name="scholar_sync_revoke_consent" class="button button-secondary"
                                onclick="return confirm('<?php esc_attr_e( 'This will remove your API key and disconnect from scholar-sync.org. Are you sure?', 'scholar-sync' ); ?>')">
                                <?php esc_html_e( 'Revoke Consent &amp; Disconnect', 'scholar-sync' ); ?>
                            </button>
                        </form>
                    <?php else : ?>
                        <span style="color:#d63638;font-weight:bold;">&#10007; <?php esc_html_e( 'Not connected', 'scholar-sync' ); ?></span>
                        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:8px;">
                            <?php wp_nonce_field( 'scholar_sync_consent', 'scholar_sync_consent_nonce' ); ?>
                            <input type="hidden" name="action" value="scholar_sync_consent">
                            <button type="submit" class="button button-primary">
                                <?php esc_html_e( 'I agree — Connect to scholar-sync.org', 'scholar-sync' ); ?>
                            </button>
                        </form>
                        <p class="description"><?php esc_html_e( 'Your site URL and a unique API key will be sent to scholar-sync.org to authenticate your requests.', 'scholar-sync' ); ?></p>
                    <?php endif; ?>
                </td>
            </tr>
        </tbody>
    </table>

    <!-- ================================================================
         Section 2: API Key
         ================================================================ -->
    <h2><?php esc_html_e( 'API Key', 'scholar-sync' ); ?></h2>
    <table class="form-table" role="presentation">
        <tbody>
            <tr>
                <th scope="row"><?php esc_html_e( 'Your API Key', 'scholar-sync' ); ?></th>
                <td>
                    <?php if ( $apikey ) : ?>
                        <code><?php echo esc_html( $apikey ); ?></code>
                        <p class="description"><?php esc_html_e( 'This key is auto-generated and sent with every request to scholar-sync.org to authenticate your site.', 'scholar-sync' ); ?></p>
                    <?php else : ?>
                        <em><?php esc_html_e( 'No API key yet. Connect to scholar-sync.org first.', 'scholar-sync' ); ?></em>
                    <?php endif; ?>
                </td>
            </tr>
        </tbody>
    </table>

    <!-- ================================================================
         Section 3: Plan / Freemius
         ================================================================ -->
    <h2><?php esc_html_e( 'Plan', 'scholar-sync' ); ?></h2>
    <table class="form-table" role="presentation">
        <tbody>
            <tr>
                <th scope="row"><?php esc_html_e( 'Your Plan', 'scholar-sync' ); ?></th>
                <td>
                    <?php if ( function_exists( 'scholar_sync_fs' ) ) : ?>
                        <?php
                        $fs = scholar_sync_fs();
                        if ( $fs->is_paying() ) {
                            echo '<strong>' . esc_html__( 'Premium', 'scholar-sync' ) . '</strong>';
                        } else {
                            echo '<strong>' . esc_html__( 'Free', 'scholar-sync' ) . '</strong>';
                            echo ' &mdash; <a href="' . esc_url( $fs->get_upgrade_url() ) . '">' . esc_html__( 'Upgrade to Premium', 'scholar-sync' ) . '</a>';
                        }
                        ?>
                        <form method="post" action="" style="margin-top:8px;">
                            <?php wp_nonce_field( 'scholar_sync_sync_plan' ); ?>
                            <button type="submit" name="scholar_sync_sync_plan" class="button button-secondary" <?php disabled( ! $consent ); ?>>
                                <?php esc_html_e( 'Re-sync Plan with Service', 'scholar-sync' ); ?>
                            </button>
                            <p class="description"><?php esc_html_e( 'Use this if you recently upgraded but the service still shows your old plan limits.', 'scholar-sync' ); ?></p>
                        </form>
                    <?php else : ?>
                        <em><?php esc_html_e( 'Plan information unavailable.', 'scholar-sync' ); ?></em>
                    <?php endif; ?>
                </td>
            </tr>
        </tbody>
    </table>
</div>