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

namespace Books.Api.Commands.BankConnections;

public class InitiateBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, InitiateBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        InitiateBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Initiate(
            command.CompanyId,
            command.AspspName,
            command.AuthorizationId,
            command.RedirectUrl,
            command.State);

        return Task.CompletedTask;
    }
}

public class EstablishBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, EstablishBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        EstablishBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Establish(
            command.SessionId,
            command.ValidUntil,
            command.Accounts);

        return Task.CompletedTask;
    }
}

public class FailBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, FailBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        FailBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Fail(command.Reason);

        return Task.CompletedTask;
    }
}

public class DisconnectBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, DisconnectBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        DisconnectBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Disconnect(command.Reason);

        return Task.CompletedTask;
    }
}

public class RefreshBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, RefreshBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        RefreshBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Refresh(command.NewSessionId, command.ValidUntil);

        return Task.CompletedTask;
    }
}

public class LinkBankAccountCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, LinkBankAccountCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        LinkBankAccountCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.LinkBankAccount(command.BankAccountId, command.LinkedAccountId, command.ImportFromDate);

        return Task.CompletedTask;
    }
}

public class ReInitiateBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, ReInitiateBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        ReInitiateBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.ReInitiate(command.AuthorizationId, command.RedirectUrl, command.State);

        return Task.CompletedTask;
    }
}

public class ArchiveBankConnectionCommandHandler
    : CommandHandler<BankConnectionAggregate, BankConnectionId, ArchiveBankConnectionCommand>
{
    public override Task ExecuteAsync(
        BankConnectionAggregate aggregate,
        ArchiveBankConnectionCommand command,
        CancellationToken cancellationToken)
    {
        aggregate.Archive(command.Reason);

        return Task.CompletedTask;
    }
}
