when:
  - event: push
    branch: main
  - event: manual
  - event: pull_request

steps:
  - name: fmt
    image: rust:1.94-bookworm
    environment:
      GIT_AUTH_TOKEN:
        from_secret: git_auth_token
      RUSTC_WRAPPER: sccache
      SCCACHE_REDIS: "redis://100.92.54.45:6380"
    commands:
      - apt-get install -y --no-install-recommends sccache >/dev/null 2>&1 || true
      - git config --global url."http://x-access-token:$GIT_AUTH_TOKEN@100.92.54.45:3002/".insteadOf "http://100.92.54.45:3002/"
      - printf '[registries.forgejo]\nindex = "sparse+https://repo.indexarr.net/api/packages/indexarr/cargo/"\ncredential-provider = "cargo:token"\n\n[registry]\ndefault = "forgejo"\n' > $CARGO_HOME/config.toml
      - printf '[registries.forgejo]\ntoken = "Bearer %s"\n' "$GIT_AUTH_TOKEN" > $CARGO_HOME/credentials.toml
      - rustup component add rustfmt
      - cargo fmt --all -- --check

  - name: check
    image: rust:1.94-bookworm
    environment:
      GIT_AUTH_TOKEN:
        from_secret: git_auth_token
      RUSTC_WRAPPER: sccache
      SCCACHE_REDIS: "redis://100.92.54.45:6380"
    commands:
      - apt-get install -y --no-install-recommends sccache >/dev/null 2>&1 || true
      - git config --global url."http://x-access-token:$GIT_AUTH_TOKEN@100.92.54.45:3002/".insteadOf "http://100.92.54.45:3002/"
      - printf '[registries.forgejo]\nindex = "sparse+https://repo.indexarr.net/api/packages/indexarr/cargo/"\ncredential-provider = "cargo:token"\n\n[registry]\ndefault = "forgejo"\n' > $CARGO_HOME/config.toml
      - printf '[registries.forgejo]\ntoken = "Bearer %s"\n' "$GIT_AUTH_TOKEN" > $CARGO_HOME/credentials.toml
      - cargo check --workspace

  - name: test
    image: rust:1.94-bookworm
    environment:
      GIT_AUTH_TOKEN:
        from_secret: git_auth_token
      RUSTC_WRAPPER: sccache
      SCCACHE_REDIS: "redis://100.92.54.45:6380"
    commands:
      - apt-get install -y --no-install-recommends sccache >/dev/null 2>&1 || true
      - git config --global url."http://x-access-token:$GIT_AUTH_TOKEN@100.92.54.45:3002/".insteadOf "http://100.92.54.45:3002/"
      - printf '[registries.forgejo]\nindex = "sparse+https://repo.indexarr.net/api/packages/indexarr/cargo/"\ncredential-provider = "cargo:token"\n\n[registry]\ndefault = "forgejo"\n' > $CARGO_HOME/config.toml
      - printf '[registries.forgejo]\ntoken = "Bearer %s"\n' "$GIT_AUTH_TOKEN" > $CARGO_HOME/credentials.toml
      - cargo test --workspace

  - name: clippy
    image: rust:1.94-bookworm
    environment:
      GIT_AUTH_TOKEN:
        from_secret: git_auth_token
      RUSTC_WRAPPER: sccache
      SCCACHE_REDIS: "redis://100.92.54.45:6380"
    commands:
      - apt-get install -y --no-install-recommends sccache >/dev/null 2>&1 || true
      - git config --global url."http://x-access-token:$GIT_AUTH_TOKEN@100.92.54.45:3002/".insteadOf "http://100.92.54.45:3002/"
      - printf '[registries.forgejo]\nindex = "sparse+https://repo.indexarr.net/api/packages/indexarr/cargo/"\ncredential-provider = "cargo:token"\n\n[registry]\ndefault = "forgejo"\n' > $CARGO_HOME/config.toml
      - printf '[registries.forgejo]\ntoken = "Bearer %s"\n' "$GIT_AUTH_TOKEN" > $CARGO_HOME/credentials.toml
      - rustup component add clippy
      - cargo clippy --workspace -- -D warnings

  - name: publish
    image: rust:1.94-bookworm
    environment:
      GIT_AUTH_TOKEN:
        from_secret: git_auth_token
    commands:
      - printf '[registries.forgejo]\nindex = "sparse+https://repo.indexarr.net/api/packages/indexarr/cargo/"\ncredential-provider = "cargo:token"\n\n[registry]\ndefault = "forgejo"\n' > $CARGO_HOME/config.toml
      - printf '[registries.forgejo]\ntoken = "Bearer %s"\n' "$GIT_AUTH_TOKEN" > $CARGO_HOME/credentials.toml
      - cargo publish --allow-dirty 2>&1 || echo "Publish skipped (version may already exist)"
    when:
      - event: push
        branch: main

  - name: publish-crates-io
    image: rust:1.94-bookworm
    environment:
      CARGO_CRATES_IO_TOKEN:
        from_secret: cargo_crates_io_token
    commands:
      - printf '[registries.crates-io]\ntoken = "%s"\n' "$CARGO_CRATES_IO_TOKEN" > $CARGO_HOME/credentials.toml
      - sed -i 's/, registry = "forgejo"//g; s/registry = "forgejo", //g' Cargo.toml
      - rm -f Cargo.lock
      - cargo publish --registry crates-io --allow-dirty --no-verify 2>&1 || echo "crates.io publish skipped (version may already exist)"
    when:
      - event: push
        branch: main

  # trigger-renovate step removed — woodpecker_token secret not configured,
  # which errored the pipeline at secret-validation time. Re-add when provisioned.

  # push-github step removed — gh_release_token secret missing errors
  # pipeline at validation. Re-add once provisioned.

  - name: discord-success
    image: alpine/curl
    environment:
      DISCORD_WEBHOOK_URL:
        from_secret: discord_webhook_url
    commands:
      - |
        curl -s -X POST "$DISCORD_WEBHOOK_URL" \
          -H "Content-Type: application/json" \
          -d "{\"embeds\":[{\"title\":\"nzb-postproc published\",\"description\":\"Published to Forgejo registry.\",\"color\":3066993}]}"
    when:
      - event: push
        branch: main
        status: [success]

  - name: discord-failure
    image: alpine/curl
    environment:
      DISCORD_WEBHOOK_URL:
        from_secret: discord_webhook_url
    commands:
      - |
        curl -s -X POST "$DISCORD_WEBHOOK_URL" \
          -H "Content-Type: application/json" \
          -d "{\"embeds\":[{\"title\":\"nzb-postproc build failed\",\"description\":\"Check CI logs for details\",\"color\":15158332}]}"
    when:
      - status: [failure]
