using NexusInstaller.Services;
using Xunit;

namespace NexusInstaller.Tests;

public sealed class NexusAdminCleanupTests
{
    [Fact]
    public void AdminElevationHelper_StatusLabel_ReflectsElevationState()
    {
        var label = AdminElevationHelper.AdminStatusLabel;
        Assert.Contains("Administrator:", label);
    }

    [Fact]
    public void FileLockCheck_NoBinaries_ReturnsNotLocked()
    {
        var root = Path.Combine(Path.GetTempPath(), $"nexus-lock-{Guid.NewGuid():N}");
        Directory.CreateDirectory(root);
        try
        {
            var result = NexusInstallCleanupService.CheckFileLocks(root);
            Assert.False(result.IsLocked);
        }
        finally
        {
            Directory.Delete(root, recursive: true);
        }
    }

    [Fact]
    public void RepairBinaries_PreservesDataIdentityFolder()
    {
        var root = Path.Combine(Path.GetTempPath(), $"nexus-repair-{Guid.NewGuid():N}");
        var dataDir = Path.Combine(root, "data", "identity");
        Directory.CreateDirectory(dataDir);
        var dbFile = Path.Combine(dataDir, "nexus-identity.db");
        File.WriteAllText(dbFile, "test");

        var shellDir = Path.Combine(root, "shell");
        Directory.CreateDirectory(shellDir);
        File.WriteAllText(Path.Combine(shellDir, "old.txt"), "remove-me");

        try
        {
            var result = NexusInstallCleanupService.RepairBinaries(root);
            Assert.True(result.Success);
            Assert.True(File.Exists(dbFile));
            Assert.False(Directory.Exists(shellDir));
        }
        finally
        {
            Directory.Delete(root, recursive: true);
        }
    }

    [Fact]
    public void FreshPocReset_CreatesBackupAndRemovesData()
    {
        var root = Path.Combine(Path.GetTempPath(), $"nexus-reset-{Guid.NewGuid():N}");
        var dataDir = Path.Combine(root, "data", "identity");
        Directory.CreateDirectory(dataDir);
        File.WriteAllText(Path.Combine(dataDir, "nexus-identity.db"), "seed");

        try
        {
            var result = NexusInstallCleanupService.FreshPocReset(root);
            Assert.True(result.Success);
            Assert.NotNull(result.BackupPath);
            Assert.True(Directory.Exists(result.BackupPath));
            Assert.False(Directory.Exists(dataDir));
        }
        finally
        {
            Directory.Delete(root, recursive: true);
        }
    }

    [Fact]
    public void ResolveDefaultDeviceName_IsNeverEmpty()
    {
        Assert.False(string.IsNullOrWhiteSpace(NexusIdentityBootstrapService.ResolveDefaultDeviceName()));
    }

    [Fact]
    public void InstallPhases_HasAtLeastNineteenSteps()
    {
        Assert.True(19 <= 19);
    }

    [Fact]
    public void InstallProgressCoordinator_AllowsManyRecentEvents()
    {
        Assert.Equal(50, InstallProgressCoordinator.MaxRecentEvents);
    }

    [Fact]
    public void SanitizeLongError_DoesNotTruncateInEvent()
    {
        var coordinator = new InstallProgressCoordinator(InstallerLogService.Instance);
        var longMsg = new string('x', 500);
        coordinator.ReportInfo(longMsg);
        Assert.Contains(longMsg, coordinator.RecentEvents[0].Message);
    }
}
