set shell := ["bash", "-cu"]

database_url := "postgres://xtablo:xtablo@localhost:5432/xtablo?sslmode=disable"
compose_config_dir := ".podman-compose"

default:
  @just --list

compose-config:
  mkdir -p {{compose_config_dir}}
  printf '%s\n' '{"auths":{}}' > {{compose_config_dir}}/config.json

machine-up:
  @if command -v podman >/dev/null 2>&1; then \
    if ! podman machine inspect podman-machine-default 2>/dev/null | grep -q '"State": "running"'; then \
      podman machine start podman-machine-default; \
    fi; \
  else \
    echo "podman is required" >&2; \
    exit 1; \
  fi

db-up: machine-up compose-config
  DOCKER_CONFIG="$PWD/{{compose_config_dir}}" podman compose up -d postgres

db-down:
  @if command -v podman >/dev/null 2>&1; then \
    DOCKER_CONFIG="$PWD/{{compose_config_dir}}" podman compose down; \
  else \
    echo "podman is required" >&2; \
    exit 1; \
  fi

db-reset: compose-config
  just machine-up
  DOCKER_CONFIG="$PWD/{{compose_config_dir}}" podman compose down -v
  DOCKER_CONFIG="$PWD/{{compose_config_dir}}" podman compose up -d postgres

db-logs: machine-up compose-config
  DOCKER_CONFIG="$PWD/{{compose_config_dir}}" podman compose logs -f postgres

generate:
  go run github.com/a-h/templ/cmd/templ@latest generate
  go run github.com/sqlc-dev/sqlc/cmd/sqlc@v1.31.1 generate

fmt:
  gofmt -w .

test:
  go test ./...

build:
  go build ./...

check: generate test build

dev: db-up
  DATABASE_URL='{{database_url}}' air -c .air.toml

run: db-up
  DATABASE_URL='{{database_url}}' go run .
