using Application.Abstractions.Messaging;
using Application.Abstractions.Data;
using Application.Abstractions.Cache;
using Domain.Entities.Page.Banner;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Admin.Banner.Position.Save;

public sealed class Handler(IAppDbContext db, ICacheService cache) : ICommandHandler<Command, Response>
{
    public async Task<Response> Handle(Command request, CancellationToken ct)
    {
        var items = request.Items ?? [];

        var dbRows = await db.BannerPosition.Include(x => x.BannerItems).ToListAsync(ct);

        var inserted = 0;
        var updated = 0;
        var deleted = 0;

        var incomingById = items.Where(x => x.ID.HasValue).ToDictionary(x => x.ID!.Value, x => x);

        foreach (var existing in dbRows)
        {
            if (!incomingById.TryGetValue(existing.ID, out var row))
            {
                if (existing.BannerItems.Count > 0)
                {
                    continue;
                }

                db.BannerPosition.Remove(existing);
                deleted++;
                continue;
            }

            if (existing.Code != row.Code || existing.Subject != row.Subject || existing.IsActive != row.IsActive)
            {
                existing.Update(row.Subject, row.IsActive);

                if (existing.Code != row.Code)
                {
                    var prop = typeof(BannerPosition).GetProperty("Code");
                    prop?.SetValue(existing, row.Code);
                }

                updated++;
            }
        }

        var existingIds = dbRows.Select(x => x.ID).ToHashSet();
        foreach (var row in items.Where(x => !x.ID.HasValue))
        {
            if (dbRows.Any(x => x.Code == row.Code))
            {
                continue;
            }

            var entity = BannerPosition.Create(row.Code, row.Subject, row.IsActive);
            db.BannerPosition.Add(entity);
            inserted++;
        }

        if (inserted + updated + deleted > 0)
        {
            await db.SaveChangesAsync(ct);
            await cache.RemoveByPrefixAsync("banner:", ct);
        }

        return new Response(inserted, updated, deleted);
    }
}