<?php

declare(strict_types=1);

namespace SecurWP\Modules\Cloud\Services;

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

use SecurWP\Modules\Cloud\Service\CloudSettings;

class SiteMigrationService {
    public static function render_admin_notice(): void {
        if ( ! function_exists( 'current_user_can' ) || ! current_user_can( 'manage_options' ) ) {
            return;
        }

        $current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if ( str_starts_with( $current_page, 'securwp' ) ) {
            return;
        }

        $settings = CloudSettings::get_all();
        $status = (string) ( $settings['migration_status'] ?? 'none' );
        $pending_url = (string) ( $settings['migration_pending_url'] ?? '' );

        if ( $status === 'pending' ) {
            echo '<div class="notice notice-warning"><p>';
            echo esc_html( 'SecurWP detected that this site is running at a different address. Open the SecurWP plugin page to choose whether it should replace the previous site or be connected as a separate site.' );
            if ( $pending_url !== '' ) {
                echo ' ';
                echo esc_html( sprintf( 'Detected URL: %s', $pending_url ) );
            }
            echo '</p></div>';
            return;
        }

        if ( $status === 'approved_replace' ) {
            echo '<div class="notice notice-warning"><p>';
            echo esc_html( 'SecurWP is ready to complete this site replacement. Open the SecurWP plugin page to finish connecting this website to the replacement site record.' );
            echo '</p></div>';
            return;
        }

    }
}
