@page "/workspace/{*WorkspaceId}"
@using ZahlenAnalyse.Web.Models
@using ZahlenAnalyse.Web.Services
@inject WorkspaceService DbService
@inject ISnackbar Snackbar
@inject NavigationManager NavManager

<MudContainer MaxWidth="MaxWidth.Large" Class="mt-8 mb-8">
    @if (_workspace == null)
    {
        <MudProgressCircular Indeterminate="true" />
    }
    else
    {
        <MudText Typo="Typo.h3" Class="mb-6">@_workspace.Name</MudText>

        <MudGrid>
            <MudItem xs="12" md="6">
                <MudPaper Class="pa-6" Elevation="1">
                    <MudText Typo="Typo.h6" Class="mb-4">Daten importieren</MudText>
                    <MudText Typo="Typo.body2" Class="mb-4">Lade eine Excel-Datei hoch, um Fakten in diesen Workspace zu importieren.</MudText>
                    
                    <MudFileUpload T="IBrowserFile" FilesChanged="@(e => UploadFile(e))" Accept=".xlsx">
                        <CustomContent>
                            <MudButton Variant="Variant.Filled" 
                                    Color="Color.Primary" 
                                    StartIcon="@Icons.Material.Filled.CloudUpload"
                                    OnClick="@context.OpenFilePickerAsync">
                                Excel hochladen
                            </MudButton>
                        </CustomContent>
                    </MudFileUpload>
                </MudPaper>
            </MudItem>

            <MudItem xs="12" md="6">
                <MudPaper Class="pa-6" Elevation="1">
                    <MudText Typo="Typo.h6" Class="mb-4">Dimensionen</MudText>
                    <MudStack Row="true" Wrap="Wrap.Wrap">
                        @foreach (var dim in _workspace.Dimensions)
                        {
                            <MudChip T="string" Variant="Variant.Outlined" Color="Color.Info">@dim.Name</MudChip>
                        }
                    </MudStack>
                </MudPaper>
            </MudItem>

            <MudItem xs="12">
                <MudPaper Class="pa-6" Elevation="1">
                    <MudText Typo="Typo.h6" Class="mb-4">Letzte Importe / Einträge</MudText>
                    <MudTable Items="_recentFakten" Hover="true" Breakpoint="Breakpoint.Sm">
                        <HeaderContent>
                            <MudTh>Datum</MudTh>
                            <MudTh>Betrag</MudTh>
                            <MudTh>Details</MudTh>
                        </HeaderContent>
                        <RowTemplate>
                            <MudTd>@context.Date.ToShortDateString()</MudTd>
                            <MudTd>@context.Amount.ToString("N2") €</MudTd>
                            <MudTd>@string.Join(" | ", context.Dimensions.Values)</MudTd>
                        </RowTemplate>
                    </MudTable>
                </MudPaper>
            </MudItem>
        </MudGrid>
    }
</MudContainer>

@code {
    [Parameter] public string WorkspaceId { get; set; } = string.Empty;
    private Workspace? _workspace;
    private List<AnalysisFakt> _recentFakten = new();

    protected override async Task OnInitializedAsync()
    {
        _workspace = await DbService.GetWorkspaceAsync(WorkspaceId);
        if (_workspace != null)
        {
            _recentFakten = await DbService.GetFaktenForWorkspaceAsync(WorkspaceId);
            _recentFakten = _recentFakten.Take(5).ToList();
        }
    }

    private async Task UploadFile(Microsoft.AspNetCore.Components.Forms.IBrowserFile e)
    {
        var file = e;
        if (file == null) return;

        try
        {
            // 1. Datenstrom vom Browser öffnen
            using var browserStream = file.OpenReadStream(maxAllowedSize: 1024 * 1024 * 10); // 10MB
            
            // 2. Den Stream komplett in den "Eimer" (Arbeitsspeicher) laden
            using var memoryStream = new MemoryStream();
            await browserStream.CopyToAsync(memoryStream);
            
            // 3. WICHTIG: Den Lese-Zeiger im Eimer wieder nach ganz oben setzen!
            memoryStream.Position = 0;

            // 4. Den fertigen MemoryStream an den Service übergeben
            await DbService.ImportFromExcelAsync(WorkspaceId, memoryStream);
            
            Snackbar.Add("Import erfolgreich!", Severity.Success);
            
            // Die Tabelle mit den neuen Daten aktualisieren
            _recentFakten = (await DbService.GetFaktenForWorkspaceAsync(WorkspaceId)).Take(5).ToList();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"Fehler beim Import: {ex.Message}", Severity.Error);
        }
    }
}