#!/bin/bash

# CloudToLocalLLM - Local CI/CD Workflow Test Script
# This script simulates the CI/CD workflow to validate the build and deployment process locally.

set -euo pipefail

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

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

# Logging functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check for required environment variables
check_env_vars() {
    log_info "Checking for required environment variables..."
    required_vars=(
        "GCP_PROJECT_ID"
        "GCP_SA_KEY"
        "FIREBASE_PROJECT_ID"
        "JWT_SECRET"
        "AUTH0_AUDIENCE"
    )

    for var in "${required_vars[@]}"; do
        if [ -z "${!var:-}" ]; then
            log_error "Required environment variable $var is not set."
            log_error "Please set it before running the script."
            exit 1
        fi
    done
    log_success "All required environment variables are set."
}

# Authenticate with Google Cloud
authenticate_gcloud() {
    log_info "Authenticating with Google Cloud..."
    echo "${GCP_SA_KEY}" > /tmp/gcp-sa-key.json
    gcloud auth activate-service-account --key-file=/tmp/gcp-sa-key.json
    gcloud config set project "${GCP_PROJECT_ID}"
    rm /tmp/gcp-sa-key.json
    log_success "Successfully authenticated with Google Cloud."
}

# Main function
main() {
    log_info "Starting local CI/CD workflow test..."

    check_env_vars
    authenticate_gcloud

    log_info "Step 1: Building Docker images..."
    docker build -f "$PROJECT_ROOT/config/cloudrun/Dockerfile.web-cloudrun" -t "test-web:latest" "$PROJECT_ROOT"
    docker build -f "$PROJECT_ROOT/config/cloudrun/Dockerfile.api-cloudrun" -t "test-api:latest" "$PROJECT_ROOT"
    docker build -f "$PROJECT_ROOT/config/cloudrun/Dockerfile.streaming-proxy-cloudrun" -t "test-streaming:latest" "$PROJECT_ROOT"
    log_success "All Docker images built successfully."

    log_info "Step 2: Simulating deployment validation..."
    log_info "Note: This step will not actually deploy anything, but it will check the health of the services if they are running locally."
    
    chmod +x "$PROJECT_ROOT/scripts/cloudrun/health-check.sh"
    "$PROJECT_ROOT/scripts/cloudrun/health-check.sh"

    log_success "Local CI/CD workflow test completed successfully."
}

main "$@"
