#!/usr/bin/env bash
# QA stub interpreter: emulates the akshare_collector.py JSON contract
# (source=stub-synthetic, _synthetic=true on every payload).
# Invoked by MarketDataService as: fund-lab-python <script> --operation search --query X | --operation nav --code X
set -euo pipefail

OP=""
QUERY=""
CODE=""
args=("$@")
for ((i=1; i<${#args[@]}; i++)); do
  case "${args[$i]}" in
    --operation) OP="${args[$((i+1))]:-}"; i=$((i+1)) ;;
    --query)     QUERY="${args[$((i+1))]:-}"; i=$((i+1)) ;;
    --code)      CODE="${args[$((i+1))]:-}"; i=$((i+1)) ;;
  esac
done

TS="$(date -u +%Y-%m-%dT%H:%M:%S+00:00)"

case "$OP" in
  search)
    printf '{"schema_version":"fund-lab.akshare.v1","operation":"search","source":"stub-synthetic","source_revision":"stub-1","_synthetic":true,"collected_at":"%s","instruments":[{"code":"000001","name":"桩测试基金一号","fund_type":"混合型"},{"code":"110011","name":"桩测试基金二号","fund_type":null}]}\n' "$TS"
    ;;
  nav)
    # Sentinel code used by QA to exercise the AKShare-unavailable branch of
    # GET /api/market/nav-dates (collector exits non-zero -> API answers 503).
    if [ "$CODE" = "999999" ]; then
      echo "AKShare collector failed: simulated outage for $CODE" >&2
      exit 2
    fi
    printf '{"schema_version":"fund-lab.akshare.v1","operation":"nav","source":"stub-synthetic","source_revision":"stub-1","_synthetic":true,"collected_at":"%s","instrument":{"code":"%s"},"observations":[{"nav_date":"2026-09-15","published_at":null,"nav":"1.1000","accumulated_nav":null,"daily_return":null},{"nav_date":"2026-09-16","published_at":null,"nav":"1.1205","accumulated_nav":null,"daily_return":"1.86"},{"nav_date":"2026-09-17","published_at":null,"nav":"1.1180","accumulated_nav":null,"daily_return":"-0.22"},{"nav_date":"2026-09-18","published_at":null,"nav":"1.1500","accumulated_nav":null,"daily_return":"2.86"},{"nav_date":"2026-09-21","published_at":null,"nav":"1.1902","accumulated_nav":null,"daily_return":"3.49"}]}\n' "$TS" "$CODE"
    ;;
  bond-quote)
    # Synthetic rows mirror the live AKShare convertible-bond spot contract:
    # a known live code answers with a name/price, unknown codes fail like the
    # "no valid quote" collector error (-> API 503).
    if [ "$CODE" = "110075" ]; then
      printf '{"schema_version":"fund-lab.akshare.v1","operation":"bond-quote","source":"stub-synthetic","source_revision":"stub-1","_synthetic":true,"collected_at":"%s","instrument":{"code":"%s"},"quote":{"name":"南航转债","price":"106.368","maturity_date":"2026-10-15"}}\n' "$TS" "$CODE"
    else
      echo "AKShare collector failed: no valid quote for $CODE" >&2
      exit 2
    fi
    ;;
  stock-quote)
    # Synthetic rows mirror the live AKShare A-share spot contract: a known live
    # code answers with a name/price, unknown codes fail like the "no valid
    # quote" collector error (-> API 503).
    if [ "$CODE" = "600519" ]; then
      printf '{"schema_version":"fund-lab.akshare.v1","operation":"stock-quote","source":"stub-synthetic","source_revision":"stub-1","_synthetic":true,"collected_at":"%s","instrument":{"code":"%s"},"quote":{"name":"贵州茅台","price":"1252.57","currency":"CNY"}}\n' "$TS" "$CODE"
    else
      echo "AKShare collector failed: no valid quote for $CODE" >&2
      exit 2
    fi
    ;;
  stock-daily)
    # Synthetic daily bars mirror the live AKShare A-share history contract
    # (>= 3 rows); unknown codes fail like the collector's "no valid daily
    # history" error (-> API 503).
    if [ "$CODE" = "600519" ]; then
      printf '{"schema_version":"fund-lab.akshare.v1","operation":"stock-daily","source":"stub-synthetic","source_revision":"stub-1","_synthetic":true,"collected_at":"%s","instrument":{"code":"%s"},"observations":[{"date":"2026-09-17","close":"1266.98","volume":"1755380.0","amount":"2217338283.0"},{"date":"2026-09-18","close":"1257.12","volume":"2489087.0","amount":"3135849108.0"},{"date":"2026-09-21","close":"1252.57","volume":"2501689.0","amount":"3135910045.0"}]}\n' "$TS" "$CODE"
    else
      echo "AKShare collector failed: no valid daily history for $CODE" >&2
      exit 2
    fi
    ;;
  *)
    echo "unknown operation: $OP" >&2
    exit 2
    ;;
esac
