version: '3'

dotenv: [".env", ".env.example"]

vars:
  VERSION: '{{.v | default "latest"}}'
  RUST_LOG: "warn,siren=info"

tasks:
  default:
    desc: "List available tasks"
    cmds:
      - task --list
    silent: true

  setup:
    desc: "Copy .env.example to .env if .env does not exist"
    cmds:
      - test -f .env || cp .env.example .env
    silent: true

  format:
    desc: "Format code"
    cmds:
      - task: format:app
      - task: format:ui
    silent: true

  lint:
    desc: "Run linters"
    cmds:
      - task: lint:app
      - task: lint:ui
    silent: true

  # -----------------------------------------------------------
  # Cargo
  # -----------------------------------------------------------
  build:
    desc: "Compile a debug build"
    deps: [ setup ]
    cmds:
      - cargo build
    silent: true

  release:
    desc: "Compile an optimised release build"
    deps: [ setup ]
    cmds:
      - cargo build --release
    silent: true

  run:
    desc: "Run the project"
    deps: [ setup ]
    env:
      RUST_LOG: "warn,siren=trace"
    cmds:
      - cargo run
    silent: true

  format:app:
    desc: "Format code"
    cmds:
      - cargo fmt
    silent: true

  clean:
    desc: "Clean the project"
    deps: [ setup ]
    cmds:
      - cargo clean
    silent: true

  lint:app:
    desc: "Run Clippy linter"
    deps: [ setup ]
    cmds:
      - cargo clippy -- -D warnings
    silent: true

  check:
    desc: "Fast type-check without producing a binary"
    deps: [ setup ]
    cmds:
      - cargo check
    silent: true

  # -----------------------------------------------------------
  # Docker
  # -----------------------------------------------------------
  docker:build:
    desc: "Build the Rust app Docker image (use v=x.x.x to set version, default is \"latest\")"
    cmds:
      - docker build -f docker/Dockerfile -t siren:{{.VERSION}} .
    silent: true

  docker:build:ui:
    desc: "Build the UI Docker image (use v=x.x.x to set version, default is \"latest\")"
    cmds:
      - docker build -f docker/Dockerfile.ui -t siren-ui:{{.VERSION}} .
    silent: true

  docker:up:
    desc: "Start backend containers (postgres + valkey)"
    cmds:
      - docker compose -f docker/docker-compose.yml up -d
    silent: true

  docker:up:all:
    desc: "Start all containers (app + ui + postgres + valkey)"
    cmds:
      - docker compose -f docker/docker-compose.yml --profile app --profile ui up -d
    silent: true

  docker:down:
    desc: "Stop all containers"
    cmds:
      - docker compose -f docker/docker-compose.yml --profile app --profile ui down
    silent: true

  docker:clean:
    desc: "Stop all containers and remove volumes"
    prompt: "This will remove all docker containers, networks, volumes, and images. Are you sure?"
    cmds:
      - docker compose -f docker/docker-compose.yml --profile app --profile ui down -v
    silent: true

  docker:refresh:
    desc: "Clean and restart containers"
    cmds:
      - task: docker:clean
      - task: docker:up
    silent: true

  # -----------------------------------------------------------
  # UI
  # -----------------------------------------------------------
  install:ui:
    desc: "Install UI npm dependencies"
    dir: ui
    cmds:
      - npm install
    silent: true

  run:ui:
    desc: "Run Vite dev server"
    dir: ui
    cmds:
      - npm run dev
    silent: true

  build:ui:
    desc: "Build the React UI into ui/dist"
    dir: ui
    cmds:
      - npm run build
    silent: true

  format:ui:
    desc: "Format UI code with prettier"
    dir: ui
    cmds:
      - npm run format
    silent: true

  lint:ui:
    desc: "Lint UI code with eslint"
    dir: ui
    cmds:
      - npm run lint
    silent: true

  # -----------------------------------------------------------
  # Utilities
  # -----------------------------------------------------------
  psql:
    desc: "Connect to the docker database"
    vars:
      POSTGRES_USER: '{{.POSTGRES_USER | default "postgres"}}'
      POSTGRES_DB: '{{.POSTGRES_DB | default "siren"}}'
      deps: [ docker:up ]
    cmds:
      - docker exec -it siren-postgres psql -U {{.POSTGRES_USER}} -d {{.POSTGRES_DB}} -P pager=off
    silent: true

  psql:admin:
    desc: "Grant admin role to a user"
    cmds:
      - docker exec -it siren-postgres psql -U {{.POSTGRES_USER}} -d {{.POSTGRES_DB}} -c "UPDATE users SET role = 'admin' WHERE username = '{{.user}}'"
    requires:
      vars: [ user ]
    silent: true

  ngrok:
    desc: "Start ngrok tunnel"
    vars:
      UI_PORT: '{{.UI_PORT | default "5173"}}'
    cmds:
      - ngrok http {{.UI_PORT}}
    silent: true
