using Books.Api.Domain.Attachments;
using EventFlow.Commands;

namespace Books.Api.Commands.Attachments;

public class UploadAttachmentCommandHandler
    : CommandHandler<AttachmentAggregate, AttachmentId, UploadAttachmentCommand>
{
    public override Task ExecuteAsync(
        AttachmentAggregate aggregate,
        UploadAttachmentCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Upload(
            command.CompanyId,
            command.FileName,
            command.OriginalFileName,
            command.ContentType,
            command.FileSize,
            command.StoragePath,
            command.UploadedBy,
            command.DraftId,
            command.TransactionId);

        return Task.CompletedTask;
    }
}

public class LinkAttachmentToTransactionCommandHandler
    : CommandHandler<AttachmentAggregate, AttachmentId, LinkAttachmentToTransactionCommand>
{
    public override Task ExecuteAsync(
        AttachmentAggregate aggregate,
        LinkAttachmentToTransactionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.LinkToTransaction(command.TransactionId);

        return Task.CompletedTask;
    }
}

public class DeleteAttachmentCommandHandler
    : CommandHandler<AttachmentAggregate, AttachmentId, DeleteAttachmentCommand>
{
    public override Task ExecuteAsync(
        AttachmentAggregate aggregate,
        DeleteAttachmentCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Delete(command.DeletedBy, command.Reason);

        return Task.CompletedTask;
    }
}
