using EventFlow.Aggregates;

namespace Books.Api.Domain.BankConnections.Events;

/// <summary>
/// Raised when a bank connection authorization is started
/// </summary>
public class BankConnectionInitiatedEvent(
    string companyId,
    string aspspName,
    string authorizationId,
    string redirectUrl,
    string state) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string CompanyId { get; } = companyId;
    public string AspspName { get; } = aspspName;
    public string AuthorizationId { get; } = authorizationId;
    public string RedirectUrl { get; } = redirectUrl;
    public string State { get; } = state;
}

/// <summary>
/// Raised when a bank connection is successfully established
/// </summary>
public class BankConnectionEstablishedEvent(
    string sessionId,
    DateTimeOffset validUntil,
    IReadOnlyList<BankAccountInfo> accounts) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string SessionId { get; } = sessionId;
    public DateTimeOffset ValidUntil { get; } = validUntil;
    public IReadOnlyList<BankAccountInfo> Accounts { get; } = accounts;
}

/// <summary>
/// Raised when a bank connection fails or is rejected
/// </summary>
public class BankConnectionFailedEvent(
    string reason) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string Reason { get; } = reason;
}

/// <summary>
/// Raised when a bank connection is disconnected
/// </summary>
public class BankConnectionDisconnectedEvent(
    string reason) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string Reason { get; } = reason;
}

/// <summary>
/// Raised when a bank connection session is refreshed
/// </summary>
public class BankConnectionRefreshedEvent(
    string newSessionId,
    DateTimeOffset validUntil) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string NewSessionId { get; } = newSessionId;
    public DateTimeOffset ValidUntil { get; } = validUntil;
}

/// <summary>
/// Raised when a bank account is linked to a chart of accounts account
/// </summary>
public class BankAccountLinkedEvent(
    string bankAccountId,
    string linkedAccountId,
    DateOnly? importFromDate) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string BankAccountId { get; } = bankAccountId;
    public string LinkedAccountId { get; } = linkedAccountId;
    public DateOnly? ImportFromDate { get; } = importFromDate;
}

/// <summary>
/// Raised when a bank connection authorization is re-initiated (reconnect)
/// </summary>
public class BankConnectionReInitiatedEvent(
    string authorizationId,
    string redirectUrl,
    string state) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string AuthorizationId { get; } = authorizationId;
    public string RedirectUrl { get; } = redirectUrl;
    public string State { get; } = state;
}

/// <summary>
/// Raised when a bank connection is archived (hidden from UI)
/// </summary>
public class BankConnectionArchivedEvent(
    string reason) : AggregateEvent<BankConnectionAggregate, BankConnectionId>
{
    public string Reason { get; } = reason;
}
