#!/bin/bash
#
# Script to systematically fix common lint errors
# Adds return types to React components and fixes type issues
#

set -euo pipefail

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

echo "Fixing lint errors systematically..."

# Fix all page components - add return types
find apps/portal-internal/src/app apps/portal-public/src/app -name "page.tsx" -type f | while read -r file; do
  if ! grep -q "export default function.*JSX.Element" "$file"; then
    # Add return type to default export function
    sed -i 's/export default function \([^(]*\)() {/export default function \1(): JSX.Element {/g' "$file"
    echo "Fixed: $file"
  fi
done

# Fix layout files
find apps/portal-internal/src/app apps/portal-public/src/app -name "layout.tsx" -type f | while read -r file; do
  if ! grep -q "export default function.*JSX.Element" "$file"; then
    sed -i 's/export default function RootLayout({/export default function RootLayout({/g' "$file"
    sed -i 's/}: {/}: {/g' "$file"
    sed -i 's/children: ReactNode;$/children: ReactNode;\n}): JSX.Element {/g' "$file"
    echo "Fixed: $file"
  fi
done

echo "Lint error fixes applied. Run 'pnpm lint' to verify."

