module account.dto;

import handy_http_primitives : Optional;
import account.model;
import attachment.data;
import attachment.dto;
import util.money;

/// The data the API provides for an Account entity.
struct AccountResponse {
    ulong id;
    string createdAt;
    bool archived;
    string type;
    string numberSuffix;
    string name;
    Currency currency;
    string description;
    Optional!long currentBalance;

    static AccountResponse of(in Account account, Optional!long currentBalance) {
        AccountResponse r;
        r.id = account.id;
        r.createdAt = account.createdAt.toISOExtString();
        r.archived = account.archived;
        r.type = account.type.id;
        r.numberSuffix = account.numberSuffix;
        r.name = account.name;
        r.currency = account.currency;
        r.description = account.description;
        r.currentBalance = currentBalance;
        return r;
    }
}

/// A limited set of account data, usually for use in other responses.
struct SimpleAccountResponse {
    ulong id;
    string name;
    string type;
    string numberSuffix;
}

// The data provided by a user to create a new account.
struct AccountCreationPayload {
    string type;
    string numberSuffix;
    string name;
    string currency;
    string description;
}

struct AccountValueRecordResponse {
    ulong id;
    string timestamp;
    ulong accountId;
    string type;
    long value;
    Currency currency;
    AttachmentResponse[] attachments;

    static AccountValueRecordResponse of(in AccountValueRecord vr, AttachmentRepository attachmentRepo) {
        import std.algorithm : map;
        import std.array : array;
        return AccountValueRecordResponse(
            vr.id,
            vr.timestamp.toISOExtString(),
            vr.accountId,
            vr.type,
            vr.value,
            vr.currency,
            attachmentRepo.findAllByValueRecordId(vr.id)
                .map!(AttachmentResponse.of)
                .array
        );
    }
}

struct ValueRecordCreationPayload {
    string timestamp;
    string type;
    long value;
}

// Class-based inheritance structure for history item response format.

class AccountHistoryItemResponse {
    string timestamp;
    string type;
}

class AccountHistoryValueRecordItemResponse : AccountHistoryItemResponse {
    ulong valueRecordId;
    string valueRecordType;
    long value;
    Currency currency;
}

class AccountHistoryJournalEntryItemResponse : AccountHistoryItemResponse {
    string journalEntryType;
    ulong amount;
    Currency currency;
    ulong transactionId;
    string transactionDescription;
}
