// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0

using System;
using GFramework.Core.Abstractions.Enums;

namespace GFramework.Core.Tests.Architectures;

/// <summary>
/// RegistryInitializationHookBase 抽象基类的单元测试
/// 测试内容包括：
/// - 在目标阶段正确触发配置注册
/// - 在非目标阶段不触发配置注册
/// - 正确遍历所有配置项
/// - 注册表不存在时不抛出异常
/// - 支持自定义目标阶段
/// </summary>
[TestFixture]
public class RegistryInitializationHookBaseTests
{
    /// <summary>
    /// 测试在目标阶段时是否正确触发配置注册
    /// </summary>
    [Test]
    public void OnPhase_Should_Register_Configs_At_Target_Phase()
    {
        var registry = new TestRegistry();
        var configs = new[] { "config1", "config2", "config3" };
        var hook = new TestRegistryInitializationHook(configs);
        var architecture = new TestArchitectureWithRegistry(registry);

        hook.OnPhase(ArchitecturePhase.AfterSystemInit, architecture);

        Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(3));
        Assert.That(registry.RegisteredConfigs, Is.EquivalentTo(configs));
    }

    /// <summary>
    /// 测试在非目标阶段时不触发配置注册
    /// </summary>
    [Test]
    public void OnPhase_Should_Not_Register_Configs_At_Wrong_Phase()
    {
        var registry = new TestRegistry();
        var configs = new[] { "config1", "config2" };
        var hook = new TestRegistryInitializationHook(configs);
        var architecture = new TestArchitectureWithRegistry(registry);

        hook.OnPhase(ArchitecturePhase.BeforeSystemInit, architecture);

        Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(0));
    }

    /// <summary>
    /// 测试支持自定义目标阶段
    /// </summary>
    [Test]
    public void OnPhase_Should_Support_Custom_Target_Phase()
    {
        var registry = new TestRegistry();
        var configs = new[] { "config1" };
        var hook = new TestRegistryInitializationHook(configs, ArchitecturePhase.AfterModelInit);
        var architecture = new TestArchitectureWithRegistry(registry);

        hook.OnPhase(ArchitecturePhase.AfterModelInit, architecture);

        Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(1));
    }

    /// <summary>
    /// 测试当注册表不存在时不抛出异常
    /// </summary>
    [Test]
    public void OnPhase_Should_Not_Throw_When_Registry_Not_Found()
    {
        var configs = new[] { "config1" };
        var hook = new TestRegistryInitializationHook(configs);
        var architecture = new TestArchitectureWithoutRegistry();

        Assert.DoesNotThrow(() => hook.OnPhase(ArchitecturePhase.AfterSystemInit, architecture));
    }

    /// <summary>
    /// 测试空配置集合不会导致错误
    /// </summary>
    [Test]
    public void OnPhase_Should_Handle_Empty_Configs()
    {
        var registry = new TestRegistry();
        var configs = Array.Empty<string>();
        var hook = new TestRegistryInitializationHook(configs);
        var architecture = new TestArchitectureWithRegistry(registry);

        hook.OnPhase(ArchitecturePhase.AfterSystemInit, architecture);

        Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(0));
    }

    /// <summary>
    /// 测试多次调用同一阶段会重复注册
    /// </summary>
    [Test]
    public void OnPhase_Should_Register_Multiple_Times_If_Called_Multiple_Times()
    {
        var registry = new TestRegistry();
        var configs = new[] { "config1" };
        var hook = new TestRegistryInitializationHook(configs);
        var architecture = new TestArchitectureWithRegistry(registry);

        hook.OnPhase(ArchitecturePhase.AfterSystemInit, architecture);
        hook.OnPhase(ArchitecturePhase.AfterSystemInit, architecture);

        Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(2));
    }
}
