#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Function for printing section headers
section_header() {
  printf "\n${BLUE}===========================================================${NC}\n"
  printf "${BLUE}   $1${NC}\n"
  printf "${BLUE}===========================================================${NC}\n\n"
}

# Error handling
error_exit() {
  printf "${RED}ERROR: $1${NC}\n" >&2
  exit 1
}

# Load .env variables
set -a
if [ -f .env ]; then
  . ./.env 2>/dev/null
fi
set +a

# Set Elasticsearch authentication flag if credentials exist
if [ -n "$ELASTICSEARCH_USER" ] && [ -n "$ELASTICSEARCH_PASSWORD" ]; then
  ES_AUTH="-u $ELASTICSEARCH_USER:$ELASTICSEARCH_PASSWORD"
else
  ES_AUTH=""
fi

section_header "🗑️ CLEANING UP OLD INDICES AND ALIASES"

# Function to delete all indices matching a pattern
delete_indices_by_pattern() {
  local pattern=$1
  local alias_name=$2

  printf "${YELLOW}Cleaning up pattern: ${pattern}${NC}\n"

  # Get all indices matching the pattern (both direct indices and versioned)
  local all_indices=$(curl $ES_AUTH -s -X GET "localhost:9200/_cat/indices?h=index" | grep -E "^${pattern}(_[0-9]+)?$" | sort)
  printf "${YELLOW}Found indices for pattern '${pattern}': ${all_indices}${NC}\n"

  # Check if there's a direct index with the alias name (this causes conflicts)
  local direct_index=$(echo "$all_indices" | grep -E "^${pattern}$")
  if [ -n "$direct_index" ]; then
    printf "${YELLOW}Found conflicting direct index: $direct_index (this will be deleted)${NC}\n"
    curl $ES_AUTH -s -X DELETE "localhost:9200/$direct_index" || printf "${RED}Failed to delete conflicting index $direct_index${NC}\n"
  fi

  # Get versioned indices only
  local versioned_indices=$(echo "$all_indices" | grep -E "^${pattern}_[0-9]+$")
  local latest_versioned=$(echo "$versioned_indices" | tail -n 1)

  if [ -n "$versioned_indices" ]; then
    # Delete all versioned indices except the latest
    for index in $versioned_indices; do
      if [ "$index" != "$latest_versioned" ]; then
        printf "${YELLOW}Deleting old versioned index: $index${NC}\n"
        curl $ES_AUTH -s -X DELETE "localhost:9200/$index" || printf "${RED}Failed to delete $index${NC}\n"
      fi
    done
  fi

  # Remove any existing alias (safe operation)
  printf "${YELLOW}Removing existing alias: $alias_name${NC}\n"
  curl $ES_AUTH -s -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d"{\"actions\":[{\"remove\":{\"alias\":\"$alias_name\",\"index\":\"*\"}}]}" 2>/dev/null || printf "${YELLOW}Alias $alias_name didn't exist${NC}\n"
}

# Define index patterns (using regular variables instead of associative array)
INDEX_PATTERNS="users_index organizations_index banks_index posts_index calls_index"

# Clean up old indices for each pattern
for alias in $INDEX_PATTERNS; do
  delete_indices_by_pattern "$alias" "$alias"
done

section_header "⏳ WAITING FOR ELASTICSEARCH CLUSTER HEALTH"

# Wait for Elasticsearch to be ready
printf "${YELLOW}Waiting for cluster health...${NC}\n"
timeout=30
counter=0
while [ $counter -lt $timeout ]; do
  health_response=$(curl $ES_AUTH -s "localhost:9200/_cluster/health" 2>/dev/null)
  if echo "$health_response" | grep -q '"status":"green\|yellow"'; then
    printf "${GREEN}Cluster is healthy!${NC}\n"
    break
  fi
  printf "${YELLOW}Waiting for cluster (${counter}/${timeout})...${NC}\n"
  sleep 1
  counter=$((counter + 1))
done

if [ $counter -eq $timeout ]; then
  printf "${YELLOW}Warning: Cluster health timeout, proceeding anyway...${NC}\n"
fi

section_header "🧹 SKIPPING FLUSH - DIRECT IMPORT WILL CREATE FRESH INDICES"

# Skip flush since we already cleaned up indices above and import will create fresh ones
printf "${YELLOW}Note: Skipping scout:flush commands to avoid shard conflicts.${NC}\n"
printf "${YELLOW}The scout:import commands will create fresh indices automatically.${NC}\n"

section_header "📥 IMPORTING ALL MODELS TO ELASTICSEARCH (WITHOUT QUEUE)"

# Import all models (this creates new indices) - Force immediate processing by disabling queue
printf "${GREEN}Importing User model...${NC}\n"
SCOUT_QUEUE=false php artisan scout:import "App\Models\User" || error_exit "Failed to import User model"

printf "${GREEN}Importing Organization model...${NC}\n"
SCOUT_QUEUE=false php artisan scout:import "App\Models\Organization" || error_exit "Failed to import Organization model"

printf "${GREEN}Importing Bank model...${NC}\n"
SCOUT_QUEUE=false php artisan scout:import "App\Models\Bank" || error_exit "Failed to import Bank model"

printf "${GREEN}Importing Post model...${NC}\n"
SCOUT_QUEUE=false php artisan scout:import "App\Models\Post" || error_exit "Failed to import Post model"

printf "${GREEN}Importing Call model...${NC}\n"
SCOUT_QUEUE=false php artisan scout:import "App\Models\Call" || error_exit "Failed to import Call model"

# Wait a moment for Elasticsearch to process (shorter wait since we're not using queue)
printf "${YELLOW}Waiting for Elasticsearch to process...${NC}\n"
sleep 2

section_header "🔗 CREATING ALIASES FOR STABLE INDEX NAMES"

# Create aliases pointing to the new timestamped indices
for alias in $INDEX_PATTERNS; do
  printf "${GREEN}Creating alias for: $alias${NC}\n"

  # Find the latest index for this pattern
  latest_index=$(curl $ES_AUTH -s -X GET "localhost:9200/_cat/indices?h=index" | grep "^${alias}_" | sort | tail -n 1)

  if [ -n "$latest_index" ]; then
    printf "${GREEN}Found latest index: $latest_index${NC}\n"

    # Create alias
    curl $ES_AUTH -s -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d"
    {
      \"actions\": [
        { \"add\": { \"index\": \"${latest_index}\", \"alias\": \"${alias}\" } }
      ]
    }
    " && printf "${GREEN}✅ Created alias ${alias} -> ${latest_index}${NC}\n" || printf "${RED}❌ Failed to create alias ${alias}${NC}\n"
  else
    printf "${RED}❌ No index found for pattern ${alias}_*${NC}\n"
  fi
done

section_header "📋 FINAL ELASTICSEARCH INDICES AND ALIASES"

printf "${BLUE}Indices:${NC}\n"
curl $ES_AUTH -s -X GET "localhost:9200/_cat/indices?v"

printf "\n${BLUE}Aliases:${NC}\n"
curl $ES_AUTH -s -X GET "localhost:9200/_cat/aliases?v"

section_header "✅ REINDEXING COMPLETE"

printf "${GREEN}All models have been reindexed successfully!${NC}\n"
printf "${GREEN}You can now use the stable alias names (users_index, organizations_index, etc.) in your application.${NC}\n"
