<?php

declare(strict_types=1);

namespace SiteFort\Modules\Cloud\Services;

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

use SiteFort\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, 'sitefort' ) ) {
            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( 'SiteFort detected that this site is running at a different address. Open the SiteFort 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( 'SiteFort is ready to complete this site replacement. Open the SiteFort plugin page to finish connecting this website to the replacement site record.' );
            echo '</p></div>';
            return;
        }

    }
}
