using DocuMed.Common.Models.Mapper;
using DocuMed.Domain.Enums.QueryFilters;

namespace DocuMed.PWA.Pages;

public class HomePageViewModel(IUserUtility userUtility, IRestWrapper restWrapper, ISnackbar snackbar)
    : BaseViewModel<List<MedicalHistorySDto>>
{
    public DayQueryFilter SelectedDayFilter { get; set; } = DayQueryFilter.Today;

    public ApplicationUserSDto User { get; private set; } = new ApplicationUserSDto();

    

    public override async Task InitializeAsync()
    {

        try
        {
            IsProcessing = true;
            User = await userUtility.GetUserAsync();
            var token = await userUtility.GetBearerTokenAsync();
            var list = await restWrapper
                .MedicalHistoryRestApi
                .GetAllByFilterAsync(SelectedDayFilter, 0, token);
            PageDto = list;

        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            snackbar.Add(e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }

        await base.InitializeAsync();

    }

    public async Task SelectDayFilter(DayQueryFilter day)
    {
        SelectedDayFilter = day;
        await InitializeAsync();
    }
}