// Services/Local/StationLocalService.cs
using FilterCair.Shared.Models;
using Microsoft.JSInterop;
using System.Text.Json;

namespace FilterCair.Client.Services.Local
{
    public class StationLocalService
    {
        private readonly IJSRuntime _js;

        public StationLocalService(IJSRuntime js)
        {
            _js = js;
        }

        public async Task SaveStationsAsync(int customerId, List<StationModel> stations)
        {
            var json = JsonSerializer.Serialize(stations, JsonOptions);
            await _js.InvokeVoidAsync("filterDb.saveStations", customerId, json);
        }

        public async Task<List<StationModel>> GetStationsAsync(int customerId)
        {
            try
            {
                var json = await _js.InvokeAsync<string>("filterDb.getStations", customerId);
                return JsonSerializer.Deserialize<List<StationModel>>(json, JsonOptions) ?? new();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"StationLocalService Fehler: {ex.Message}");
                return new();
            }
        }

        private static readonly JsonSerializerOptions JsonOptions = new()
        {
            PropertyNameCaseInsensitive = true,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };
    }
}