#!/bin/bash

# Promotion Module Integration Test Runner
# This script runs comprehensive integration tests for the promotion module

echo "🚀 Starting Promotion Module Integration Tests..."
echo "=================================================="

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

# Function to print colored output
print_status() {
    local status=$1
    local message=$2
    case $status in
        "SUCCESS")
            echo -e "${GREEN}✅ $message${NC}"
            ;;
        "ERROR")
            echo -e "${RED}❌ $message${NC}"
            ;;
        "WARNING")
            echo -e "${YELLOW}⚠️  $message${NC}"
            ;;
        "INFO")
            echo -e "${BLUE}ℹ️  $message${NC}"
            ;;
    esac
}

# Check if we're in the correct directory
if [ ! -f "artisan" ]; then
    print_status "ERROR" "Please run this script from the Laravel project root directory"
    exit 1
fi

# Check if PHP is available
if ! command -v php &> /dev/null; then
    print_status "ERROR" "PHP is not installed or not in PATH"
    exit 1
fi

# Check if Composer is available
if ! command -v composer &> /dev/null; then
    print_status "ERROR" "Composer is not installed or not in PATH"
    exit 1
fi

print_status "INFO" "Environment checks passed"

# Install/update dependencies if needed
print_status "INFO" "Checking dependencies..."
if [ ! -d "vendor" ]; then
    print_status "INFO" "Installing dependencies..."
    composer install --no-interaction --prefer-dist
fi

# Run database migrations
print_status "INFO" "Setting up test database..."
php artisan migrate:fresh --env=testing --force

# Seed test data
print_status "INFO" "Seeding test data..."
php artisan db:seed --class=ConditionAndActionTypesSeeder --env=testing --force
php artisan db:seed --class=TestPromotionSeeder --env=testing --force

# Run the integration tests
print_status "INFO" "Running Promotion Integration Tests..."

echo ""
echo "📋 Test Categories:"
echo "1. Core Promotion Integration Tests"
echo "2. Sales/Return/Exchange Cycle Integration Tests"
echo "3. Frontend Integration Tests"
echo ""

# Run core integration tests
print_status "INFO" "Running Core Promotion Integration Tests..."
php artisan test tests/Feature/PromotionIntegration/PromotionIntegrationTest.php --env=testing

if [ $? -eq 0 ]; then
    print_status "SUCCESS" "Core Promotion Integration Tests passed"
else
    print_status "ERROR" "Core Promotion Integration Tests failed"
    exit 1
fi

echo ""

# Run sales cycle integration tests
print_status "INFO" "Running Sales/Return/Exchange Cycle Integration Tests..."
php artisan test tests/Feature/PromotionIntegration/PromotionSalesCycleIntegrationTest.php --env=testing

if [ $? -eq 0 ]; then
    print_status "SUCCESS" "Sales Cycle Integration Tests passed"
else
    print_status "ERROR" "Sales Cycle Integration Tests failed"
    exit 1
fi

echo ""

# Run frontend integration tests
print_status "INFO" "Running Frontend Integration Tests..."
php artisan test tests/Feature/PromotionIntegration/PromotionFrontendIntegrationTest.php --env=testing

if [ $? -eq 0 ]; then
    print_status "SUCCESS" "Frontend Integration Tests passed"
else
    print_status "ERROR" "Frontend Integration Tests failed"
    exit 1
fi

echo ""

# Run all promotion tests together for a comprehensive report
print_status "INFO" "Running All Promotion Tests Together..."
php artisan test tests/Feature/PromotionIntegration/ --env=testing --coverage

if [ $? -eq 0 ]; then
    print_status "SUCCESS" "All Promotion Integration Tests passed!"
else
    print_status "ERROR" "Some Promotion Integration Tests failed"
    exit 1
fi

echo ""
echo "🎉 Promotion Module Integration Test Summary:"
echo "============================================="
print_status "SUCCESS" "✅ Core Promotion CRUD Operations"
print_status "SUCCESS" "✅ Voucher Management System"
print_status "SUCCESS" "✅ Promotion Service Logic"
print_status "SUCCESS" "✅ Sales Transaction Integration"
print_status "SUCCESS" "✅ Return Transaction Handling"
print_status "SUCCESS" "✅ Exchange Transaction Handling"
print_status "SUCCESS" "✅ Voucher Validation System"
print_status "SUCCESS" "✅ Usage Tracking & Limits"
print_status "SUCCESS" "✅ Branch Scope Management"
print_status "SUCCESS" "✅ Stackable Promotions"
print_status "SUCCESS" "✅ Admin Panel API Integration"
print_status "SUCCESS" "✅ Frontend Data Flow"
print_status "SUCCESS" "✅ Error Handling & Validation"
print_status "SUCCESS" "✅ Permission Enforcement"

echo ""
print_status "INFO" "All tests completed successfully!"
print_status "INFO" "The promotion module is fully integrated and working correctly."

echo ""
echo "📊 Test Coverage Areas:"
echo "- Admin Panel Promotion Management"
echo "- Voucher Code Generation & Management"
echo "- Promotion Application Logic"
echo "- Sales/Return/Exchange Cycle Integration"
echo "- API Endpoint Functionality"
echo "- Database Integration"
echo "- Permission & Security"
echo "- Error Handling & Validation"

echo ""
print_status "SUCCESS" "🚀 Promotion Module is ready for production!"
