@page "/workspaces/create"
@using ZahlenAnalyse.Web.Models
@using ZahlenAnalyse.Web.Services
@using Microsoft.AspNetCore.Components.Authorization
@inject WorkspaceService DbService
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavManager
@inject ISnackbar Snackbar

<MudContainer MaxWidth="MaxWidth.Medium" Class="mt-8 mb-8">
    <MudText Typo="Typo.h4" Class="mb-6">Neuen Workspace erstellen</MudText>

    <MudPaper Class="pa-6 mb-6" Elevation="1">
        <MudTextField @bind-Value="_workspace.Name" 
                      Label="Name des Workspaces (z.B. Urlaubsabrechnung)" 
                      Variant="Variant.Outlined" 
                      Required="true" />
    </MudPaper>

    <MudText Typo="Typo.h5" Class="mb-4">Analysedimensionen</MudText>

    @foreach (var dim in _workspace.Dimensions.ToList())
    {
        <MudCard Class="mb-4" Elevation="1">
            <MudCardContent>
                <MudStack Row="true" AlignItems="AlignItems.Center" Class="mb-4">
                    <MudTextField @bind-Value="dim.Name" 
                                  Label="Name der Dimension (z.B. Ort oder Kostenart)" 
                                  Variant="Variant.Outlined" />
                    
                    <MudIconButton Icon="@Icons.Material.Filled.Delete" 
                                   Color="Color.Error" 
                                   OnClick="() => _workspace.Dimensions.Remove(dim)" />
                </MudStack>

                <MudText Typo="Typo.subtitle2" Class="mb-2">Hierarchie-Knoten:</MudText>
                
                @foreach (var rootNode in dim.Nodes.ToList())
                {
                    <NodeEditor Node="rootNode" OnRemove="(n) => dim.Nodes.Remove(n)" />
                }

                <MudButton Variant="Variant.Text" 
                           StartIcon="@Icons.Material.Filled.Add" 
                           Color="Color.Success" 
                           OnClick="() => dim.Nodes.Add(new DimensionNode())"
                           Class="mt-2">
                    Haupt-Knoten hinzufügen
                </MudButton>
            </MudCardContent>
        </MudCard>
    }

    <MudButton Variant="Variant.Outlined" 
               StartIcon="@Icons.Material.Filled.AddBox" 
               Color="Color.Info" 
               OnClick="() => _workspace.Dimensions.Add(new DimensionDefinition())"
               Class="mb-8">
        Neue Dimension hinzufügen
    </MudButton>

    <MudDivider Class="mb-4" />

    <MudStack Row="true" Justify="Justify.FlexEnd">
        <MudButton Variant="Variant.Text" Href="/">Abbrechen</MudButton>
        <MudButton Variant="Variant.Filled" 
                   Color="Color.Primary" 
                   StartIcon="@Icons.Material.Filled.Save" 
                   OnClick="SaveWorkspace">
            Workspace speichern
        </MudButton>
    </MudStack>

</MudContainer>

@code {
    private Workspace _workspace = new();

   private async Task SaveWorkspace()
{
    if (string.IsNullOrWhiteSpace(_workspace.Name))
    {
        Snackbar.Add("Bitte gib dem Workspace einen Namen.", Severity.Warning);
        return;
    }

    try
    {
        // Wir übergeben nur noch das blanke Formular-Objekt.
        // Der Service kümmert sich um den Auth-Rest!
        await DbService.SaveWorkspaceAsync(_workspace);
        
        Snackbar.Add($"Workspace '{_workspace.Name}' erfolgreich gespeichert!", Severity.Success);
        NavManager.NavigateTo("/"); 
    }
    catch (Exception ex)
    {
        Snackbar.Add($"Fehler beim Speichern: {ex.Message}", Severity.Error);
    }
}
}