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

namespace Application.Features.Api.Crew.EndSession;

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        var session = await db.CrewSession
            .FirstOrDefaultAsync(s => s.ID == request.CrewSessionID && s.Status == CrewSessionStatus.Active, ct);

        if (session is null)
        {
            throw new KeyNotFoundException("활성 세션을 찾을 수 없습니다.");
        }

        session.End();
        await db.SaveChangesAsync(ct);

        // 쪽지 발송은 Endpoint에서 처리
    }
}