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

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

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result<Response>>
{
    public async Task<Result<Response>> Handle(Command request, CancellationToken ct)
    {
        var account = await db.SettlementAccount.FirstOrDefaultAsync(a => a.ID == request.AccountID && a.MemberID == request.MemberID, ct);
        if (account is null)
        {
            return Result.Failure<Response>(Error.NotFound("Account", "계좌 정보를 찾을 수 없습니다."));
        }

        db.SettlementAccount.Remove(account);
        await db.SaveChangesAsync(ct);

        return Result.Success(new Response("계좌가 삭제되었습니다."));
    }
}