<?php
declare(strict_types=1);

namespace ProRank\SEO\Integrations\SeedProd;

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

/**
 * SeedProd integration marker.
 *
 * SeedProd's Landing Page Builder is a standalone app rendered at admin URLs
 * such as ?page=seedprod_lite_template&id=X and
 * ?page=seedprod_lite_builder&id=X. It deliberately does NOT load the
 * standard WordPress admin chrome inside the builder frame, so the editor
 * sidebar/meta-box does not appear there. We do not
 * mount a floating ProRank SEO panel inside the SeedProd builder in v1 —
 * its enqueue hooks are not source-confirmed for ProRank yet, and any
 * extra scripts loaded in the builder iframe risk breaking SeedProd's
 * own runtime.
 *
 * Read-only responsibilities active today:
 *   - Mark SeedProd as detected for compatibility logic.
 *   - Expose helpers other modules can call to know if the current request
 *     is the SeedProd builder (so they can avoid optimization).
 *
 * SeedProd-managed landing pages are marked with _seedprod_page metadata and
 * may live as WordPress pages or in the seedprod post type.
 * Content extraction is handled by BuilderContentSupport, not by this
 * class — keeping the integration surface narrow until a stable builder
 * mount hook is identified.
 */
class SeedProdIntegration {

    private static ?self $instance = null;

    public static function get_instance(): self {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        if (!self::is_seedprod_active()) {
            return;
        }
    }

    /**
     * SeedProd is loaded when any of its public surface is present.
     */
    public static function is_seedprod_active(): bool {
        return defined('SEEDPROD_VERSION')
            || defined('SEEDPROD_PRO_VERSION')
            || defined('SEEDPROD_SLUG')
            || defined('SEEDPROD_PLUGIN_VERSION')
            || class_exists('SeedProd_Lite_Init')
            || class_exists('SeedProd_Pro')
            || class_exists('SeedProd_Settings');
    }

    /**
     * Detect SeedProd builder admin requests.
     *
     * SeedProd Lite uses seedprod_lite_template after template selection and
     * seedprod_lite_builder for the direct builder route.
     */
    public static function is_builder_request(): bool {
        if (!is_admin()) {
            return false;
        }
        // phpcs:disable WordPress.Security.NonceVerification.Recommended
        $page = isset($_GET['page']) ? (string) $_GET['page'] : '';
        $action = isset($_GET['seedprod_action']) ? (string) $_GET['seedprod_action'] : '';
        // phpcs:enable
        if ($page === '') {
            return false;
        }
        if (in_array($page, ['seedprod_lite_template', 'seedprod_lite_builder'], true)) {
            return true;
        }
        if (strpos($page, 'seedprod') !== 0) {
            return false;
        }
        return in_array($action, ['builder', 'edit'], true);
    }
}
