#!/bin/bash
# Type Safety Validation Script
# Run before committing code to prevent type issues

set -e

echo "🔍 Type Safety Validation"
echo "========================="
echo ""

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

ERRORS=0

# Check 1: Duplicate Type Definitions
echo "📋 Check 1: Duplicate type definitions..."
DUPLICATES=$(find packages apps -name "*.ts" -o -name "*.vue" | \
  grep -v "OLD_PROJECT" | \
  xargs grep -l "export interface Plan[^A-Za-z]\|export interface Stage[^A-Za-z]\|export interface UserProfile[^A-Za-z]" 2>/dev/null | \
  grep -v "packages/types/src/" | wc -l | tr -d ' ')

if [ "$DUPLICATES" -gt 0 ]; then
  echo -e "${RED}❌ Found duplicate type definitions:${NC}"
  find packages apps -name "*.ts" -o -name "*.vue" | \
    grep -v "OLD_PROJECT" | \
    xargs grep -l "export interface Plan[^A-Za-z]\|export interface Stage[^A-Za-z]\|export interface UserProfile[^A-Za-z]" 2>/dev/null | \
    grep -v "packages/types/src/"
  ERRORS=$((ERRORS + 1))
else
  echo -e "${GREEN}✅ No duplicate type definitions${NC}"
fi
echo ""

# Check 2: Wrong Property Names (plan.title instead of plan.name)
echo "📋 Check 2: Wrong property names..."
WRONG_PROPS=$(grep -r "plan\.title\|stage\.description" apps/web/src/modules/planner/ 2>/dev/null | wc -l | tr -d ' ')

if [ "$WRONG_PROPS" -gt 0 ]; then
  echo -e "${RED}❌ Found wrong property names:${NC}"
  grep -rn "plan\.title\|stage\.description" apps/web/src/modules/planner/ 2>/dev/null || true
  echo -e "${YELLOW}Should use: plan.name, stage.notes${NC}"
  ERRORS=$((ERRORS + 1))
else
  echo -e "${GREEN}✅ All property names correct${NC}"
fi
echo ""

# Check 3: Package Name Typos
echo "📋 Check 3: Package name typos..."
TYPOS=$(grep -r "@my-road-trip/types\|@myroadtrip/types" apps/ packages/ 2>/dev/null | wc -l | tr -d ' ')

if [ "$TYPOS" -gt 0 ]; then
  echo -e "${RED}❌ Found package name typos:${NC}"
  grep -rn "@my-road-trip/types\|@myroadtrip/types" apps/ packages/ 2>/dev/null || true
  echo -e "${YELLOW}Should use: @my-roadtrip/types${NC}"
  ERRORS=$((ERRORS + 1))
else
  echo -e "${GREEN}✅ Package names correct${NC}"
fi
echo ""

# Check 4: Deprecated Types Usage
echo "📋 Check 4: Deprecated types usage..."
DEPRECATED=$(grep -r "import.*\bUserProfile\b.*from.*@my-roadtrip/types\|import.*UserAccountSnapshot.*from.*@my-roadtrip/types\|import.*PlanSettings" apps/ 2>/dev/null | \
  grep -v "__tests__\|\.test\.ts" | wc -l | tr -d ' ')

if [ "$DEPRECATED" -gt 0 ]; then
  echo -e "${RED}❌ Found deprecated type imports:${NC}"
  grep -rn "import.*\bUserProfile\b.*from.*@my-roadtrip/types\|import.*UserAccountSnapshot.*from.*@my-roadtrip/types\|import.*PlanSettings" apps/ 2>/dev/null | \
    grep -v "__tests__\|\.test\.ts" || true
  echo -e "${YELLOW}Should use: User (not UserProfile/UserAccountSnapshot), RouteSettings (not PlanSettings)${NC}"
  ERRORS=$((ERRORS + 1))
else
  echo -e "${GREEN}✅ No deprecated types in use${NC}"
fi
echo ""

# Check 5: Type Consistency in planner.types.ts
echo "📋 Check 5: Type definition consistency..."
TYPE_FILE="packages/types/src/planner.types.ts"
if [ -f "$TYPE_FILE" ]; then
  # Check Plan has 'name' property
  if grep -q "name: string" "$TYPE_FILE" && grep -q "export interface Plan" "$TYPE_FILE"; then
    echo -e "${GREEN}✅ Plan type has 'name' property${NC}"
  else
    echo -e "${RED}❌ Plan type missing 'name' property${NC}"
    ERRORS=$((ERRORS + 1))
  fi
  
  # Check Stage has 'planId' property
  if grep -q "planId: string" "$TYPE_FILE" && grep -q "export interface Stage" "$TYPE_FILE"; then
    echo -e "${GREEN}✅ Stage type has 'planId' property${NC}"
  else
    echo -e "${RED}❌ Stage type missing 'planId' property${NC}"
    ERRORS=$((ERRORS + 1))
  fi
else
  echo -e "${RED}❌ planner.types.ts not found${NC}"
  ERRORS=$((ERRORS + 1))
fi
echo ""

# Check 6: TypeScript Errors
echo "📋 Check 6: TypeScript type checking..."
if command -v pnpm &> /dev/null; then
  echo "Running pnpm type-check..."
  if pnpm type-check 2>&1 | grep -q "ERROR"; then
    PLANNER_ERRORS=$(pnpm type-check 2>&1 | grep "planner" | grep "error TS" | wc -l | tr -d ' ')
    if [ "$PLANNER_ERRORS" -gt 0 ]; then
      echo -e "${RED}❌ Found $PLANNER_ERRORS TypeScript errors in planner module${NC}"
      ERRORS=$((ERRORS + 1))
    else
      echo -e "${YELLOW}⚠️  TypeScript errors exist, but not in planner module${NC}"
    fi
  else
    echo -e "${GREEN}✅ TypeScript check passed${NC}"
  fi
else
  echo -e "${YELLOW}⚠️  pnpm not found, skipping type check${NC}"
fi
echo ""

# Summary
echo "========================="
if [ $ERRORS -eq 0 ]; then
  echo -e "${GREEN}✅ All type safety checks passed!${NC}"
  echo ""
  echo "Safe to commit. Type system is consistent."
  exit 0
else
  echo -e "${RED}❌ Found $ERRORS type safety issues${NC}"
  echo ""
  echo "Please fix the issues above before committing."
  echo "See .github/instructions/TYPE_SAFETY_PROTOCOL.md for guidance."
  exit 1
fi
