using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Domain.Entities.Forum.ValueObject;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Admin.Forum.PostReport.Search;

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var query = db.PostReport.AsNoTracking().Include(c => c.Board).Include(c => c.Post).Include(c => c.Member).AsQueryable();

        if (request.BoardID.HasValue)
        {
            query = query.Where(c => c.BoardID == request.BoardID.Value);
        }

        if (request.PostID.HasValue)
        {
            query = query.Where(c => c.PostID == request.PostID.Value);
        }

        if (!string.IsNullOrWhiteSpace(request.Keyword))
        {
            var kw = request.Keyword.Trim();
            query = request.Search switch
            {
                1 => query.Where(c => c.PostID.ToString().Contains(kw)),
                2 => query.Where(c => c.Post.Subject.Contains(kw)),
                3 => query.Where(c => c.Member != null && c.Member.Name != null && c.Member.Name.Contains(kw)),
                _ => query.Where(c => c.Post.Subject.Contains(kw))
            };
        }

        if (request.Status.HasValue)
        {
            query = query.Where(c => c.Status == (ReportStatus)request.Status.Value);
        }

        if (request.Type.HasValue)
        {
            query = query.Where(c => c.Type == (ReportType)request.Type.Value);
        }

        if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startDate))
        {
            query = query.Where(c => c.CreatedAt >= startDate);
        }

        if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endDate))
        {
            query = query.Where(c => c.CreatedAt <= endDate.AddDays(1));
        }

        query = query.OrderByDescending(c => c.ID);

        var total = await query.CountAsync(ct);

        var list = await query
            .Skip((request.PageNum - 1) * request.PerPage)
            .Take(request.PerPage)
            .Select(c => new
            {
                c.ID,
                c.BoardID,
                BoardName = c.Board.Name,
                c.PostID,
                PostSubject = c.Post.Subject,
                c.MemberID,
                MemberName = c.Member.Name,
                c.Type,
                c.Reason,
                c.Status,
                c.Memo,
                c.UpdatedAt,
                c.CreatedAt
            })
            .ToListAsync(ct);

        var startNum = total - ((request.PageNum - 1) * request.PerPage);

        return new Response(
            total,
            [..list.Select((c, i) => new Response.Row(
                Num: startNum - i,
                c.ID,
                c.BoardID,
                c.BoardName,
                c.PostID,
                c.PostSubject,
                c.MemberID,
                c.MemberName,
                c.Type,
                c.Reason,
                c.Status,
                c.Memo,
                c.UpdatedAt,
                c.CreatedAt
            ))]
        );
    }
}