using System.Collections.Concurrent;
using System.Collections.Generic;
using BattSim.Models;

namespace BattSim.Services
{
    public static class DataFilter
    {
        public enum FilterOption { ALL, YEAR, MONTH, WEEK, DAY }
        public static EnergyData[] FilterData(EnergyData[] data, FilterOption filterOption)
        {
            var dailyData = data
                            .GroupBy(d=>d.Time.Date)
                            .Select(group=> new EnergyData{
                                Consumption = group.Sum(d => d.Consumption),
                                Production = group.Sum(d => d.Production),
                                BatteryCharge = group.Max(d => d.BatteryCharge),
                                Time = group.First().Time
                            }).ToArray();
            
            return filterOption switch {
                FilterOption.DAY => data[(data.Length > 96?data.Length - 96:0)..], // 24 hours * 4 quarters per hour
                FilterOption.WEEK => data[(data.Length > 672?data.Length - 672:0)..], // 7 days * 24 hours * 4 quarters per hour
                FilterOption.MONTH => dailyData[(dailyData.Length > 30?dailyData.Length - 30:0)..],
                FilterOption.YEAR => dailyData[(dailyData.Length > 365?dailyData.Length - 365:0)..],
                _ => dailyData
            };
        }
    }
}