#!/bin/bash
# setup.sh - Setup script untuk Bahasa Indonesia Auto Formalizer

echo "🇮🇩 Setting up Bahasa Indonesia Auto Formalizer..."
echo "============================================="

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

# Check Node.js installation
check_node() {
    if ! command -v node &> /dev/null; then
        echo -e "${RED}❌ Node.js tidak terinstall${NC}"
        echo "Silakan install Node.js terlebih dahulu: https://nodejs.org/"
        exit 1
    fi
    
    NODE_VERSION=$(node -v)
    echo -e "${GREEN}✅ Node.js terdeteksi: $NODE_VERSION${NC}"
}

# Check npm installation
check_npm() {
    if ! command -v npm &> /dev/null; then
        echo -e "${RED}❌ npm tidak terinstall${NC}"
        exit 1
    fi
    
    NPM_VERSION=$(npm -v)
    echo -e "${GREEN}✅ npm terdeteksi: $NPM_VERSION${NC}"
}

# Create directory structure
create_directories() {
    echo -e "\n${YELLOW}📁 Membuat struktur direktori...${NC}"
    
    mkdir -p src
    mkdir -p test
    mkdir -p demo
    mkdir -p api
    mkdir -p examples
    mkdir -p data
    mkdir -p .github/workflows
    
    echo -e "${GREEN}✅ Direktori dibuat${NC}"
}

# Install dependencies
install_dependencies() {
    echo -e "\n${YELLOW}📦 Installing dependencies...${NC}"
    
    if [ -f "package.json" ]; then
        npm install
        echo -e "${GREEN}✅ Dependencies terinstall${NC}"
    else
        echo -e "${RED}❌ package.json tidak ditemukan${NC}"
        exit 1
    fi
}

# Run tests
run_tests() {
    echo -e "\n${YELLOW}🧪 Menjalankan tests...${NC}"
    
    if npm test; then
        echo -e "${GREEN}✅ Semua test berhasil${NC}"
    else
        echo -e "${RED}❌ Beberapa test gagal${NC}"
    fi
}

# Create sample data
create_sample_data() {
    echo -e "\n${YELLOW}📝 Membuat sample data...${NC}"
    
    cat > data/sample_informal.txt << EOF
gue mau nanya dong, lo udah selesai belom?
makasih bgt ya udh bantuin gue kemaren
sorry gue telat, jalanan macet parah
btw meeting besok jadi ga?
kalo ada waktu, tolong cek email gue ya
EOF
    
    echo -e "${GREEN}✅ Sample data dibuat di data/sample_informal.txt${NC}"
}

# Setup Git hooks
setup_git_hooks() {
    echo -e "\n${YELLOW}🔧 Setting up Git hooks...${NC}"
    
    if [ -d ".git" ]; then
        # Pre-commit hook untuk formatting
        cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
npm run lint
npm run format
EOF
        chmod +x .git/hooks/pre-commit
        
        echo -e "${GREEN}✅ Git hooks configured${NC}"
    else
        echo -e "${YELLOW}⚠️  Git repository tidak terdeteksi, skipping hooks${NC}"
    fi
}

# Main setup flow
main() {
    echo -e "\n${YELLOW}🚀 Starting setup process...${NC}\n"
    
    check_node
    check_npm
    create_directories
    install_dependencies
    create_sample_data
    setup_git_hooks
    
    echo -e "\n${GREEN}============================================="
    echo -e "✨ Setup selesai!${NC}"
    echo -e "\nLangkah selanjutnya:"
    echo -e "1. Test CLI: ${YELLOW}npm run dev${NC}"
    echo -e "2. Run interactive mode: ${YELLOW}node src/cli.js interactive${NC}"
    echo -e "3. Process sample file: ${YELLOW}node src/cli.js file data/sample_informal.txt${NC}"
    echo -e "4. Lihat dokumentasi: ${YELLOW}cat GETTING_STARTED.md${NC}"
    echo -e "\n${GREEN}Happy formalizing! 🇮🇩${NC}"
}

# Run main function
main