using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using QuantEngine.Core.Interfaces;
using QuantEngine.Core.Models;

namespace QuantEngine.Application.Services
{
    public class WorkspaceService
    {
        private readonly IWorkspaceRepository _repository;
        private readonly IPostgresqlHistoryStore _historyStore;

        public WorkspaceService(IWorkspaceRepository repository, IPostgresqlHistoryStore historyStore)
        {
            _repository = repository;
            _historyStore = historyStore;
        }

        public Task<IEnumerable<Setting>> GetSettingsAsync() => _repository.GetSettingsAsync();
        public Task<Setting?> GetSettingByKeyAsync(string key) => _repository.GetSettingByKeyAsync(key);
        public Task<bool> UpsertSettingAsync(Setting setting) => _repository.UpsertSettingAsync(setting);
        public Task<bool> DeleteSettingAsync(string key) => _repository.DeleteSettingAsync(key);

        public Task<IEnumerable<AccountSnapshot>> GetAccountSnapshotsAsync() => _repository.GetAccountSnapshotsAsync();
        public Task<bool> InsertAccountSnapshotsAsync(IEnumerable<AccountSnapshot> snapshots) => _repository.InsertAccountSnapshotsAsync(snapshots);
        public Task<bool> ClearAccountSnapshotsAsync() => _repository.ClearAccountSnapshotsAsync();

        public Task<int> AppendHistoryAsync(string domain, IDictionary<string, object?> payload) => _historyStore.AppendAsync(domain, payload);
        public Task<IReadOnlyList<IDictionary<string, object?>>> ReadHistorySnapshotAsync(string domain, int limit = 500) => _historyStore.SnapshotAsync(domain, limit);
    }
}
