mod client
mod server
mod shared

set quiet := true

pod_name := "pod_sigo"

# database

pg_image := "docker.io/postgres:18-alpine"
pg_env := "-e POSTGRES_USER -e POSTGRES_PASSWORD -e POSTGRES_DB"
pg_volume := "-v ./sql/create:/docker-entrypoint-initdb.d"
pg_health_cmd := "--health-cmd 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'"
pg_health_conf := "--health-interval 10s --health-retries 3 --health-timeout 5s"
pg_health_start_period := "--health-start-period 30s"

# server

run_server_locally := env("RUN_SERVER_LOCALLY", "false")
server_port := if run_server_locally == "false" { "-p 8000:8000" } else { "" }
server_health_cmd := "--health-cmd 'wget -q --spider http://127.0.0.1:8000/api/health || exit 1'"
server_health_conf := "--health-interval 30s --health-retries 3 --health-timeout 10s"
server_health_start_period := "--health-start-period 10s"

# List available recipes
_default:
    just --list 

# 󰌢  Build Lustre runtime and start HTTP server
[group("dev")]
dev:
    just client::build
    just server::run

#   Update project dependencies
[group("dev")]
deps-update:
    just shared::deps-update
    just client::deps-update
    just server::deps-update

#   Check for errors
[group("dev")]
lint:
    just server::lint
    just client::lint
    just shared::lint

# 󰙨  Run unit tests
[group("dev")]
test:
    just server::test

# 󰏖  Create pod
[group("podman")]
_create-pod:
    podman pod exists {{ pod_name }} || \
    podman pod create \
      --name {{ pod_name }} \
      {{ server_port }} \
      -p 5432:5432

# 󰏖  Build the server container image
[group("podman")]
build-server:
    podman build -t sigo-server .

#   Starts the PostgreSQL database inside the pod
[group("podman")]
_init-database: _create-pod
    podman run -d \
      --pod {{ pod_name }} \
      --name db \
      {{ pg_env }} \
      {{ pg_health_cmd }} {{ pg_health_conf }} {{ pg_health_start_period }} \
      {{ pg_volume }} \
      {{ pg_image }}

#   Starts the server
[group("podman")]
_init-server: _create-pod build-server
    podman run -d \
      --pod {{ pod_name }} \
      --name server \
      -e DATABASE_URL -e SECRET_KEY \
      {{ server_health_cmd }} \
      {{ server_health_conf }} \
      {{ server_health_start_period }} \
      sigo-server

#   Starts the server and database
[group("podman")]
up: _init-database _init-server
    echo {{ GREEN }}"Listening on http://0.0.0.0:8000"{{ NORMAL }}

#   Starts only the database
[group("podman")]
db-up: _init-database
    echo {{ GREEN }}"  PostgreSQL is ready on port 5432"{{ NORMAL }}

#   Stop and remove the entire pod
[group("podman")]
down:
    podman pod exists {{ pod_name }} || (echo "Pod does not exist" && exit 1)
    podman pod stop {{ pod_name }}
    podman pod rm {{ pod_name }}
