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

database_url := "postgres://xtablo:xtablo@localhost:5432/xtablo?sslmode=disable"
compose_config_dir := ".podman-compose"
tailwind_input := "tailwind.input.css"
tailwind_output := "static/tailwind.css"

default:
  @just --list

build-styles:
  go run ./cmd/buildstyles

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:
  just build-styles
  pnpm exec tailwindcss -i {{tailwind_input}} -o {{tailwind_output}} --cwd .
  go run github.com/a-h/templ/cmd/templ@v0.3.1020 generate
  go run github.com/sqlc-dev/sqlc/cmd/sqlc@v1.31.1 generate

design-system:
  just generate
  go run ./cmd/designsystem

css-watch:
  pnpm exec tailwindcss -i {{tailwind_input}} -o {{tailwind_output}} --cwd . --watch

fmt:
  gofmt -w .

test:
  just build-styles
  go test ./...

build:
  just build-styles
  pnpm exec tailwindcss -i {{tailwind_input}} -o {{tailwind_output}} --cwd .
  go build ./...

check: generate test build

dev: db-up
  just build-styles
  pnpm exec tailwindcss -i {{tailwind_input}} -o {{tailwind_output}} --cwd .
  DATABASE_URL='{{database_url}}' air -c .air.toml

run: db-up
  just build-styles
  pnpm exec tailwindcss -i {{tailwind_input}} -o {{tailwind_output}} --cwd .
  DATABASE_URL='{{database_url}}' go run .
