using UtopiaCanteen.Shared;
using UtopiaCanteenSystem.Api;
using UtopiaCanteenSystem.Models;
namespace UtopiaCanteenSystem.Services;
///
/// Meal schedule CRUD via central backend API (client never touches HRMS/MySQL).
///
public sealed class BackendApiMealScheduleService : IMealScheduleService
{
private readonly ICanteenBackendApiClient _api;
public BackendApiMealScheduleService(ICanteenBackendApiClient api)
{
_api = api;
}
public IReadOnlyList GetActiveSchedulesForSite(string siteId)
{
var all = GetAllSchedulesAsync().GetAwaiter().GetResult();
if (string.IsNullOrWhiteSpace(siteId))
return all;
var site = siteId.Trim().Replace("SITE : ", "", StringComparison.OrdinalIgnoreCase).Trim();
return all.Where(s => string.Equals(s.LocationSiteId?.Trim(), site, StringComparison.OrdinalIgnoreCase)).ToList();
}
public async Task> GetAllSchedulesAsync(CancellationToken cancellationToken = default)
{
var dtos = await _api.GetMealSchedulesAsync(cancellationToken).ConfigureAwait(false);
return dtos.Select(ApiDtoMapper.ToMealSchedule).ToList();
}
public async Task CreateAsync(MealSchedule schedule, CancellationToken cancellationToken = default)
{
var dto = ApiDtoMapper.ToMealScheduleDto(schedule);
return await _api.CreateMealScheduleAsync(dto, cancellationToken).ConfigureAwait(false);
}
public async Task UpdateAsync(MealSchedule schedule, CancellationToken cancellationToken = default)
{
var dto = ApiDtoMapper.ToMealScheduleDto(schedule);
await _api.UpdateMealScheduleAsync(dto, cancellationToken).ConfigureAwait(false);
}
public async Task DeleteAsync(long id, CancellationToken cancellationToken = default) =>
await _api.DeleteMealScheduleAsync(id, cancellationToken).ConfigureAwait(false);
}