@page "/"
@attribute [Authorize]
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Security
@inject ISiteRepository SiteRepository
@inject ITemplateEngineRepository TemplateEngineRepository
@inject IInboundApiKeyAdmin ApiKeyAdmin

<div class="container-fluid mt-3">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">Welcome to ScadaBridge</h4>
        <AuthorizeView>
            <Authorized>
                <span class="text-muted small">
                    @* CentralUI-024: claim type resolved via JwtTokenService. *@
                    Signed in as <strong>@context.User.GetDisplayName()</strong>
                </span>
            </Authorized>
        </AuthorizeView>
    </div>
    <p class="text-muted">Central management console for the ScadaBridge SCADA system.</p>

    @* KPI row *@
    <div class="row g-3 mb-4">
        <div class="col-lg-3 col-md-6 col-12">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold">@(_loaded ? _siteCount.ToString() : "—")</div>
                    <div class="text-muted small">Sites configured</div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 col-12">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold">@(_loaded ? _dataConnectionCount.ToString() : "—")</div>
                    <div class="text-muted small">Data connections configured</div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 col-12">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold">@(_loaded ? _templateCount.ToString() : "—")</div>
                    <div class="text-muted small">Templates</div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 col-12">
            <div class="card h-100">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold">@(_loaded ? _apiKeyCount.ToString() : "—")</div>
                    <div class="text-muted small">API keys</div>
                </div>
            </div>
        </div>
    </div>

    @* Quick actions *@
    <h6 class="text-muted text-uppercase small mb-2">Quick actions</h6>
    <div class="row g-3">
        <div class="col-lg-4 col-md-6 col-12">
            <a class="card h-100 text-decoration-none text-reset" href="/monitoring/health">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start">
                        <h6 class="mb-1">Health Dashboard</h6>
                        <span class="text-muted">&rarr;</span>
                    </div>
                    <p class="text-muted small mb-0">Live cluster, data connection, and queue health per site.</p>
                </div>
            </a>
        </div>
        <div class="col-lg-4 col-md-6 col-12">
            <a class="card h-100 text-decoration-none text-reset" href="/audit/configuration">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start">
                        <h6 class="mb-1">Configuration Audit Log</h6>
                        <span class="text-muted">&rarr;</span>
                    </div>
                    <p class="text-muted small mb-0">Browse changes to configuration and deployments.</p>
                </div>
            </a>
        </div>
        <div class="col-lg-4 col-md-6 col-12">
            <a class="card h-100 text-decoration-none text-reset" href="/design/templates">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start">
                        <h6 class="mb-1">Templates</h6>
                        <span class="text-muted">&rarr;</span>
                    </div>
                    <p class="text-muted small mb-0">Design templates, shared scripts, and external systems.</p>
                </div>
            </a>
        </div>
    </div>
</div>

@code {
    private bool _loaded;
    private int _siteCount;
    private int _dataConnectionCount;
    private int _templateCount;
    private int _apiKeyCount;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _siteCount = (await SiteRepository.GetAllSitesAsync()).Count;
            _dataConnectionCount = (await SiteRepository.GetAllDataConnectionsAsync()).Count;
            _templateCount = (await TemplateEngineRepository.GetAllTemplatesAsync()).Count;
            _apiKeyCount = (await ApiKeyAdmin.ListAsync()).Count;
        }
        catch
        {
            // Non-fatal — leave counts at zero with the placeholder rendering.
        }
        _loaded = true;
    }
}
