<?php
/**
 * Onboarding flag + admin notice that links to the wizard tab.
 *
 * @package Brainwerk SEO Suite
 */

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

class Seoforge_Onboarding {

    public static function register( Seoforge_Loader $loader ) {
        $loader->add_action( 'admin_notices', __CLASS__, 'maybe_notice' );
        $loader->add_action( 'network_admin_notices', __CLASS__, 'maybe_notice' );
    }

    public static function is_pending() {
        return (bool) get_site_option( 'seoforge_onboarding_pending', 0 );
    }

    public static function complete() {
        update_site_option( 'seoforge_onboarding_pending', 0 );
    }

    public static function maybe_notice() {
        $cap_ok = current_user_can( 'manage_options' ) || current_user_can( 'manage_network_options' );
        if ( ! $cap_ok ) {
            return;
        }
        if ( self::is_pending() ) {
            $url = is_network_admin()
                ? network_admin_url( 'admin.php?page=brainwerk-seo-suite&tab=onboarding' )
                : admin_url( 'admin.php?page=brainwerk-seo-suite&tab=onboarding' );
            echo '<div class="notice notice-info"><p>';
            echo esc_html__( 'Welcome to Brainwerk SEO Suite! Run the quick setup to enable analytics + SEO defaults.', 'brainwerk-seo-suite' );
            echo ' <a class="button button-primary" href="' . esc_url( $url ) . '">' . esc_html__( 'Start setup', 'brainwerk-seo-suite' ) . '</a>';
            echo '</p></div>';
        }
        // Coexistence notice — only on Brainwerk SEO Suite admin pages.
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        if ( $screen && false !== strpos( (string) $screen->id, 'brainwerk-seo-suite' ) ) {
            $det = Seoforge_Compat::detect_competitor();
            if ( $det['active'] && ! Seoforge_Settings::get( 'seo_force_takeover', false ) ) {
                echo '<div class="notice notice-warning"><p>';
                printf(
                    /* translators: %s competitor plugin name */
                    esc_html__( '%s is active. Brainwerk SEO Suite has stood down its meta tags, schema and sitemap to avoid duplicates. Visitor analytics + 404 log + on-page analyzer continue to work.', 'brainwerk-seo-suite' ),
                    '<strong>' . esc_html( $det['name'] ) . '</strong>'
                );
                echo ' <a href="' . esc_url( admin_url( 'admin.php?page=brainwerk-seo-suite&tab=meta' ) ) . '">' . esc_html__( 'Override / take over →', 'brainwerk-seo-suite' ) . '</a>';
                echo '</p></div>';
            }
        }
    }
}
