#!/bin/bash

cd "$(dirname "$0")"

# Java 21 명시적 설정
export JAVA_HOME="/Users/coziny/Library/Java/JavaVirtualMachines/temurin-21.0.9/Contents/Home"
export PATH="$JAVA_HOME/bin:$PATH"

echo "=========================================="
echo "  LogHunter 시작"
echo "=========================================="
echo "  Java: $(java -version 2>&1 | head -1)"
echo "=========================================="

# 포트 정리
echo "[1/4] 8080 포트 정리..."
lsof -ti:8080 | xargs kill -9 2>/dev/null

# 프론트엔드 빌드
echo "[2/4] 프론트엔드 빌드..."
cd frontend
npm install --include=dev
npm run build
cd ..

# static 폴더 확인
echo "[3/4] 빌드 결과 확인..."
if [ -f "src/main/resources/static/index.html" ]; then
    echo "  ✓ index.html 확인됨"
else
    echo "  ✗ index.html 없음 - 빌드 실패"
    exit 1
fi

# 서버 실행
echo "[4/4] 서버 실행..."
echo ""
echo "=========================================="
echo "  http://localhost:8080"
echo "  종료: Ctrl+C"
echo "=========================================="
echo ""

./gradlew bootRun
