<?php
/**
 * Activator first-activation option seed test.
 *
 * @package Ppok\AIToolkit\Tests
 * @license GPL-2.0-or-later
 */

namespace Ppok\AIToolkit\Tests;

use Ppok\AIToolkit\Activator;

class Test_Activator extends \WP_UnitTestCase {

	public function set_up(): void {
		parent::set_up();
		// Tests in this class mutate the singleton option; reset to the activator-seeded shape
		// so each test starts from the canonical defaults regardless of declaration order.
		update_option( Activator::OPTION_KEY, Activator::default_settings() );
	}

	public function test_default_settings_uses_v1_keys(): void {
		$defaults = Activator::default_settings();

		$this->assertSame(
			array(
				'abilities_enabled',
				'write_post_types',
				'write_post_statuses',
				'block_allowlist_mode',
				'block_curated_allowlist',
				'log_retention_days',
			),
			array_keys( $defaults )
		);

		$this->assertSame( array(), $defaults['abilities_enabled'] );
		$this->assertSame( array( 'post' ), $defaults['write_post_types'] );
		$this->assertSame( array( 'draft' ), $defaults['write_post_statuses'] );
		$this->assertSame( 'registered', $defaults['block_allowlist_mode'] );
		$this->assertSame( array(), $defaults['block_curated_allowlist'] );
		$this->assertSame( 90, $defaults['log_retention_days'] );
	}

	public function test_default_settings_drops_pre_option_a_keys(): void {
		$defaults = Activator::default_settings();

		foreach ( array( 'mcp_enabled', 'mcp_auth_method', 'mcp_allowed_caps', 'mcp_route_base', 'mcp_log_level' ) as $stale ) {
			$this->assertArrayNotHasKey( $stale, $defaults, "Stale pre-Option-A key '{$stale}' must not be seeded." );
		}
	}

	public function test_activation_seeds_v1_option_shape(): void {
		// The test bootstrap calls Activator::activate(); option should already exist.
		$option = get_option( Activator::OPTION_KEY );

		$this->assertIsArray( $option, 'Expected ppok_ai_toolkit_settings option to be seeded by activation.' );
		$this->assertArrayHasKey( 'abilities_enabled', $option );
		$this->assertArrayHasKey( 'log_retention_days', $option );
		$this->assertArrayHasKey( 'block_allowlist_mode', $option );
		$this->assertSame( 90, $option['log_retention_days'] );
	}

	public function test_activation_does_not_overwrite_existing_settings(): void {
		// Replace seeded settings with a custom-edited value, re-activate, confirm intact.
		update_option(
			Activator::OPTION_KEY,
			array(
				'abilities_enabled'       => array( 'ppok/rest-call' ),
				'write_post_types'        => array( 'page' ),
				'write_post_statuses'     => array( 'draft', 'publish' ),
				'block_allowlist_mode'    => 'curated',
				'block_curated_allowlist' => array( 'core/paragraph' ),
				'log_retention_days'      => 30,
			)
		);

		Activator::activate();

		$option = get_option( Activator::OPTION_KEY );
		$this->assertSame( array( 'ppok/rest-call' ), $option['abilities_enabled'] );
		$this->assertSame( 'curated', $option['block_allowlist_mode'] );
		$this->assertSame( 30, $option['log_retention_days'] );
	}
}
