#!/bin/bash

# Type Safety Pre-commit Check Script
# This script ensures type safety before commits by running linting and type checking

set -e

echo "🔍 Running Type Safety Pre-commit Checks..."

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

# Function to print colored output
print_status() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Check if we're in the right directory
if [ ! -f "package.json" ]; then
    print_error "Must run from project root directory"
    exit 1
fi

# Step 1: Run ESLint with TypeScript rules
print_status "Running ESLint TypeScript checks..."
if npm run lint > /dev/null 2>&1; then
    print_status "ESLint passed - no type safety issues found"
else
    print_error "ESLint failed - type safety issues detected"
    echo ""
    echo "Running lint with details..."
    npm run lint
    echo ""
    print_error "Please fix the above type safety issues before committing"
    exit 1
fi

# Step 2: Run TypeScript type checking
print_status "Running TypeScript type checking..."
if npm run type-check > /dev/null 2>&1; then
    print_status "TypeScript compilation passed"
else
    print_error "TypeScript compilation failed"
    echo ""
    echo "Running type check with details..."
    npm run type-check
    echo ""
    print_error "Please fix the above TypeScript errors before committing"
    exit 1
fi

# Step 3: Check for any remaining 'any' types
print_status "Scanning for any remaining 'any' types..."
ANY_COUNT=$(grep -r "any" src/ --include="*.ts" --include="*.vue" | grep -v "// eslint-disable" | grep -v "eslint-disable-next-line" | wc -l)

if [ "$ANY_COUNT" -eq 0 ]; then
    print_status "No 'any' types found in source code"
else
    print_warning "Found $ANY_COUNT instances of 'any' type usage"
    echo ""
    echo "Instances found:"
    grep -r "any" src/ --include="*.ts" --include="*.vue" | grep -v "// eslint-disable" | grep -v "eslint-disable-next-line" || true
    echo ""
    print_error "Please replace 'any' types with proper TypeScript types before committing"
    exit 1
fi

# Step 4: Verify database migration status
print_status "Checking database migration status..."
if grep -r "databaseUtil" src/ --include="*.ts" --include="*.vue" > /dev/null 2>&1; then
    print_warning "Found databaseUtil imports - ensure migration is complete"
    echo ""
    echo "Files with databaseUtil imports:"
    grep -r "databaseUtil" src/ --include="*.ts" --include="*.vue" | head -5 || true
    echo ""
    print_warning "Consider completing database migration to PlatformServiceMixin"
else
    print_status "No databaseUtil imports found - migration appears complete"
fi

# All checks passed
echo ""
print_status "All type safety checks passed! 🎉"
print_status "Your code is ready for commit"
echo ""
echo "📚 Remember to follow the Type Safety Guidelines:"
echo "   - docs/typescript-type-safety-guidelines.md"
echo "   - Use proper error handling patterns"
echo "   - Leverage existing type definitions"
echo "   - Run 'npm run lint-fix' for automatic fixes"

exit 0
