@using Moonlight.App.Database.Entities.LogsEntries
@using Moonlight.App.Helpers
@using Moonlight.App.Repositories
@using Newtonsoft.Json
@using Moonlight.App.Database.Entities
@using Moonlight.App.Models.Log

@inject UserRepository UserRepository

<div class="timeline-item">
    <div class="timeline-line w-40px"></div>
    <div class="timeline-icon symbol symbol-circle symbol-40px">
        <div class="symbol-label bg-light">
            <i class="bx bx-md bx-log-in"></i>
        </div>
    </div>
    <div class="timeline-content mb-10 mt-n2">
        <div class="overflow-auto pe-3">
            <div class="fs-5 fw-semibold mb-2">
                @if (User == null)
                {
                    <TL>Password change for</TL> @(Data[0].Value)
                }
                else
                {
                    <TL>Password change for</TL> <a href="/admin/users/view/@(User.Id)">@(User.Email)</a>
                }
            </div>
            <div class="d-flex align-items-center mt-1 fs-6">
                <div class="text-muted me-2 fs-7">@(Formatter.FormatDate(Entry.CreatedAt)), @(Entry.System ? "System" : Entry.Ip)</div>
            </div>
        </div>
    </div>
</div>

@code
{
    [Parameter]
    public AuditLogEntry Entry { get; set; }

    private User? User;
    private LogData[] Data;

    protected override void OnInitialized()
    {
        Data = JsonConvert.DeserializeObject<LogData[]>(Entry.JsonData)!;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            User = UserRepository.Get().FirstOrDefault(x => x.Email == Data[0].Value);

            await InvokeAsync(StateHasChanged);
        }
    }
}