using AyCode.Core.Consts;
using AyCode.Core.Enums;
using AyCode.Core.Helpers;
using AyCode.Core.Loggers;
using AyCode.Database.DbContexts.Loggers;
using AyCode.Entities;
using AyCode.Entities.Server.LogItems;
using AyCode.Interfaces.Entities;
using Microsoft.EntityFrameworkCore;

namespace AyCode.Database;

public class AcDbLogItemWriter<TLoggerDbContext, TLogItem> : AcLogItemWriterBase<TLogItem> where TLoggerDbContext : AcLoggerDbContextBase<TLogItem>
    where TLogItem : class, IAcLogItem
{
    private TLoggerDbContext _ctx = Activator.CreateInstance<TLoggerDbContext>();

    protected AcDbLogItemWriter() : this(null)
    { }

    protected AcDbLogItemWriter(string? categoryName = null) : base(categoryName)
    { }

    public AcDbLogItemWriter(AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
    { }

    protected override void WriteLogItemCallback(TLogItem logItem)
    {
        //using var ctx = Activator.CreateInstance<TLoggerDbContext>(); 

        _ctx.LogItems.Add(logItem);
        _ctx.SaveChanges();
    }

    //protected override void WriteLogItem(TLogItem logItem, Action<IEnumerable<TLogItem>>? callback = null)
    //{
    //    try
    //    {
    //        base.WriteLogItem(logItem, logItems =>
    //        {
    //            _ctx.LogItems.AddRange(logItems);
    //            _ctx.SaveChanges();
    //        });
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("ERRORORRORO");
    //    }
    //}
}