using System.Net;

namespace Tiku.IntegrationTests.Api;

public sealed class PlatformAdminStaticEndpointTests
{
    [Fact]
    public async Task Platform_admin_static_shell_is_served_without_api_authentication()
    {
        await using var factory = new ApiTestFactory();
        using var client = factory.CreateClient();

        var response = await client.GetAsync("/platform-admin/");
        var body = await response.Content.ReadAsStringAsync();

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.Contains("恭学题库 SaaS · 平台管理", body, StringComparison.Ordinal);
        Assert.Contains("./platform-api.js", body, StringComparison.Ordinal);
        Assert.Contains("./platform-auth.js", body, StringComparison.Ordinal);
        Assert.Contains("platformAuthGate", body, StringComparison.Ordinal);
    }

    [Fact]
    public async Task Platform_admin_short_path_redirects_to_static_shell_directory()
    {
        await using var factory = new ApiTestFactory();
        using var client = factory.CreateClient(new()
        {
            AllowAutoRedirect = false
        });

        var response = await client.GetAsync("/platform-admin");

        Assert.Equal(HttpStatusCode.MovedPermanently, response.StatusCode);
        Assert.Equal("/platform-admin/", response.Headers.Location?.AbsolutePath);
    }

    [Fact]
    public async Task Platform_admin_runtime_uses_real_api_without_mock_fallback()
    {
        await using var factory = new ApiTestFactory();
        using var client = factory.CreateClient();

        var runtime = await client.GetStringAsync("/platform-admin/runtime-config.js");
        var authentication = await client.GetStringAsync("/platform-admin/platform-auth.js");

        Assert.Contains("mode: 'api'", runtime, StringComparison.Ordinal);
        Assert.Contains("fallbackToMock: false", runtime, StringComparison.Ordinal);
        Assert.Contains("/api/auth/login/password", authentication, StringComparison.Ordinal);
        Assert.Contains("/api/auth/password/change-required", authentication, StringComparison.Ordinal);
        Assert.DoesNotContain("/api/auth/mfa", authentication, StringComparison.Ordinal);
    }
}
