using System.Collections.ObjectModel;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;
using FootwearTest.Models;
using FootwearTest.Services;

namespace FootwearTest.ViewModels;

public partial class DashboardViewModel : ViewModelBase
{
    private readonly TestRunRepository _repository;

    public DashboardViewModel(TestRunRepository repository)
    {
        _repository = repository;
        RefreshCommand = new AsyncRelayCommand(RefreshAsync);
        _ = RefreshAsync();
    }

    private DeviceSnapshot _snapshot = DeviceSnapshot.Initial;

    public ObservableCollection<TestRunSummary> RecentRuns { get; } = [];
    public IAsyncRelayCommand RefreshCommand { get; }

    public DeviceSnapshot Snapshot
    {
        get => _snapshot;
        set => SetProperty(ref _snapshot, value);
    }

    private async Task RefreshAsync()
    {
        RecentRuns.Clear();
        foreach (var run in await _repository.GetRecentAsync(8))
        {
            RecentRuns.Add(run);
        }
    }
}
