@page "/admin"
@using Moonlight.App.Repositories.Servers
@using Moonlight.App.Repositories
@using Moonlight.App.Repositories.Domains
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Models.Misc
@using Moonlight.App.Services
@using Newtonsoft.Json

@inject ServerRepository ServerRepository
@inject UserRepository UserRepository
@inject Repository<WebSpace> WebSpaceRepository
@inject DomainRepository DomainRepository
@inject ConfigService ConfigService

@attribute [PermissionRequired(nameof(Permissions.AdminDashboard))]

<LazyLoader Load="Load">
    <div class="row mb-5">
        <div class="col-12 col-lg-6 col-xl">
            <a class="mt-4 card" href="/admin/servers">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Servers</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(ServerCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-0">
                                <i class="text-primary bx bx-server bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-12 col-lg-6 col-xl">
            <a class="mt-4 card" href="/admin/webspaces">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Webspaces</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(WebSpaceCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-0">
                                <i class="text-primary bx bx-globe bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-12 col-lg-6 col-xl">
            <a class="mt-4 card" href="/admin/domains">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Domains</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(DomainCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-">
                                <i class="text-primary bx bx-purchase-tag bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-12 col-lg-6 col-xl">
            <a class="mt-4 card" href="/admin/users">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Users</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(UserCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-">
                                <i class="text-primary bx bx-user bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    </div>

    <LazyLoader Load="LoadHealthCheckData">
        @if (HealthCheckData == null)
        {
            <div class="card">
                <div class="card-header">
                    <div class="card-title">
                        <TL>Moonlight health</TL>
                    </div>
                </div>
                <div class="card-body">
                    <div class="alert alert-warning">
                        <TL>Unable to fetch health check data</TL>
                    </div>
                </div>
            </div>
        }
        else
        {
            <HealthCheckView HealthCheck="@HealthCheckData"/>
        }
    </LazyLoader>
</LazyLoader>

@code
{
    private int ServerCount = 0;
    private int UserCount = 0;
    private int DomainCount = 0;
    private int WebSpaceCount = 0;

    private HealthCheck? HealthCheckData;

    private Task Load(LazyLoader lazyLoader)
    {
        ServerCount = ServerRepository.Get().Count();
        UserCount = UserRepository.Get().Count();
        DomainCount = DomainRepository.Get().Count();
        WebSpaceCount = WebSpaceRepository.Get().Count();

        return Task.CompletedTask;
    }

    private async Task LoadHealthCheckData(LazyLoader lazyLoader)
    {
        await lazyLoader.SetText("Loading health check data");

        var appUrl = ConfigService
            .Get()
            .Moonlight.AppUrl;

        try
        {
            using var client = new HttpClient();
            var json = await client.GetStringAsync($"{appUrl}/_health");
            HealthCheckData = JsonConvert.DeserializeObject<HealthCheck>(json) ?? new();
        }
        catch (Exception e)
        {
            HealthCheckData = null;
            Logger.Warn("Unable to fetch health check data");
            Logger.Warn(e);
        }
    }
}