# justfile for Euchre Camp
# Automates development, testing, and deployment tasks.
# Designed for CI/CD pipelines and local development.

# --- Configuration ---
set dotenv-load
set positional-arguments

# Project name
PROJECT := "euchre-camp"

# Docker registry (CasaOS local registry)
REGISTRY := "registry"  # Update to your registry host if needed
IMAGE_TAG := "latest"

# Get git commit hash (short version)
GIT_COMMIT := `git rev-parse --short HEAD`

# Image tag with git commit
IMAGE_TAG_COMMIT := "{{GIT_COMMIT}}"

# Full image name with registry
REGISTRY_IMAGE := "{{REGISTRY}}/{{PROJECT}}"

# --- Variables ---
# Database
DB_CONTAINER := "euchre-camp-postgres"

# --- Setup & Installation ---

# Install dependencies and setup environment
setup:
    @echo "Installing dependencies..."
    npm install
    @echo "Setting up database..."
    npm run db:setup-postgres
    @echo "Generating Prisma client..."
    npx prisma generate

# --- Development ---

# Start the development server
dev:
    npm run dev

# Lint the codebase
lint:
    npm run lint

# Type check the codebase
typecheck:
    npx tsc --noEmit

# Format code (if prettier is available, otherwise just lint)
format:
    npx prettier --write .

# --- Testing ---

# Run all tests (unit + acceptance)
test: test-unit test-acceptance

# Run unit tests (Vitest)
test-unit:
    npm run test:run

# Run acceptance tests (Playwright)
test-acceptance:
    @echo "Starting Docker containers for acceptance tests..."
    docker compose up -d
    @echo "Waiting for services to be ready..."
    sleep 10
    @echo "Running acceptance tests..."
    npm run test:acceptance
    @echo "Stopping Docker containers..."
    docker compose down

# Run database migrations (Prisma)
migrate:
    npx prisma migrate dev

# Seed the database
seed:
    npm run db:seed

# --- Docker ---

# Build the Docker image (standard build)
docker-build:
    @echo "Building Docker image {{PROJECT}}:{{IMAGE_TAG}}..."
    docker build -t {{PROJECT}}:{{IMAGE_TAG}} .

# Build the Docker image with git commit tag
docker-build-commit:
    @echo "Building Docker image {{PROJECT}}:{{IMAGE_TAG_COMMIT}}..."
    docker build -t {{PROJECT}}:{{IMAGE_TAG_COMMIT}} .

# Build and tag with both latest and commit hash
docker-build-full: docker-build docker-build-commit

# Fast rebuild using Docker BuildKit cache
# Uses build cache to speed up rebuilds when only code changes
docker-rebuild-fast:
    @echo "Fast rebuilding Docker image {{PROJECT}}:{{IMAGE_TAG}}..."
    DOCKER_BUILDKIT=1 docker build \
        --cache-from={{PROJECT}}:{{IMAGE_TAG}} \
        -t {{PROJECT}}:{{IMAGE_TAG}} \
        .

# Run the Docker container locally
docker-run: docker-build
    @echo "Running Docker container {{PROJECT}}..."
    docker run -d -p 3000:3000 --name {{PROJECT}} {{PROJECT}}:{{IMAGE_TAG}}

# Stop and remove the Docker container
docker-stop:
    @echo "Stopping Docker container {{PROJECT}}..."
    docker stop {{PROJECT}} || true
    docker rm {{PROJECT}} || true

# Clean up Docker images and containers
docker-clean: docker-stop
    @echo "Removing Docker image {{PROJECT}}:{{IMAGE_TAG}}..."
    docker rmi {{PROJECT}}:{{IMAGE_TAG}} || true

# --- Deployment ---

# Tag image for registry
docker-tag:
    @echo "Tagging image for registry..."
    docker tag {{PROJECT}}:{{IMAGE_TAG}} {{REGISTRY_IMAGE}}:{{IMAGE_TAG}}
    docker tag {{PROJECT}}:{{IMAGE_TAG_COMMIT}} {{REGISTRY_IMAGE}}:{{IMAGE_TAG_COMMIT}}

# Push image to registry
docker-push: docker-build-full docker-tag
    @echo "Pushing image to {{REGISTRY}}..."
    docker push {{REGISTRY_IMAGE}}:{{IMAGE_TAG}}
    docker push {{REGISTRY_IMAGE}}:{{IMAGE_TAG_COMMIT}}

# --- CI/CD Pipeline Simulation ---

# Run full CI pipeline locally (lint, test, build, push)
ci: lint typecheck test-unit docker-build
    @echo "CI Pipeline completed successfully!"

# --- Utilities ---

# Show help information
help:
    @just --list

# Clean up project (remove node_modules, build artifacts)
clean:
    @echo "Cleaning project..."
    rm -rf node_modules .next dist
    @echo "Cleaning Docker artifacts..."
    docker system prune -f
