#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
PROJECT_ROOT=$(cd "${SCRIPT_DIR}/../.." && pwd)

PROJECT_KEY=${PROJECT_KEY:-68ef617fb371dc80a10641f7}
USER_KEY=${USER_KEY:-7550145433285312514}
VIEW_NAME=${VIEW_NAME:-董颖-G1Q3}
WORK_ITEM_TYPE=${WORK_ITEM_TYPE:-issue}

SYNC_ROOT=${SYNC_ROOT:-/data1/dongying/Mono3d/G1Q3/feishu_project}
SYNC_MANIFEST_PATH_WAS_SET=${SYNC_MANIFEST_PATH+x}
INFERENCE_ROOT_WAS_SET=${INFERENCE_ROOT+x}
INFERENCE_MANIFEST_PATH_WAS_SET=${INFERENCE_MANIFEST_PATH+x}
EXPORT_JSON=${EXPORT_JSON:-"${SYNC_ROOT}/exports/dongying_g1q3_issue_list.json"}
SYNC_MANIFEST_PATH=${SYNC_MANIFEST_PATH:-"${SYNC_ROOT}/exports/dongying_g1q3_issue_list.sync_manifest.json"}
SNAPSHOT_DIR=${SNAPSHOT_DIR:-"${SYNC_ROOT}/exports/history"}
DOWNLOAD_ROOT=${DOWNLOAD_ROOT:-"${SYNC_ROOT}/downloaded_issue_data"}
DOWNLOAD_MANIFEST_PATH=${DOWNLOAD_MANIFEST_PATH:-"${DOWNLOAD_ROOT}/download_manifest.json"}
INFERENCE_ROOT=${INFERENCE_ROOT:-"${SYNC_ROOT}/inference_issue_data"}
INFERENCE_MANIFEST_PATH=${INFERENCE_MANIFEST_PATH:-"${INFERENCE_ROOT}/inference_manifest.json"}

PYTHON_BIN=${PYTHON_BIN:-/deeplearning_team/ydong/dongying/miniconda/envs/dev/bin/python}
RUN_DOWNLOAD=${RUN_DOWNLOAD:-1}
RUN_INFERENCE=${RUN_INFERENCE:-1}
ENABLE_TRACKING=${ENABLE_TRACKING:-1}
ENABLE_CONVERT=${ENABLE_CONVERT:-1}
CONVERT_OUTPUT_DIR_NAME=${CONVERT_OUTPUT_DIR_NAME:-objectlist}
CONVERT_TRACKING_JSON_NAME=${CONVERT_TRACKING_JSON_NAME:-merge.json}
CONVERT_CAM_ID=${CONVERT_CAM_ID:-}
SAVE_SNAPSHOT=${SAVE_SNAPSHOT:-1}
REFRESH_CHANGED_ISSUES=${REFRESH_CHANGED_ISSUES:-0}
SKIP_EXISTING_INFERENCE=${SKIP_EXISTING_INFERENCE:-1}
DRY_RUN=${DRY_RUN:-0}
USE_ISSUE_FRAME_WINDOW=${USE_ISSUE_FRAME_WINDOW:-1}
FRAME_BEFORE=${FRAME_BEFORE:-200}
FRAME_AFTER=${FRAME_AFTER:-200}
MISSING_ISSUE_FRAME_POLICY=${MISSING_ISSUE_FRAME_POLICY:-full}
ISSUE_ID_MIN=${ISSUE_ID_MIN:-}
ISSUE_ID_MAX=${ISSUE_ID_MAX:-6979030662}

EXPORTED_MODEL=${EXPORTED_MODEL:-${PROJECT_ROOT}/runs/export/train_mono3d_two_roi_20260427-raw_no_edge/merged_model.torchscript}
# EXPORTED_MODEL=${EXPORTED_MODEL:-${PROJECT_ROOT}/runs/export/train_mono3d_two_roi_20260506-keep_fake_3d_branch/merged_model.torchscript}

ISSUE_TRACKING_SCRIPT=${ISSUE_TRACKING_SCRIPT:-"${PROJECT_ROOT}/tools/feishu_project/run_issue_data_tracking.sh"}
TRACKING_MODEL_VERSION=${TRACKING_MODEL_VERSION:-20260427}
ENABLE_RETEST=${ENABLE_RETEST:-1}
RETEST_NAME_KEYWORDS=${RETEST_NAME_KEYWORDS:-AEB,FCW,场地} # AEB,FCW,场地 ACC,HMI,LCC,TSR
RETEST_RUN_NAME=${RETEST_RUN_NAME:-$(date +%Y%m%d_%H%M%S)}
RETEST_ROOT=${RETEST_ROOT:-"${SYNC_ROOT}/retest_runs/${TRACKING_MODEL_VERSION}_${RETEST_RUN_NAME}"}
SHOW_DISTANCE_LABEL=${SHOW_DISTANCE_LABEL:-1}
DISTANCE_LABEL_MODE=${DISTANCE_LABEL_MODE:-depth}
DISTANCE_LABEL_PANELS=${DISTANCE_LABEL_PANELS:-3d}

