<?php
/**
 * Network bootstrap migration.
 *
 * @package NimblyMail\SMTP
 *
 * @since 1.0.0
 */

namespace NimblyMail\SMTP\Migration;

use NimblyMail\SMTP\Options;

defined( 'ABSPATH' ) || exit;

/**
 * Class NetworkBootstrap
 *
 * Runs on the first network activation of the plugin. Promotes the main
 * site's existing NimblyMail options into the network defaults so that
 * sub-sites inherit the working configuration immediately. Idempotent:
 * guarded by a `bootstrapped` flag in the network option.
 *
 * @since 1.0.0
 */
final class NetworkBootstrap {

    /**
     * Default shape of the network option.
     *
     * Kept here rather than reaching into Options so we can run the
     * migration even if the Options singleton has not yet been constructed
     * (e.g. during the activation hook before plugins_loaded).
     *
     * @since 1.0.0
     *
     * @var array
     */
    private const NETWORK_DEFAULTS = array(
        'values'           => array(),
        'locks'            => array(),
        'bootstrapped'     => false,
        'wizard_completed' => false,
        'db_version'       => '1.0.0',
    );

    /**
     * Promote the main site's options into the network option.
     *
     * No-op when:
     *   - The install is not multisite.
     *   - The network option already has `bootstrapped = true`.
     *
     * When the main site has existing options they become the network
     * defaults and the main site's row is deleted so site 1 inherits from
     * the network like every other site on the network.
     *
     * @since 1.0.0
     *
     * @return bool True if promotion ran, false if it was skipped.
     */
    public static function maybe_promote_site_1_settings(): bool {
        if ( ! is_multisite() ) {
            return false;
        }

        $stored = get_site_option( Options::NETWORK_OPTION_KEY, array() );
        if ( is_array( $stored ) && ! empty( $stored['bootstrapped'] ) ) {
            return false;
        }

        $network_state = array_merge(
            self::NETWORK_DEFAULTS,
            is_array( $stored ) ? $stored : array()
        );

        $main_id      = get_main_site_id();
        $main_options = get_blog_option( $main_id, Options::OPTION_KEY, array() );
        $had_options  = is_array( $main_options ) && ! empty( $main_options );

        if ( $had_options ) {
            $network_state['values'] = $main_options;
        }

        $network_state['bootstrapped'] = true;

        // Write the network option FIRST. Only delete the main-site row once
        // we've confirmed the network value is in place — otherwise an
        // update_site_option() failure would leave site 1 with no settings
        // anywhere on disk.
        $written = (bool) update_site_option( Options::NETWORK_OPTION_KEY, $network_state );

        if ( ! $written ) {
            return false;
        }

        if ( $had_options ) {
            $verification = get_site_option( Options::NETWORK_OPTION_KEY, array() );
            if ( is_array( $verification ) && ! empty( $verification['bootstrapped'] ) ) {
                delete_blog_option( $main_id, Options::OPTION_KEY );
            }
        }

        // Drop any cached Options singleton so subsequent reads reflect the migration.
        Options::reset();

        return true;
    }
}
