using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Microsoft.EntityFrameworkCore;
using SharedKernel.Results;

namespace Application.Features.Api.Studio.Settlement.GetAccount;

internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
{
    public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
    {
        var accounts = await db.SettlementAccount.AsNoTracking()
            .Where(a => a.MemberID == request.MemberID)
            .OrderBy(a => a.CreatedAt)
            .Select(a => new AccountItem(
                a.ID,
                a.BankCode,
                a.BankName,
                a.AccountNumber.Length > 4 ? "****" + a.AccountNumber.Substring(a.AccountNumber.Length - 4) : a.AccountNumber,
                a.AccountHolder,
                a.IsVerified,
                a.CreatedAt,
                a.UpdatedAt
            ))
            .ToListAsync(ct);

        return Result.Success(new Response(accounts));
    }
}