#!/usr/bin/env bash
# Shared config helper — sourced by all deploy scripts.
# Provides cfg() to read values from config.yaml.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
CONFIG_FILE="${DEPLOY_DIR}/config.yaml"

if [[ ! -f "${CONFIG_FILE}" ]]; then
  if [[ -f "${CONFIG_FILE}.example" ]]; then
    echo "[error] config.yaml not found. Run: cp config.yaml.example config.yaml" >&2
  else
    echo "[error] config.yaml not found." >&2
  fi
  exit 1
fi

# cfg "dotted.key.path" — reads a value from config.yaml
cfg() {
  python3 -c "
import yaml, json, sys
with open(sys.argv[1]) as f:
    c = yaml.safe_load(f)
keys = sys.argv[2].split('.')
v = c
for k in keys:
    if isinstance(v, list):
        v = v[int(k)]
    else:
        v = v[k]
if isinstance(v, bool):
    print(str(v).lower())
elif isinstance(v, (dict, list)):
    print(json.dumps(v))
else:
    print('' if v is None else v)
" "${CONFIG_FILE}" "$1" 2>/dev/null
}

# cfg_require "key" "label" — reads value and dies if empty
cfg_require() {
  local val
  val="$(cfg "$1")"
  if [[ -z "${val}" ]]; then
    echo "[error] Missing required config: $1 ($2)" >&2
    exit 1
  fi
  printf '%s' "${val}"
}

# generate_env — writes the flat env file the app expects to stdout
# Points DB at in-cluster PostgreSQL, storage at in-cluster MinIO
generate_env() {
  python3 -c "
import yaml

with open('${CONFIG_FILE}') as f:
    c = yaml.safe_load(f)

d = c['domains']
db = c['database']
em = c['email']
ps = c['push']
st = c['storage']
wk = c['worker']
ft = c['features']
aa = c.get('apple_auth', {})
ga = c.get('google_auth', {})
rd = c.get('redis', {})

def b(v):
    return str(v).lower() if isinstance(v, bool) else str(v)

def val(v):
    return '' if v is None else str(v)

lines = [
    # API
    'DEBUG=true',
    f\"ALLOWED_HOSTS={d['api']},{d['base']},localhost\",
    f\"CORS_ALLOWED_ORIGINS=https://{d['base']},https://{d['admin']}\",
    'TIMEZONE=UTC',
    f\"BASE_URL=https://{d['base']}\",
    'PORT=8000',
    # Admin
    f\"NEXT_PUBLIC_API_URL=https://{d['api']}\",
    f\"ADMIN_PANEL_URL=https://{d['admin']}\",
    # Database (in-cluster PostgreSQL)
    'DB_HOST=postgres.honeydue.svc.cluster.local',
    'DB_PORT=5432',
    f\"POSTGRES_USER={val(db['user'])}\",
    f\"POSTGRES_DB={db['name']}\",
    'DB_SSLMODE=disable',
    f\"DB_MAX_OPEN_CONNS={db['max_open_conns']}\",
    f\"DB_MAX_IDLE_CONNS={db['max_idle_conns']}\",
    f\"DB_MAX_LIFETIME={db['max_lifetime']}\",
    # Redis (in-cluster)
    f\"REDIS_URL=redis://{':%s@' % val(rd.get('password')) if rd.get('password') else ''}redis.honeydue.svc.cluster.local:6379/0\",
    'REDIS_DB=0',
    # Email
    f\"EMAIL_HOST={em['host']}\",
    f\"EMAIL_PORT={em['port']}\",
    f\"EMAIL_USE_TLS={b(em['use_tls'])}\",
    f\"EMAIL_HOST_USER={val(em['user'])}\",
    f\"DEFAULT_FROM_EMAIL={val(em['from'])}\",
    # Push
    'APNS_AUTH_KEY_PATH=/secrets/apns/apns_auth_key.p8',
    f\"APNS_AUTH_KEY_ID={val(ps['apns_key_id'])}\",
    f\"APNS_TEAM_ID={val(ps['apns_team_id'])}\",
    f\"APNS_TOPIC={ps['apns_topic']}\",
    f\"APNS_USE_SANDBOX={b(ps['apns_use_sandbox'])}\",
    f\"APNS_PRODUCTION={b(ps['apns_production'])}\",
    # Worker
    f\"TASK_REMINDER_HOUR={wk['task_reminder_hour']}\",
    f\"OVERDUE_REMINDER_HOUR={wk['overdue_reminder_hour']}\",
    f\"DAILY_DIGEST_HOUR={wk['daily_digest_hour']}\",
    # Storage (in-cluster MinIO — S3-compatible, same env vars as B2)
    f\"B2_KEY_ID={val(st['minio_root_user'])}\",
    # B2_APP_KEY injected from secret (MINIO_ROOT_PASSWORD)
    f\"B2_BUCKET_NAME={val(st['bucket'])}\",
    'B2_ENDPOINT=minio.honeydue.svc.cluster.local:9000',
    'STORAGE_USE_SSL=false',
    f\"STORAGE_MAX_FILE_SIZE={st['max_file_size']}\",
    f\"STORAGE_ALLOWED_TYPES={st['allowed_types']}\",
    # MinIO root user (for MinIO deployment + bucket init job)
    f\"MINIO_ROOT_USER={val(st['minio_root_user'])}\",
    # Features
    f\"FEATURE_PUSH_ENABLED={b(ft['push_enabled'])}\",
    f\"FEATURE_EMAIL_ENABLED={b(ft['email_enabled'])}\",
    f\"FEATURE_WEBHOOKS_ENABLED={b(ft['webhooks_enabled'])}\",
    f\"FEATURE_ONBOARDING_EMAILS_ENABLED={b(ft['onboarding_emails_enabled'])}\",
    f\"FEATURE_PDF_REPORTS_ENABLED={b(ft['pdf_reports_enabled'])}\",
    f\"FEATURE_WORKER_ENABLED={b(ft['worker_enabled'])}\",
    # Apple auth/IAP
    f\"APPLE_CLIENT_ID={val(aa.get('client_id'))}\",
    f\"APPLE_TEAM_ID={val(aa.get('team_id'))}\",
    f\"APPLE_IAP_KEY_ID={val(aa.get('iap_key_id'))}\",
    f\"APPLE_IAP_ISSUER_ID={val(aa.get('iap_issuer_id'))}\",
    f\"APPLE_IAP_BUNDLE_ID={val(aa.get('iap_bundle_id'))}\",
    f\"APPLE_IAP_KEY_PATH={val(aa.get('iap_key_path'))}\",
    f\"APPLE_IAP_SANDBOX={b(aa.get('iap_sandbox', True))}\",
    # Google auth/IAP
    f\"GOOGLE_CLIENT_ID={val(ga.get('client_id'))}\",
    f\"GOOGLE_ANDROID_CLIENT_ID={val(ga.get('android_client_id'))}\",
    f\"GOOGLE_IOS_CLIENT_ID={val(ga.get('ios_client_id'))}\",
    f\"GOOGLE_IAP_PACKAGE_NAME={val(ga.get('iap_package_name'))}\",
    f\"GOOGLE_IAP_SERVICE_ACCOUNT_PATH={val(ga.get('iap_service_account_path'))}\",
]

print('\n'.join(lines))
"
}
