#!/bin/bash
set -euo pipefail

ES_HOST="${ELASTICSEARCH_HOSTS:-http://es:9200}"
KB_HOST="http://localhost:5601"

echo "[INFO] Starting Kibana post-start configuration..."

# 等待 Elasticsearch 可用
wait_for_elasticsearch() {
    echo "[INFO] Waiting for Elasticsearch..."
    local max_attempts=60
    local attempt=1

    while [ $attempt -le $max_attempts ]; do
        if curl -fs "$ES_HOST/_cluster/health" >/dev/null 2>&1; then
            echo "[OK] Elasticsearch is available"
            return 0
        fi
        echo "    Waiting for ES... ($attempt/$max_attempts)"
        sleep 5
        ((attempt++))
    done

    echo "[ERROR] Elasticsearch timeout"
    return 1
}

# 等待 Kibana 可用
wait_for_kibana() {
    echo "[INFO] Waiting for Kibana..."
    local max_attempts=120
    local attempt=1

    while [ $attempt -le $max_attempts ]; do
        if curl -fs "$KB_HOST/api/status" >/dev/null 2>&1; then
            local status=$(curl -s "$KB_HOST/api/status" | grep -o '"level":"available"' || echo "")
            if [ -n "$status" ]; then
                echo "[OK] Kibana is available"
                return 0
            fi
            echo "    Waiting for Kibana... ($attempt/$max_attempts, status: $status)"
        else
            echo "    Waiting for Kibana... ($attempt/$max_attempts, connection failed)"
        fi
        sleep 5
        ((attempt++))
    done

    echo "[ERROR] Kibana timeout"
    return 1
}

# 幂等设置索引副本数为0
fix_replicas_idempotent() {
    echo "[INFO] Checking and fixing index replicas..."

    # 获取所有 train-* 和 infer-* 索引
    local indices=$(curl -s "$ES_HOST/_cat/indices/train-*,infer-*?h=index" 2>/dev/null || echo "")

    if [ -z "$indices" ]; then
        echo "[INFO] No train-*/infer-* indices found, skipping replica adjustment"
        return 0
    fi

    for idx in $indices; do
        # 检查当前副本数
        local current_replicas=$(curl -s "$ES_HOST/$idx/_settings" | grep -o '"number_of_replicas":"[^"]*"' | cut -d'"' -f4 || echo "")

        if [ "$current_replicas" != "0" ]; then
            echo "[INFO] Setting replicas to 0 for index: $idx (current: $current_replicas)"
            curl -fsS -X PUT "$ES_HOST/$idx/_settings" \
                -H 'Content-Type: application/json' \
                -d '{"index":{"number_of_replicas":0}}' >/dev/null || {
                echo "[WARN] Failed to set replicas for $idx"
                continue
            }
            echo "[OK] Updated replicas for $idx"
        else
            echo "[INFO] Index $idx already has 0 replicas, skipping"
        fi
    done
}

# 幂等创建数据视图
create_data_views_idempotent() {
    echo "[INFO] Checking and creating data views..."

    # 检查是否存在匹配的索引
    local train_indices=$(curl -s "$ES_HOST/_cat/indices/train-*?h=index" 2>/dev/null | wc -l || echo "0")
    local infer_indices=$(curl -s "$ES_HOST/_cat/indices/infer-*?h=index" 2>/dev/null | wc -l || echo "0")

    # 创建 train 数据视图
    if [ "$train_indices" -gt 0 ]; then
        # 检查数据视图是否已存在
        local train_exists=$(curl -s "$KB_HOST/api/data_views" -H 'kbn-xsrf: true' 2>/dev/null | grep '"title":"train-\*"' | wc -l )

        if [ "$train_exists" -eq 0 ]; then
            echo "[INFO] Creating data view for train-* indices"
            curl -fsS -X POST "$KB_HOST/api/data_views/data_view" \
                -H 'kbn-xsrf: true' \
                -H 'Content-Type: application/json' \
                -d '{"data_view":{"name":"train","title":"train-*","timeFieldName":"@timestamp"}}' \
                >/dev/null && echo "[OK] Created train data view" || echo "[WARN] Failed to create train data view"
        else
            echo "[INFO] Train data view already exists, skipping"
        fi
    else
        echo "[INFO] No train-* indices found, skipping train data view creation"
    fi

    # 创建 infer 数据视图
    if [ "$infer_indices" -gt 0 ]; then
        # 检查数据视图是否已存在
        local infer_exists=$(curl -s "$KB_HOST/api/data_views" -H 'kbn-xsrf: true' 2>/dev/null | grep '"title":"infer-\*"' | wc -l )

        if [ "$infer_exists" -eq 0 ]; then
            echo "[INFO] Creating data view for infer-* indices"
            curl -fsS -X POST "$KB_HOST/api/data_views/data_view" \
                -H 'kbn-xsrf: true' \
                -H 'Content-Type: application/json' \
                -d '{"data_view":{"name":"infer","title":"infer-*","timeFieldName":"@timestamp"}}' \
                >/dev/null && echo "[OK] Created infer data view" || echo "[WARN] Failed to create infer data view"
        else
            echo "[INFO] Infer data view already exists, skipping"
        fi
    else
        echo "[INFO] No infer-* indices found, skipping infer data view creation"
    fi
}

# 主逻辑
main() {
    # 等待服务可用
    wait_for_elasticsearch || exit 1
    wait_for_kibana || exit 1

    # 执行幂等配置
    fix_replicas_idempotent
    create_data_views_idempotent

    echo "[INFO] Kibana post-start configuration completed"
}

# 运行主逻辑
main