export ENABLE_CONVERT
export CONVERT_OUTPUT_DIR_NAME
export CONVERT_TRACKING_JSON_NAME
export CONVERT_CAM_ID

if [[ "${ENABLE_RETEST}" == "1" ]]; then
  if [[ -z "${INFERENCE_ROOT_WAS_SET}" ]]; then
    INFERENCE_ROOT="${RETEST_ROOT}/inference_issue_data"
  fi
  if [[ -z "${INFERENCE_MANIFEST_PATH_WAS_SET}" ]]; then
    INFERENCE_MANIFEST_PATH="${INFERENCE_ROOT}/inference_manifest.json"
  fi
  if [[ -z "${SYNC_MANIFEST_PATH_WAS_SET}" ]]; then
    SYNC_MANIFEST_PATH="${RETEST_ROOT}/dongying_g1q3_issue_list.sync_manifest.json"
  fi
fi

CMD=(
  "${PYTHON_BIN}" "${PROJECT_ROOT}/tools/feishu_project/sync_issue_data.py"
  --project-key "${PROJECT_KEY}"
  --user-key "${USER_KEY}"
  --view-name "${VIEW_NAME}"
  --work-item-type "${WORK_ITEM_TYPE}"
  --output-json "${EXPORT_JSON}"
  --sync-manifest-path "${SYNC_MANIFEST_PATH}"
  --snapshot-dir "${SNAPSHOT_DIR}"
  --python-bin "${PYTHON_BIN}"
  --download-root "${DOWNLOAD_ROOT}"
  --download-manifest-path "${DOWNLOAD_MANIFEST_PATH}"
  --inference-root "${INFERENCE_ROOT}"
  --inference-manifest-path "${INFERENCE_MANIFEST_PATH}"
)

if [[ "${RUN_DOWNLOAD}" == "1" ]]; then
  CMD+=(--run-download)
fi

if [[ "${RUN_INFERENCE}" == "1" ]]; then
  CMD+=(--run-inference)
fi

if [[ "${USE_ISSUE_FRAME_WINDOW}" == "1" ]]; then
  CMD+=(--use-issue-frame-window --frame-before "${FRAME_BEFORE}" --frame-after "${FRAME_AFTER}" --missing-issue-frame-policy "${MISSING_ISSUE_FRAME_POLICY}")
fi

if [[ "${ENABLE_TRACKING}" == "1" ]]; then
  CMD+=(--run-tracking --issue-tracking-script "${ISSUE_TRACKING_SCRIPT}")
fi

if [[ "${SAVE_SNAPSHOT}" == "1" ]]; then
  CMD+=(--save-snapshot)
fi

if [[ "${REFRESH_CHANGED_ISSUES}" == "1" ]]; then
  CMD+=(--refresh-changed-issues)
fi

if [[ "${SKIP_EXISTING_INFERENCE}" == "1" ]]; then
  CMD+=(--skip-existing-inference)
fi

if [[ "${DRY_RUN}" == "1" ]]; then
  CMD+=(--dry-run)
fi

if [[ -n "${ISSUE_ID_MIN}" ]]; then
  CMD+=(--issue-id-min "${ISSUE_ID_MIN}")
fi

if [[ -n "${ISSUE_ID_MAX}" ]]; then
  CMD+=(--issue-id-max "${ISSUE_ID_MAX}")
fi

if [[ -n "${EXPORTED_MODEL}" ]]; then
  CMD+=(--exported-model "${EXPORTED_MODEL}")
fi

if [[ -n "${TRACKING_MODEL_VERSION}" ]]; then
  CMD+=(--tracking-model-version "${TRACKING_MODEL_VERSION}")
fi

if [[ "${SHOW_DISTANCE_LABEL}" == "1" ]]; then
  CMD+=("--inference-arg=--show-distance-label")
  if [[ -n "${DISTANCE_LABEL_MODE}" ]]; then
    CMD+=("--inference-arg=--distance-label-mode" "--inference-arg=${DISTANCE_LABEL_MODE}")
  fi
  if [[ -n "${DISTANCE_LABEL_PANELS}" ]]; then
    # shellcheck disable=SC2206
    DISTANCE_LABEL_PANELS_ARR=(${DISTANCE_LABEL_PANELS})
    CMD+=("--inference-arg=--distance-label-panels")
    for panel in "${DISTANCE_LABEL_PANELS_ARR[@]}"; do
      CMD+=("--inference-arg=${panel}")
    done
  fi
fi

if [[ "${ENABLE_RETEST}" == "1" ]] && [[ -n "${RETEST_NAME_KEYWORDS}" ]]; then
  IFS=',' read -r -a RETEST_KEYWORD_ARRAY <<< "${RETEST_NAME_KEYWORDS}"
  for keyword in "${RETEST_KEYWORD_ARRAY[@]}"; do
    keyword="${keyword#"${keyword%%[![:space:]]*}"}"
    keyword="${keyword%"${keyword##*[![:space:]]}"}"
    if [[ -n "${keyword}" ]]; then
      CMD+=(--issue-name-keyword "${keyword}")
    fi
  done
fi

CMD+=("$@")
"${CMD[@]}"
