@page "/"
@using ZahlenAnalyse.Web.Models
@using ZahlenAnalyse.Web.Services
@using Microsoft.AspNetCore.Components.Authorization
@inject WorkspaceService DbService
@inject AuthenticationStateProvider AuthStateProvider

<MudContainer MaxWidth="MaxWidth.Large" Class="mt-8">
    <MudStack Row="true" AlignItems="AlignItems.Center" Justify="Justify.SpaceBetween" Class="mb-8">
        <MudText Typo="Typo.h3">Meine Workspaces</MudText>
        
        <AuthorizeView>
            <Authorized>
                <MudButton Href="/workspaces/create" 
                           Variant="Variant.Filled" 
                           Color="Color.Primary" 
                           StartIcon="@Icons.Material.Filled.Add">
                    Neuer Workspace
                </MudButton>
            </Authorized>
        </AuthorizeView>
    </MudStack>

    <AuthorizeView>
        <Authorized>
            @if (_isLoading)
            {
                <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
            }
            else if (!_workspaces.Any())
            {
                <MudPaper Class="pa-8 text-center" Elevation="1">
                    <MudIcon Icon="@Icons.Material.Filled.FolderOpen" Size="Size.Large" Color="Color.Default" Class="mb-4" />
                    <MudText Typo="Typo.h5" Class="mb-2">Noch keine Workspaces vorhanden</MudText>
                    <MudText Typo="Typo.body1" Color="Color.Secondary" Class="mb-6">
                        Erstelle deinen ersten Workspace (z.B. Urlaubsabrechnung), um mit der Datenanalyse zu beginnen.
                    </MudText>
                    <MudButton Href="/workspaces/create" Variant="Variant.Outlined" Color="Color.Primary">
                        Jetzt erstellen
                    </MudButton>
                </MudPaper>
            }
            else
            {
                <MudGrid>
                    @foreach (var ws in _workspaces)
                    {
                        <MudItem xs="12" sm="6" md="4">
                            <MudCard Elevation="2" Class="h-100">
                                <MudCardHeader>
                                    <CardHeaderContent>
                                        <MudText Typo="Typo.h6">@ws.Name</MudText>
                                        <MudText Typo="Typo.caption" Color="Color.Secondary">
                                            Erstellt am @ws.CreatedAt.ToLocalTime().ToString("dd.MM.yyyy")
                                        </MudText>
                                    </CardHeaderContent>
                                    <CardHeaderActions>
                                        <MudIconButton Icon="@Icons.Material.Filled.Settings" 
                                                    Color="Color.Default" 
                                                    Href="@($"/workspace/{ws.Id}")" />
                                    </CardHeaderActions>
                                </MudCardHeader>
                                <MudCardContent>
                                    <MudText Typo="Typo.body2">
                                        @ws.Dimensions.Count Dimensionen konfiguriert
                                    </MudText>
                                    <MudStack Row="true" Spacing="1" Class="mt-2 flex-wrap">
                                        @foreach (var dim in ws.Dimensions.Take(3))
                                        {
                                            <MudChip T="string" Size="Size.Small" Variant="Variant.Outlined">@dim.Name</MudChip>
                                        }
                                        @if (ws.Dimensions.Count > 3)
                                        {
                                            <MudChip T="string" Size="Size.Small" Variant="Variant.Text">+@(ws.Dimensions.Count - 3)</MudChip>
                                        }
                                    </MudStack>
                                </MudCardContent>
                                <MudCardActions>
                                    <MudButton Href="@($"/erfassen/{ws.Id}")" Variant="Variant.Text" Color="Color.Primary">
                                        Daten erfassen
                                    </MudButton>
                                    <MudButton Href="@($"/auswertung/{ws.Id}")" Variant="Variant.Text" Color="Color.Secondary">
                                        Auswertung
                                    </MudButton>
                                </MudCardActions>
                            </MudCard>
                        </MudItem>
                    }
                </MudGrid>
            }
        </Authorized>
        <NotAuthorized>
            <MudPaper Class="pa-8 text-center" Elevation="1">
                <MudText Typo="Typo.h5" Class="mb-4">Willkommen beim Zahlen-Analyse Tool</MudText>
                <MudText Typo="Typo.body1" Class="mb-6">Bitte melde dich an, um deine Daten zu verwalten.</MudText>
                <MudButton Href="/login" Variant="Variant.Filled" Color="Color.Primary">
                    Mit Pocket-ID anmelden
                </MudButton>
            </MudPaper>
        </NotAuthorized>
    </AuthorizeView>
</MudContainer>

@code {
    private List<Workspace> _workspaces = new();
    private bool _isLoading = true;

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        
        if (authState.User.Identity?.IsAuthenticated == true)
        {
            await LoadWorkspaces();
        }
        else
        {
            _isLoading = false;
        }
    }

    private async Task LoadWorkspaces()
    {
        _isLoading = true;
        try
        {
            // Dank unseres Services reicht hier ein simpler Aufruf!
            _workspaces = await DbService.GetWorkspacesForUserAsync();
        }
        finally
        {
            _isLoading = false;
        }
    }
}