@page "/admin/support"
@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities
@using Microsoft.EntityFrameworkCore
@using Moonlight.App.Database
@using Moonlight.App.Services

@inject SupportMessageRepository SupportMessageRepository
@inject ConfigService ConfigService
@inject MessageService MessageService
@implements IDisposable

<OnlyAdmin>
    <LazyLoader @ref="LazyLoader" Load="Load">
        <div class="card">
            <div class="card-body">
                <div class="d-flex flex-column flex-xl-row p-5 pb-0">
                    <div class="flex-lg-row-fluid me-xl-15 mb-20 mb-xl-0">
                        <div class="mb-0">
                            <h1 class="text-dark mb-6">
                                <TL>Open tickets</TL>
                            </h1>
                            <div class="separator"></div>
                            <div class="mb-5">
                                @if (Users.Any())
                                {
                                    foreach (var user in Users)
                                    {
                                        <div class="d-flex mt-3 mb-3 ms-2 me-2">
                                            <table>
                                                <tr>
                                                    <td rowspan="2">
                                                        <span class="svg-icon svg-icon-2x me-5 ms-n1 svg-icon-success">
                                                            <i class="text-primary bx bx-md bx-message-dots"></i>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <a href="/admin/support/view/@(user.Id)" class="text-dark text-hover-primary fs-4 me-3 fw-semibold">
                                                            @(user.FirstName) @(user.LastName)
                                                        </a>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <span class="text-muted fw-semibold fs-6">
                                                            @{
                                                                var lastMessage = MessageCache.ContainsKey(user) ? MessageCache[user] : null;
                                                            }

                                                            @if (lastMessage == null)
                                                            {
                                                                <TL>No message sent yet</TL>
                                                            }
                                                            else
                                                            {
                                                                @(lastMessage.Message)
                                                            }
                                                        </span>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                        <div class="separator"></div>
                                    }
                                }
                                else
                                {
                                    <div class="alert alert-info">
                                        <TL>No support ticket is currently open</TL>
                                    </div>
                                }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </LazyLoader>
</OnlyAdmin>

@code
{
    private User[] Users;
    private Dictionary<User, SupportMessage?> MessageCache;

    private LazyLoader? LazyLoader;

    protected override void OnInitialized()
    {
        MessageCache = new();
        
        MessageService.Subscribe<Index, User>("support.new", this, async user =>
        {
            if (LazyLoader != null)
                await LazyLoader.Reload();
        });
        
        MessageService.Subscribe<Index, User>("support.close", this, async user =>
        {
            if (LazyLoader != null)
                await LazyLoader.Reload();
        });
    }

    private Task Load(LazyLoader arg)
    {
        // We dont want cache here
        Users = (new UserRepository(new DataContext(ConfigService)))
            .Get()
            .Where(x => x.SupportPending)
            .ToArray();

        foreach (var user in Users)
        {
            var lastMessage = SupportMessageRepository
                .Get()
                .Include(x => x.Recipient)
                .OrderByDescending(x => x.Id)
                .FirstOrDefault(x => x.Recipient!.Id == user.Id);

            MessageCache.Add(user, lastMessage);
        }

        return Task.CompletedTask;
    }

    public void Dispose()
    {
        MessageService.Unsubscribe("support.new", this);
        MessageService.Unsubscribe("support.close", this);
    }
}