<?php

namespace WPSN\Modules\Stability;

use WPSN\Core\AbstractModule;
use WPSN\Core\Capabilities;
use WPSN\Modules\Stability\Admin\SnapshotsAdmin;
use WPSN\Modules\Stability\Admin\CanonicalMatrixAdmin;

defined('ABSPATH') || exit;

/**
 * Stability tools:
 * - Config snapshots + rollback
 * - Head output snapshots + update diffs
 */
final class StabilityModule extends AbstractModule
{
    public function id(): string
    {
        return 'stability';
    }

    

public function name(): string
{
    return __('Stability', 'wp-seo-next');
}

public function description(): string
{
    return __('Snapshots, diffs and rollback tools (config + head output) for safer updates.', 'wp-seo-next');
}public function defaultEnabled(): bool
    {
        return true;
    }

    public function register(): void
    {
        // DB table.
        add_action('init', [SnapshotsInstaller::class, 'maybeInstall'], 1);

        // Admin UI.
        if (is_admin()) {
            (new SnapshotsAdmin($this->container))->register();
            (new CanonicalMatrixAdmin($this->container->get('settings')))->register();

            add_action('admin_notices', [$this, 'maybeShowHeadDiffNotice']);
            add_action('admin_post_wpsn_head_diff_dismiss', [$this, 'handleDismissHeadDiffNotice']);
        }
    }

    public function boot(): void
    {
        // no-op
    }

    public function maybeShowHeadDiffNotice(): void
    {
        if (!Capabilities::canManageTools()) {
            return;
        }

        $notice = get_option(HeadDiffService::OPTION_NOTICE);
        if (!is_array($notice) || empty($notice['changed'])) {
            return;
        }

        $from = isset($notice['from_version']) ? (string) $notice['from_version'] : '';
        $to = isset($notice['to_version']) ? (string) $notice['to_version'] : '';
        $when = isset($notice['created_at']) ? (string) $notice['created_at'] : '';

        $count = 0;
        if (isset($notice['changed']) && is_array($notice['changed'])) {
            $count = count($notice['changed']);
        }

        $msg = sprintf(
            /* translators: 1: old version, 2: new version, 3: count */
            __('WP SEO Next detected changes in generated head output after update (%1$s → %2$s). Changed cases: %3$d. Review the diff before deploying further.', 'wp-seo-next'),
            $from !== '' ? $from : '?',
            $to !== '' ? $to : (string) WPSN_VERSION,
            $count
        );

        $pageUrl = admin_url('tools.php?page=wpsn-snapshots&tab=head');
        $dismissUrl = wp_nonce_url(admin_url('admin-post.php?action=wpsn_head_diff_dismiss'), 'wpsn_head_diff_dismiss');

        echo '<div class="notice notice-warning"><p><strong>' . esc_html__('Head output changed after update', 'wp-seo-next') . '</strong><br />' . esc_html($msg) . '</p>';
        echo '<p><a class="button button-primary" href="' . esc_url($pageUrl) . '">' . esc_html__('Review snapshots', 'wp-seo-next') . '</a> ';
        echo '<a class="button" href="' . esc_url($dismissUrl) . '">' . esc_html__('Dismiss', 'wp-seo-next') . '</a></p>';
        if ($when !== '') {
            echo '<p class="description">' . esc_html(sprintf(__('Detected at: %s (UTC)', 'wp-seo-next'), $when)) . '</p>';
        }
        echo '</div>';
    }

    public function handleDismissHeadDiffNotice(): void
    {
        if (!Capabilities::canManageTools()) {
            wp_die(esc_html__('Permission denied.', 'wp-seo-next'));
        }

        check_admin_referer('wpsn_head_diff_dismiss');

        delete_option(HeadDiffService::OPTION_NOTICE);

        $url = admin_url('tools.php?page=wpsn-snapshots&tab=head');
        wp_safe_redirect($url);
        exit;
    }
}
