module transaction.api;

import handy_http_primitives;
import handy_http_data.json;
import handy_http_handlers.path_handler;
import slf4d;
import std.typecons;

import transaction.model;
import transaction.data;
import transaction.service;
import transaction.dto;
import profile.data;
import profile.service;
import account.api;
import util.money;
import util.pagination;
import util.data;

// Transactions API

immutable DEFAULT_TRANSACTION_PAGE = PageRequest(1, 10, [Sort("timestamp", SortDir.DESC)]);

void handleGetTransactions(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    PageRequest pr = PageRequest.parse(request, DEFAULT_TRANSACTION_PAGE);
    auto responsePage = getTransactions(ds, pr);
    writeJsonBody(response, responsePage);
}

void handleGetTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    TransactionDetail txn = getTransaction(ds, getTransactionIdOrThrow(request));
    import asdf : serializeToJson;
    string jsonStr = serializeToJson(txn);
    response.writeBodyString(jsonStr, "application/json");
}

void handleAddTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    auto payload = readJsonBodyAs!AddTransactionPayload(request);
    addTransaction(ds, payload);
}

void handleUpdateTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    // TODO
}

void handleDeleteTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    // TODO
}

private ulong getTransactionIdOrThrow(in ServerHttpRequest request) {
    return getPathParamOrThrow(request, "transactionId");
}

// Vendors API

void handleGetVendors(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    TransactionVendor[] vendors = getAllVendors(ds);
    writeJsonBody(response, vendors);
}

void handleGetVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    TransactionVendor vendor = getVendor(ds, getVendorId(request));
    writeJsonBody(response, vendor);
}

struct VendorPayload {
    string name;
    string description;
}

void handleCreateVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    VendorPayload payload = readJsonBodyAs!VendorPayload(request);
    ProfileDataSource ds = getProfileDataSource(request);
    TransactionVendor vendor = createVendor(ds, payload);
    writeJsonBody(response, vendor);
}

void handleUpdateVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    VendorPayload payload = readJsonBodyAs!VendorPayload(request);
    ProfileDataSource ds = getProfileDataSource(request);
    TransactionVendor updated = updateVendor(ds, getVendorId(request), payload);
    writeJsonBody(response, updated);
}

void handleDeleteVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
    ProfileDataSource ds = getProfileDataSource(request);
    deleteVendor(ds, getVendorId(request));
}

private ulong getVendorId(in ServerHttpRequest request) {
    return getPathParamOrThrow(request, "vendorId");
}

// Categories API
