#!/bin/bash
# ============================================
# BreakPilot Environment Setup Tests
# ============================================
# Tests for environment configuration and scripts
#
# Usage: ./scripts/test-environment-setup.sh
# ============================================

set -e

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

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

# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0

# Test result function
test_result() {
    local name=$1
    local result=$2
    TESTS_RUN=$((TESTS_RUN + 1))

    if [ "$result" -eq 0 ]; then
        echo -e "${GREEN}✓${NC} $name"
        TESTS_PASSED=$((TESTS_PASSED + 1))
    else
        echo -e "${RED}✗${NC} $name"
        TESTS_FAILED=$((TESTS_FAILED + 1))
    fi
}

cd "$ROOT_DIR"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Environment Setup Tests${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# ==========================================
# File Existence Tests
# ==========================================
echo -e "${YELLOW}Testing: File Existence${NC}"

# Test .gitignore
test -f ".gitignore"
test_result ".gitignore exists" $?

# Test .env files
test -f ".env.dev"
test_result ".env.dev exists" $?

test -f ".env.staging"
test_result ".env.staging exists" $?

test -f ".env.example"
test_result ".env.example exists" $?

# Test Docker Compose files
test -f "docker-compose.yml"
test_result "docker-compose.yml exists" $?

test -f "docker-compose.override.yml"
test_result "docker-compose.override.yml exists" $?

test -f "docker-compose.staging.yml"
test_result "docker-compose.staging.yml exists" $?

# Test Scripts
test -x "scripts/env-switch.sh"
test_result "scripts/env-switch.sh is executable" $?

test -x "scripts/start.sh"
test_result "scripts/start.sh is executable" $?

test -x "scripts/stop.sh"
test_result "scripts/stop.sh is executable" $?

test -x "scripts/promote.sh"
test_result "scripts/promote.sh is executable" $?

test -x "scripts/status.sh"
test_result "scripts/status.sh is executable" $?

# Test Documentation
test -f "docs/architecture/environments.md"
test_result "docs/architecture/environments.md exists" $?

test -f "docs/guides/environment-setup.md"
test_result "docs/guides/environment-setup.md exists" $?

echo ""

# ==========================================
# Environment File Content Tests
# ==========================================
echo -e "${YELLOW}Testing: Environment File Content${NC}"

# Test .env.dev content
grep -q "ENVIRONMENT=development" .env.dev 2>/dev/null
test_result ".env.dev contains ENVIRONMENT=development" $?

grep -q "COMPOSE_PROJECT_NAME=breakpilot-dev" .env.dev 2>/dev/null
test_result ".env.dev contains correct COMPOSE_PROJECT_NAME" $?

grep -q "POSTGRES_DB=breakpilot_dev" .env.dev 2>/dev/null
test_result ".env.dev contains correct POSTGRES_DB" $?

# Test .env.staging content
grep -q "ENVIRONMENT=staging" .env.staging 2>/dev/null
test_result ".env.staging contains ENVIRONMENT=staging" $?

grep -q "COMPOSE_PROJECT_NAME=breakpilot-staging" .env.staging 2>/dev/null
test_result ".env.staging contains correct COMPOSE_PROJECT_NAME" $?

grep -q "POSTGRES_DB=breakpilot_staging" .env.staging 2>/dev/null
test_result ".env.staging contains correct POSTGRES_DB" $?

grep -q "DEBUG=false" .env.staging 2>/dev/null
test_result ".env.staging has DEBUG=false" $?

echo ""

# ==========================================
# Docker Compose Syntax Tests
# ==========================================
echo -e "${YELLOW}Testing: Docker Compose Syntax${NC}"

# Test docker-compose.override.yml syntax
docker compose -f docker-compose.yml -f docker-compose.override.yml config > /dev/null 2>&1
test_result "docker-compose.override.yml syntax valid" $?

# Test docker-compose.staging.yml syntax
docker compose -f docker-compose.yml -f docker-compose.staging.yml config > /dev/null 2>&1
test_result "docker-compose.staging.yml syntax valid" $?

echo ""

# ==========================================
# Git Repository Tests
# ==========================================
echo -e "${YELLOW}Testing: Git Repository${NC}"

# Test git repo exists
test -d ".git"
test_result "Git repository initialized" $?

# Test branches exist
git branch --list develop | grep -q develop
test_result "develop branch exists" $?

git branch --list staging | grep -q staging
test_result "staging branch exists" $?

git branch --list main | grep -q main
test_result "main branch exists" $?

# Test current branch
CURRENT_BRANCH=$(git branch --show-current)
[ "$CURRENT_BRANCH" = "develop" ]
test_result "Current branch is develop" $?

echo ""

# ==========================================
# .gitignore Tests
# ==========================================
echo -e "${YELLOW}Testing: .gitignore Content${NC}"

# Test that .env is ignored but .env.dev is not
grep -q "^\.env$" .gitignore 2>/dev/null
test_result ".gitignore excludes .env" $?

grep -q "!\.env\.dev" .gitignore 2>/dev/null
test_result ".gitignore includes .env.dev" $?

grep -q "!\.env\.staging" .gitignore 2>/dev/null
test_result ".gitignore includes .env.staging" $?

grep -q "__pycache__" .gitignore 2>/dev/null
test_result ".gitignore excludes __pycache__" $?

grep -q "node_modules" .gitignore 2>/dev/null
test_result ".gitignore excludes node_modules" $?

echo ""

# ==========================================
# Script Functionality Tests
# ==========================================
echo -e "${YELLOW}Testing: Script Functionality${NC}"

# Test env-switch.sh creates .env
./scripts/env-switch.sh dev > /dev/null 2>&1
test -f ".env"
test_result "env-switch.sh creates .env file" $?

# Verify .env content matches .env.dev
grep -q "ENVIRONMENT=development" .env 2>/dev/null
test_result "env-switch.sh sets correct environment" $?

# Test switching to staging
./scripts/env-switch.sh staging > /dev/null 2>&1
grep -q "ENVIRONMENT=staging" .env 2>/dev/null
test_result "env-switch.sh can switch to staging" $?

# Switch back to dev
./scripts/env-switch.sh dev > /dev/null 2>&1

echo ""

# ==========================================
# Summary
# ==========================================
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Test Summary${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "Tests run:    $TESTS_RUN"
echo -e "Tests passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Tests failed: ${RED}$TESTS_FAILED${NC}"
echo ""

if [ $TESTS_FAILED -eq 0 ]; then
    echo -e "${GREEN}All tests passed!${NC}"
    exit 0
else
    echo -e "${RED}Some tests failed.${NC}"
    exit 1
fi
