# 아리 — 작업 러너 (make 대체). `just --list` 로 전체 보기.
# 개발 포트는 흔한 dev 포트(3000/5173/8000/8080)와 충돌을 피해 31xxx 대역 사용.

be_port := "31800"   # 백엔드 API
fe_port := "31300"   # 프론트(package.json `dev` 스크립트도 -p {{fe_port}})

# 기본: 사용 가능한 레시피 목록
default:
    @just --list

# 의존성 설치 (프론트 pnpm + 백엔드 uv)
install:
    cd frontend && pnpm install
    cd backend  && uv sync

# 실행: `just run dev`(둘 다) | `just run fe` | `just run be`
run target="dev":
    #!/usr/bin/env bash
    set -euo pipefail
    case "{{target}}" in
      dev)
        echo "▶ backend :{{be_port}} / frontend :{{fe_port}} (Ctrl-C 로 종료)"
        trap 'kill 0' INT TERM EXIT
        ( cd backend  && uv run uvicorn app.main:app --reload --port {{be_port}} ) &
        ( cd frontend && pnpm dev ) &
        wait ;;
      fe) cd frontend && pnpm dev ;;
      be) cd backend  && uv run uvicorn app.main:app --reload --port {{be_port}} ;;
      *)  echo "알 수 없는 대상: {{target}} (dev|fe|be)"; exit 1 ;;
    esac

# 테스트 (백엔드 pytest + 프론트 vitest)
test:
    cd backend  && uv run pytest
    cd frontend && pnpm test

# E2E (Playwright). 백엔드는 ARI_ALLOW_TEST_RESET=1 LLM_PROVIDER=heuristic 로 기동해야 전부 통과.
e2e:
    cd frontend && pnpm exec playwright test --workers=1

# 린트 (ruff + eslint) / 포맷
lint:
    cd backend  && uv run ruff check .
    cd frontend && pnpm lint

format:
    cd backend  && uv run ruff format . && uv run black .
    cd frontend && pnpm format

# health 엔드포인트 확인
health:
    @echo "── /api/health ──"     && curl -s http://localhost:{{be_port}}/api/health     | python3 -m json.tool
    @echo "── /api/llm/health ──" && curl -s http://localhost:{{be_port}}/api/llm/health | python3 -m json.tool

# DB 시드 / 마이그레이션
seed:
    cd backend && uv run python -m app.seed

migrate:
    cd backend && uv run alembic upgrade head

# 빌드 산출물 정리
clean:
    rm -rf frontend/.next backend/ari.db backend/.pytest_cache
