#!/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
# - ACCOUNT_TYPE: (Opsional) Filter tipe akun (asset, expense, revenue, dll.). Jika tidak diset, ambil semua.

# Contoh menjalankan script:
# YOUR_FIREFLY_III_URL="https://firefly.yourdomain.com" YOUR_ACCESS_TOKEN="your_actual_token_here" ./get_accounts.sh
# YOUR_FIREFLY_III_URL="https://firefly.yourdomain.com" YOUR_ACCESS_TOKEN="your_actual_token_here" ACCOUNT_TYPE="asset" ./get_accounts.sh

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

# Tentukan URL dan parameter query
BASE_URL="${YOUR_FIREFLY_III_URL}/api/v1/accounts"
QUERY_PARAM=""
if [[ -n "${ACCOUNT_TYPE}" ]]; then
  QUERY_PARAM="?type=${ACCOUNT_TYPE}"
fi
FINAL_URL="${BASE_URL}${QUERY_PARAM}"

# --- Perintah Curl ---
echo "=== Mengambil Daftar Akun ==="
if [[ -n "${ACCOUNT_TYPE}" ]]; then
  echo "Filter tipe akun: ${ACCOUNT_TYPE}"
else
  echo "Mengambil semua jenis akun (tanpa filter)."
fi
echo "URL yang digunakan: ${FINAL_URL}"
echo ""

curl -s -X GET "${FINAL_URL}" \
  -H "Authorization: Bearer ${YOUR_ACCESS_TOKEN}" \
  -H "Accept: application/json" | jq '.' # Gunakan 'jq' untuk format output JSON jika tersedia

echo -e "\n---\n"