@page "/"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using GmRelay.Shared.Domain
@using GmRelay.Web.Services
@attribute [Authorize]
@inject AuthorizedSessionService SessionService
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager Navigation

<PageTitle>Панель управления — GM-Relay</PageTitle>

<div class="page-container">
    <div class="page-header animate-fade-in">
        <h2>Добро пожаловать, @userName! 👋</h2>
        <p>Выберите группу для управления игровыми сессиями.</p>
    </div>

    @if (groups == null)
    {
        <div class="card-grid">
            @for (int i = 0; i < 3; i++)
            {
                <div class="skeleton skeleton-card"></div>
            }
        </div>
    }
    else if (groups.Count == 0)
    {
        <div class="glass-card">
            <div class="empty-state">
                <div class="empty-state-icon">🤖</div>
                <div class="empty-state-title">Нет зарегистрированных групп</div>
                <p class="empty-state-text">Добавьте бота GM-Relay в свою группу Telegram, чтобы начать управлять игровыми сессиями.</p>
            </div>
        </div>
    }
    else
    {
        <div class="card-grid stagger-children">
            @foreach (var group in groups)
            {
                <div class="glass-card group-card">
                    <div class="group-card-icon">🎮</div>
                    <h3 class="group-card-title">@group.Name</h3>
                    <p class="group-card-id">ID: @(group.Platform == "Discord" ? group.ExternalGroupId : group.TelegramChatId.ToString())</p>
                    <div class="group-card-meta">
                        <span class="status-badge platform-badge">
                            @FormatPlatform(group.Platform)
                        </span>
                        <span class="status-badge @(group.ManagerRole == GroupManagerRoleExtensions.OwnerValue ? "status-success" : "status-info")">
                            @FormatRole(group.ManagerRole)
                        </span>
                    </div>
                    <a href="/group/@group.Id" class="btn-gm btn-gm-primary" style="width: 100%; justify-content: center; margin-top: auto;">
                        Посмотреть игры →
                    </a>
                </div>
            }
        </div>
    }
</div>

<style>
    .group-card {
        display: flex;
        flex-direction: column;
        min-height: 180px;
    }

    .group-card-icon {
        font-size: 2rem;
        margin-bottom: 0.75rem;
    }

    .group-card-title {
        font-size: 1.125rem;
        font-weight: 600;
        margin-bottom: 0.25rem;
        color: var(--text-primary);
    }

    .group-card-id {
        font-size: 0.75rem;
        color: var(--text-muted);
        font-family: 'Courier New', monospace;
        margin-bottom: 1rem;
    }

    .group-card-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        align-items: center;
        margin-bottom: 1rem;
    }

    .platform-badge {
        background: rgba(88, 101, 242, 0.15);
        color: #9ea8ff;
        border-color: rgba(88, 101, 242, 0.35);
    }
</style>

@code {
    private List<WebGameGroup>? groups;
    private string userName = "";

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;
        userName = user.Identity?.Name ?? "Мастер Игры";

        if (!user.TryGetPlatformIdentity(out _, out _))
        {
            Navigation.NavigateTo("/access-denied");
            return;
        }

        groups = await SessionService.GetGroupsForCurrentUserAsync();
    }

    private static string FormatRole(string role) =>
        GroupManagerRoleExtensions.FromDatabaseValue(role).ToDisplayName();

    private static string FormatPlatform(string? platform) =>
        string.Equals(platform, "Discord", StringComparison.OrdinalIgnoreCase) ? "Discord" : "Telegram";
}
