// Copyright © Selmo Technology GmbH. All Rights Reserved

using Novus.Shared.Types.Extensions;
using SelmoStudio.Hmi.Server.AuditTrail.Model;

namespace SelmoStudio.Hmi.Server.TestSetup
{
    public class TestAuditTrailBuilder
    {
        private int _totalNumberOfAuditTrailEntries;
        private Random _random;
        private static string[] _users = { "Admin", "SuperUser", "User" };
        private static AuditTrailClass _auditTrail;

        internal TestAuditTrailBuilder()
        {
            _auditTrail = new()
            {
                Id = Guid.NewGuid(),
                Name = "Audit trail object",
                Entries = new List<AuditTrailEntry>()
            };

            _random = new Random();
        }

        internal TestAuditTrailBuilder WithEntries(int totalNumber)
        {
            _totalNumberOfAuditTrailEntries = totalNumber;

            return this;
        }

        internal AuditTrailClass Build()
        {
            for (var i = 1; i <= _totalNumberOfAuditTrailEntries; i++)
            {
                _auditTrail.Entries.Add(GetAuditTrailEntry(i));
            }

            return _auditTrail;
        }

        private AuditTrailEntry GetAuditTrailEntry(int changedValue)
        {
            var id = Guid.NewGuid();

            AuditTrailEntry entry = new()
            {
                Id = id,
                Description = $"Value with id {id} was changed to {100 + changedValue}",
                User = GetRandomUser(),
                TimeStamp = DateTime.Now,
            };

            return entry;
        }

        private string GetRandomUser()
        {
            return _users[_random.Next(_users.Length)];
        }
    }
}