# DreamChat Justfile
# https://just.systems/
#
# Installation:
#   cargo install just
#   OR
#   brew install just  (macOS)
#   OR
#   apt install just   (Ubuntu/Debian)
#
# Usage:
#   just               # Show available commands
#   just dev           # Start development servers
#   just build         # Build all packages
#   just generate-api  # Regenerate OpenAPI spec and API client

# Default recipe - show available commands
default:
    @just --list

# =============================================================================
# Build Commands
# =============================================================================

# Build all packages and apps
build:
    pnpm build

# Build backend only
build-backend:
    cd apps/backend && pnpm build

# Build frontend only
build-frontend:
    cd apps/frontend && pnpm build

# Build shared package only
build-shared:
    cd packages/shared && pnpm build

# Clean all build artifacts
clean:
    pnpm -r clean

# =============================================================================
# Development Commands
# =============================================================================

# Start development servers (backend + frontend)
dev:
    pnpm dev

# Start backend development server
dev-backend:
    cd apps/backend && pnpm dev

# Start frontend development server
dev-frontend:
    cd apps/frontend && pnpm dev

# =============================================================================
# API & Code Generation
# =============================================================================

# Generate OpenAPI spec from backend
openapi-generate:
    cd apps/backend && pnpm run openapi:generate

# Generate frontend API client from OpenAPI spec
api-client-generate:
    cd apps/frontend && pnpm run api:generate

# Generate both OpenAPI spec and API client
generate-api: openapi-generate api-client-generate

# =============================================================================
# Database Commands
# =============================================================================

# Generate Prisma client
db-generate:
    cd apps/backend && pnpm run db:generate

# Run database migrations
db-migrate:
    cd apps/backend && pnpm run db:migrate

# Reset database (destructive!)
db-reset:
    cd apps/backend && npx prisma migrate reset --force

# Open Prisma Studio
db-studio:
    cd apps/backend && npx prisma studio

# Seed database
db-seed:
    cd apps/backend && pnpm run db:seed

# =============================================================================
# Testing Commands
# =============================================================================

# Run all tests
test:
    pnpm test

# Run backend tests
test-backend:
    cd apps/backend && pnpm test

# Run frontend tests
test-frontend:
    cd apps/frontend && pnpm test

# Run tests in watch mode (backend)
test-backend-watch:
    cd apps/backend && pnpm run test:watch

# =============================================================================
# Linting & Code Quality
# =============================================================================

# Run linter on all packages
lint:
    pnpm lint

# Run linter on backend
lint-backend:
    cd apps/backend && pnpm lint

# Run linter on frontend
lint-frontend:
    cd apps/frontend && pnpm lint

# =============================================================================
# Installation & Dependencies
# =============================================================================

# Install all dependencies
install:
    pnpm install

# Update all dependencies
update:
    pnpm update

# =============================================================================
# Docker Commands
# =============================================================================

# Start all Docker services (db, redis, etc.)
docker-up:
    docker-compose up -d

# Stop all Docker services
docker-down:
    docker-compose down

# View Docker logs
docker-logs:
    docker-compose logs -f

# =============================================================================
# Utility Commands
# =============================================================================

# Format code with prettier (if installed)
format:
    npx prettier --write "**/*.{ts,tsx,js,jsx,json,md}"

# Check TypeScript types
typecheck:
    pnpm -r typecheck 2>/dev/null || echo "Type checking..."
    cd apps/backend && npx tsc --noEmit
    cd apps/frontend && npx tsc --noEmit

# Full CI check - build, lint, test
ci: build lint test
    @echo "✅ All CI checks passed!"
