#!/usr/bin/env bash
# ──────────────────────────────────────────────────────────────────
# Cosmos DB — Telemetry Container Indexing Policies
#
# Run once to apply composite indexes optimized for the query patterns
# used by the telemetry module (events, clusters, policies).
#
# Prerequisites:
#   - Azure CLI installed and authenticated
#   - COSMOS_ACCOUNT_NAME and COSMOS_RESOURCE_GROUP env vars set
#   - Database name: lysnrai (or set COSMOS_DATABASE)
# ──────────────────────────────────────────────────────────────────

set -euo pipefail

ACCOUNT="${COSMOS_ACCOUNT_NAME:?Set COSMOS_ACCOUNT_NAME}"
RG="${COSMOS_RESOURCE_GROUP:?Set COSMOS_RESOURCE_GROUP}"
DB="${COSMOS_DATABASE:-lysnrai}"

echo "🔧 Applying telemetry indexing policies..."
echo "   Account: $ACCOUNT | Database: $DB | Resource Group: $RG"

# ── telemetry_events ─────────────────────────────────────────────
# Query patterns:
#   - WHERE productId = X AND occurredAt >= Y ORDER BY occurredAt DESC
#   - + userId, platform, channel, eventType, module filters
echo "📦 telemetry_events..."
az cosmosdb sql container update \
  --account-name "$ACCOUNT" \
  --resource-group "$RG" \
  --database-name "$DB" \
  --name telemetry_events \
  --idx '{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
      { "path": "/productId/?" },
      { "path": "/pk/?" },
      { "path": "/userId/?" },
      { "path": "/anonymousInstallId/?" },
      { "path": "/platform/?" },
      { "path": "/channel/?" },
      { "path": "/eventType/?" },
      { "path": "/module/?" },
      { "path": "/eventName/?" },
      { "path": "/occurredAt/?" },
      { "path": "/receivedAt/?" },
      { "path": "/osFamily/?" },
      { "path": "/appVersion/?" },
      { "path": "/buildNumber/?" }
    ],
    "excludedPaths": [
      { "path": "/message/?" },
      { "path": "/stackTrace/?" },
      { "path": "/tags/*" },
      { "path": "/metrics/*" },
      { "path": "/context/*" },
      { "path": "/*" }
    ],
    "compositeIndexes": [
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/occurredAt", "order": "descending" }
      ],
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/platform", "order": "ascending" },
        { "path": "/occurredAt", "order": "descending" }
      ],
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/eventType", "order": "ascending" },
        { "path": "/occurredAt", "order": "descending" }
      ],
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/userId", "order": "ascending" },
        { "path": "/occurredAt", "order": "descending" }
      ]
    ]
  }' \
  --output none

# ── telemetry_error_clusters ─────────────────────────────────────
# Query patterns:
#   - WHERE productId = X ORDER BY totalCount DESC
#   - + platform, module, lastSeenAt filters
echo "📦 telemetry_error_clusters..."
az cosmosdb sql container update \
  --account-name "$ACCOUNT" \
  --resource-group "$RG" \
  --database-name "$DB" \
  --name telemetry_error_clusters \
  --idx '{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
      { "path": "/productId/?" },
      { "path": "/pk/?" },
      { "path": "/platform/?" },
      { "path": "/module/?" },
      { "path": "/severity/?" },
      { "path": "/status/?" },
      { "path": "/totalCount/?" },
      { "path": "/lastSeenAt/?" },
      { "path": "/firstSeenAt/?" }
    ],
    "excludedPaths": [
      { "path": "/affectedVersions/*" },
      { "path": "/affectedUserIds/*" },
      { "path": "/affectedInstallIds/*" },
      { "path": "/affectedOsFamilies/*" },
      { "path": "/*" }
    ],
    "compositeIndexes": [
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/totalCount", "order": "descending" }
      ],
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/status", "order": "ascending" },
        { "path": "/totalCount", "order": "descending" }
      ]
    ]
  }' \
  --output none

# ── telemetry_collection_policies ────────────────────────────────
# Query patterns:
#   - WHERE productId = X ORDER BY priority DESC
echo "📦 telemetry_collection_policies..."
az cosmosdb sql container update \
  --account-name "$ACCOUNT" \
  --resource-group "$RG" \
  --database-name "$DB" \
  --name telemetry_collection_policies \
  --idx '{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
      { "path": "/productId/?" },
      { "path": "/priority/?" },
      { "path": "/enabled/?" }
    ],
    "excludedPaths": [
      { "path": "/targeting/*" },
      { "path": "/*" }
    ],
    "compositeIndexes": [
      [
        { "path": "/productId", "order": "ascending" },
        { "path": "/priority", "order": "descending" }
      ]
    ]
  }' \
  --output none

echo "✅ Telemetry indexing policies applied successfully."
