#!/bin/bash

# --- Placeholder for User Execution ---
# Anda perlu mengganti placeholder berikut dengan nilai sebenarnya:
# - YOUR_FIREFLY_III_URL: URL instance Firefly III Anda (contoh: https://firefly.yourdomain.com)
# - YOUR_ACCESS_TOKEN: Token akses personal Anda dari Firefly III
# - SOURCE_ACCOUNT_ID: ID akun sumber (dapat ditemukan dengan get_accounts.sh)
# - DESTINATION_ACCOUNT_ID: ID akun tujuan (dapat ditemukan dengan get_accounts.sh)
# - TRANSACTION_TYPE: (Opsional) Jenis transaksi. Default: 'deposit'. Nilai: 'withdrawal', 'deposit', 'transfer'.

# Contoh menjalankan script:
# YOUR_FIREFLY_III_URL="https://firefly.yourdomain.com" YOUR_ACCESS_TOKEN="your_actual_token_here" SOURCE_ACCOUNT_ID=1 DESTINATION_ACCOUNT_ID=2 ./submit_dummy_transaction.sh
# YOUR_FIREFLY_III_URL="https://firefly.yourdomain.com" YOUR_ACCESS_TOKEN="your_actual_token_here" SOURCE_ACCOUNT_ID=1 DESTINATION_ACCOUNT_ID=2 TRANSACTION_TYPE="transfer" ./submit_dummy_transaction.sh

# Cek apakah variabel lingkungan wajib diset
if [[ -z "${YOUR_FIREFLY_III_URL}" || -z "${YOUR_ACCESS_TOKEN}" || -z "${SOURCE_ACCOUNT_ID}" || -z "${DESTINATION_ACCOUNT_ID}" ]]; then
  echo "Error: Variabel lingkungan 'YOUR_FIREFLY_III_URL', 'YOUR_ACCESS_TOKEN', 'SOURCE_ACCOUNT_ID', dan 'DESTINATION_ACCOUNT_ID' harus diset."
  echo "Contoh penggunaan:"
  echo "YOUR_FIREFLY_III_URL=\"https://firefly.yourdomain.com\" YOUR_ACCESS_TOKEN=\"your_actual_token_here\" SOURCE_ACCOUNT_ID=1 DESTINATION_ACCOUNT_ID=2 ./submit_dummy_transaction.sh"
  exit 1
fi

# Set default transaction type if not provided
if [[ -z "${TRANSACTION_TYPE}" ]]; then
  TRANSACTION_TYPE="deposit"
fi

# --- Data Transaksi Dummy ---
# Anda bisa mengubah nilai-nilai ini sesuai kebutuhan untuk pengujian.
DUMMY_DATE="2025-08-19"
DUMMY_AMOUNT="50.00"
DUMMY_DESCRIPTION="Pengeluaran Dummy via Script"

# --- Perintah Curl ---
echo "=== Mengirim Transaksi Dummy ==="
echo "Data yang dikirim:"
echo "  Type: ${TRANSACTION_TYPE}"
echo "  Date: ${DUMMY_DATE}"
echo "  Amount: ${DUMMY_AMOUNT}"
echo "  Description: ${DUMMY_DESCRIPTION}"
echo "  Source ID: ${SOURCE_ACCOUNT_ID}"
echo "  Destination ID: ${DESTINATION_ACCOUNT_ID}"
echo "  URL: ${YOUR_FIREFLY_III_URL}"
echo ""

# Membangun payload JSON secara langsung dalam perintah curl
RESPONSE=$(curl -s -w "%{http_code}" -X POST "${YOUR_FIREFLY_III_URL}/api/v1/transactions" \
  -H "Authorization: Bearer ${YOUR_ACCESS_TOKEN}" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d "{
  \"transactions\": [
    {
      \"type\": \"${TRANSACTION_TYPE}\",
      \"date\": \"${DUMMY_DATE}\",
      \"amount\": \"${DUMMY_AMOUNT}\",
      \"description\": \"${DUMMY_DESCRIPTION}\",
      \"source_id\": ${SOURCE_ACCOUNT_ID},
      \"destination_id\": ${DESTINATION_ACCOUNT_ID}
    }
  ]
}")

HTTP_CODE="${RESPONSE: -3}" # Ambil 3 karakter terakhir sebagai HTTP code
JSON_RESPONSE="${RESPONSE%???}" # Ambil semua karakter kecuali 3 terakhir

echo "Response HTTP Code: ${HTTP_CODE}"
if [[ "${HTTP_CODE}" -eq 200 ]] || [[ "${HTTP_CODE}" -eq 201 ]]; then
    echo "Transaksi berhasil dikirim!"
    echo "Response JSON:"
    echo "${JSON_RESPONSE}" | jq '.' # Format output jika jq tersedia
else
    echo "Gagal mengirim transaksi. HTTP Code: ${HTTP_CODE}"
    if [[ -n "${JSON_RESPONSE}" ]]; then
        echo "Response JSON:"
        echo "${JSON_RESPONSE}" | jq '.'
    fi
fi