using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Domain.Entities.Donations;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Api.DonationAlert.BatchSaveConfig;

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command r, CancellationToken ct)
    {
        // 삭제
        if (r.DeleteIDs is { Count: > 0 })
        {
            var toDelete = await db.DonationAlertConfig
                .Where(c => r.DeleteIDs.Contains(c.ID) && c.ChannelID == r.ChannelID)
                .ToListAsync(ct);

            db.DonationAlertConfig.RemoveRange(toDelete);
        }

        // 추가/수정
        foreach (var item in r.Items)
        {
            if (item.ID.HasValue)
            {
                var config = await db.DonationAlertConfig
                    .FirstOrDefaultAsync(c => c.ID == item.ID.Value && c.ChannelID == r.ChannelID, ct);

                if (config is null)
                {
                    continue;
                }

                config.Update(
                    item.Title, item.Amount, item.MatchType, item.Message,
                    item.PlayDelaySec, item.DisplayDurationSec,
                    item.PopupEffect, item.TextEffect,
                    item.NicknameFontFamily, item.NicknameFontSize, item.NicknameFontColor,
                    item.AmountFontFamily, item.AmountFontSize, item.AmountFontColor,
                    item.MessageFontFamily, item.MessageFontSize, item.MessageFontColor,
                    item.TemplateFontFamily, item.TemplateFontSize, item.TemplateFontColor,
                    item.EnableImage, item.ImageUrl,
                    item.EnableSound, item.SoundUrl,
                    item.IsActive
                );
            }
            else
            {
                var config = DonationAlertConfig.Create(
                    r.ChannelID, r.MemberID,
                    item.Title, item.Amount, item.MatchType, item.Message,
                    item.PlayDelaySec, item.DisplayDurationSec,
                    popupEffect: item.PopupEffect,
                    textEffect: item.TextEffect,
                    nicknameFontFamily: item.NicknameFontFamily,
                    nicknameFontSize: item.NicknameFontSize,
                    nicknameFontColor: item.NicknameFontColor,
                    amountFontFamily: item.AmountFontFamily,
                    amountFontSize: item.AmountFontSize,
                    amountFontColor: item.AmountFontColor,
                    messageFontFamily: item.MessageFontFamily,
                    messageFontSize: item.MessageFontSize,
                    messageFontColor: item.MessageFontColor,
                    templateFontFamily: item.TemplateFontFamily,
                    templateFontSize: item.TemplateFontSize,
                    templateFontColor: item.TemplateFontColor,
                    enableImage: item.EnableImage,
                    imageUrl: item.ImageUrl,
                    enableSound: item.EnableSound,
                    soundUrl: item.SoundUrl
                );

                db.DonationAlertConfig.Add(config);
            }
        }

        await db.SaveChangesAsync(ct);
    }
}