#!/bin/bash

# Ensure the script exits if any command fails
set -e

echo "Starting monorepo refactoring..."

# --- 1. Create new directories ---
echo "Creating new directories..."

mkdir -p backend/src/auth
mkdir -p backend/src/common
mkdir -p backend/deprecated-migrations
mkdir -p frontend/src/components
mkdir -p frontend/src/pages
mkdir -p frontend/src/store
mkdir -p docs

echo "Directories created."

# --- 2. Move files to backend/ ---
echo "Moving backend files..."

mv admin.guard.ts backend/src/auth/
mv jwt-auth.guard.ts backend/src/auth/
mv jwt.strategy.ts backend/src/auth/
mv rbac.middleware.ts backend/src/common/
mv main.ts backend/src/
mv nest-cli.json backend/
mv tsconfig.json backend/
mv tsconfig.build.json backend/
mv seed.ts backend/

# Special handling for prisma directory and prisma.service.ts
# First, move the existing prisma directory to backend/
mv prisma backend/

# Then, move prisma.service.ts into the newly moved backend/prisma directory
mv prisma.service.ts backend/prisma/

# Move old migrations (not Prisma's) to deprecated-migrations
if [ -d "migrations" ]; then
    mv migrations backend/deprecated-migrations/
else
    echo "Warning: 'migrations' directory not found at root. Skipping move."
fi

# Move schema.sql if it exists and is related to backend
if [ -f "schema.sql" ]; then
    mv schema.sql backend/prisma/
else
    echo "Warning: 'schema.sql' not found at root. Skipping move."
fi

echo "Backend files moved."

# --- 3. Move files to frontend/ ---
echo "Moving frontend files..."

mv App.tsx frontend/src/
mv CreateTourModal.tsx frontend/src/components/
mv ExpenseManager.tsx frontend/src/components/ # Assuming this is a component
mv ExploreMap.tsx frontend/src/pages/ # Assuming this is a page
mv index.tsx frontend/src/
mv ItineraryTimeline.tsx frontend/src/components/ # Assuming this is a component
mv LandingPage.tsx frontend/src/pages/
mv LoginModal.tsx frontend/src/components/
mv SignupPage.tsx frontend/src/pages/
mv TourDetailPage.tsx frontend/src/pages/
mv useTourStore.ts frontend/src/store/
mv vite.config.ts frontend/
mv tailwind.config.ts frontend/
mv postcss.config.js frontend/
mv index.css frontend/src/

echo "Frontend files moved."

# --- 4. Move files to docs/ ---
echo "Moving documentation files..."

mv ARCHITECTURE.md docs/
mv UITourDesign.md docs/

echo "Documentation files moved."

echo "Monorepo refactoring complete. Please verify the new structure."
echo "Next, you will need to update import paths and configuration files."
