using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

namespace ASTRAIN.Client.Pages;

public partial class Home
{
    /// <summary>
    /// Optional user id from route.
    /// </summary>
    [Parameter]
    public string? UserId { get; set; }

    /// <inheritdoc/>
    protected override async Task OnInitializedAsync()
    {
        var ensured = await Api.EnsureUserAsync(UserId);
        if (string.IsNullOrWhiteSpace(ensured))
        {
            return;
        }

        UserContext.SetUserId(ensured);
        var target = $"/{ensured}/routines";
        if (!Navigation.Uri.EndsWith(target, StringComparison.OrdinalIgnoreCase))
        {
            Navigation.NavigateTo(target, true);
        }
    }
}
