using DeepDrftModels.DTOs;
using DeepDrftPublic.Client.Services;

namespace DeepDrftPublic.Client.ViewModels;

public class TrackDetailViewModel
{
    public ITrackDataService TrackData { get; }

    public TrackDto? Track { get; set; }
    public bool IsLoading { get; set; } = true;
    public bool NotFound { get; set; }

    public TrackDetailViewModel(ITrackDataService trackData)
    {
        TrackData = trackData;
    }

    public async Task Load(string entryKey)
    {
        // Idempotent across navigations: the scoped instance may be reused, so reset
        // every flag before the fetch rather than relying on construction defaults.
        IsLoading = true;
        NotFound = false;
        Track = null;

        try
        {
            var result = await TrackData.GetTrack(entryKey);

            if (result.Success && result.Value is not null)
            {
                Track = result.Value;
            }
            else
            {
                NotFound = true;
            }
        }
        finally
        {
            IsLoading = false;
        }
    }
}
