using Application.Abstractions.Messaging;
using Application.Abstractions.Data;
using Application.Abstractions.Cache;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Api.Banner.Item.GetByCode;

public sealed class Handler(IAppDbContext db, ICacheService cache) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var cacheKey = CacheKeys.BannerByCode(request.Code);
        var cached = await cache.GetAsync<Response>(cacheKey, ct);
        if (cached is not null)
        {
            return cached;
        }

        var position = await db.BannerPosition.AsNoTracking().FirstOrDefaultAsync(c => c.Code == request.Code, ct);

        if (position is null)
        {
            return new Response(0, []);
        }

        var query = db.BannerItem.AsNoTracking().Include(c => c.BannerPosition).Where(c => c.PositionID == position.ID);

        var total = await query.CountAsync(ct);

        var list = await query
            .OrderBy(c => c.Order)
            .ThenByDescending(c => c.ID)
            .Select(c => new
            {
                c.ID,
                c.PositionID,
                PositionCode = c.BannerPosition.Code,
                PositionSubject = c.BannerPosition.Subject,
                c.Subject,
                c.DesktopImage,
                c.MobileImage,
                c.Link,
                c.Order,
                c.IsActive,
                c.StartAt,
                c.EndAt,
                c.UpdatedAt,
                c.CreatedAt
            })
            .ToListAsync(ct);

        var response = new Response(
            total,
            [..list.Select(c => new Response.Row(
                c.ID,
                c.PositionID,
                c.PositionCode,
                c.PositionSubject,
                c.Subject,
                c.DesktopImage,
                c.MobileImage,
                c.Link,
                c.Order,
                c.IsActive,
                c.StartAt,
                c.EndAt,
                c.UpdatedAt,
                c.CreatedAt
            ))]
        );

        await cache.SetAsync(cacheKey, response, ct);
        return response;
    }
}