//! App-wide constants.
//!
//! # Bounded Context: Identity & Configuration Names
//!
//! Single source of truth for the app name, config filenames, env var
//! names, version strings, and URLs. Every other module imports from
//! here — a rename only requires changing this file.

// ── Version & build metadata ────────────────────────────────────────

/// Semantic version from Cargo.toml (patched from the git tag in CI).
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Full version string including dev metadata.
///
/// For release builds (HEAD tagged `v{VERSION}`) this equals [`VERSION`].
/// For development builds it appends `-dev+{GIT_SHA}` so that telemetry
/// and diagnostics can distinguish local builds from released ones.
pub const FULL_VERSION: &str = env!("FULL_VERSION");

/// Short git commit hash at build time, or `"unknown"`.
pub const GIT_SHA: &str = env!("GIT_SHA");

/// Build date (UTC, `YYYY-MM-DD`), or `"unknown"`.
pub const BUILD_DATE: &str = env!("BUILD_DATE");

/// Compilation target triple (e.g. `x86_64-unknown-linux-gnu`).
pub const TARGET: &str = env!("TARGET");

/// User-Agent header for HTTP requests.
pub const USER_AGENT: &str = concat!("nitpik/", env!("CARGO_PKG_VERSION"));

// ── App identity ────────────────────────────────────────────────────

/// Display name of the tool (lowercase).
pub const APP_NAME: &str = "nitpik";

/// Local config filename (e.g. `.nitpik.toml` in repo root).
pub const CONFIG_FILENAME: &str = ".nitpik.toml";

/// Directory name under `~/.config/` for global config and cache.
pub const CONFIG_DIR: &str = "nitpik";

/// AI-generated content disclosure notice.
pub const AI_DISCLOSURE: &str =
    "Generated by nitpik — AI-powered analysis. Findings are advisory and may contain errors.";

/// Telemetry heartbeat endpoint.
pub const TELEMETRY_URL: &str = "https://nitpik.dev/v1/heartbeat";

// ── GitHub releases (self-update) ───────────────────────────────────

/// GitHub owner/repo for release downloads.
pub const GITHUB_REPO: &str = "nsrosenqvist/nitpik";

/// GitHub API endpoint for the latest release metadata.
pub const GITHUB_RELEASES_LATEST_API: &str =
    "https://api.github.com/repos/nsrosenqvist/nitpik/releases/latest";

/// Build a download URL for a release asset.
pub fn release_asset_url(tag: &str, target: &str) -> String {
    format!(
        "https://github.com/{}/releases/download/{}/nitpik-{}.tar.gz",
        GITHUB_REPO, tag, target
    )
}

/// Build a download URL for the SHA256SUMS file of a release.
pub fn release_checksums_url(tag: &str) -> String {
    format!(
        "https://github.com/{}/releases/download/{}/SHA256SUMS",
        GITHUB_REPO, tag
    )
}

// ── Review context docs ─────────────────────────────────────────────

/// Dedicated review context files that take precedence over generic project docs.
///
/// When any of these exist in the repo root (and are not excluded), only these
/// are included — the generic doc list (`AGENTS.md`, `CONVENTIONS.md`, etc.) is
/// skipped entirely. This lets maintainers provide focused review guidance
/// without polluting the prompt with coding-agent instructions.
pub const PRIORITY_DOC_FILES: &[&str] = &["REVIEW.md", "NITPIK.md"];

// ── Environment variable names ──────────────────────────────────────

pub const ENV_PROVIDER: &str = "NITPIK_PROVIDER";
pub const ENV_MODEL: &str = "NITPIK_MODEL";
pub const ENV_API_KEY: &str = "NITPIK_API_KEY";
pub const ENV_BASE_URL: &str = "NITPIK_BASE_URL";
pub const ENV_LICENSE_KEY: &str = "NITPIK_LICENSE_KEY";
pub const ENV_TELEMETRY: &str = "NITPIK_TELEMETRY";
pub const ENV_DEBUG: &str = "NITPIK_DEBUG";

/// Environment variables that custom command subprocesses inherit by default.
///
/// Only these system/shell variables are passed to spawned commands.
/// Everything else — including API keys, tokens, and credentials — is
/// stripped unless the profile explicitly lists it in the `environment`
/// frontmatter field.
pub const SAFE_ENV_VARS: &[&str] = &[
    "HOME",
    "HOSTNAME",
    "LANG",
    "LOGNAME",
    "PATH",
    "PWD",
    "SHELL",
    "SHLVL",
    "TERM",
    "TMPDIR",
    "TEMP",
    "TMP",
    "USER",
    "COLORTERM",
    "TERM_PROGRAM",
];

/// Environment variable prefixes that are safe to inherit.
///
/// Any variable whose name starts with one of these prefixes is included
/// in the subprocess environment alongside [`SAFE_ENV_VARS`].
pub const SAFE_ENV_PREFIXES: &[&str] = &[
    "LC_",  // locale categories (LC_ALL, LC_CTYPE, …)
    "XDG_", // XDG base directory spec
];

// ── Retry constants ─────────────────────────────────────────────────

/// Maximum number of retry attempts for transient API errors.
pub const MAX_RETRIES: u32 = 5;

/// Initial backoff delay between retries.
pub const INITIAL_BACKOFF: std::time::Duration = std::time::Duration::from_secs(10);

/// Maximum backoff delay between retries.
pub const MAX_BACKOFF: std::time::Duration = std::time::Duration::from_secs(60);

// ── HTTP constants ──────────────────────────────────────────────────

/// Default total request timeout for outgoing HTTP calls.
pub const HTTP_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

/// Default TCP connect timeout for outgoing HTTP calls.
pub const HTTP_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);

// ── Resource limits ─────────────────────────────────────────────────

/// Maximum lines per chunk when splitting large diffs (context window guard).
pub const DEFAULT_CHUNK_LINES: usize = 500;

/// Maximum file size (bytes) for project documentation files.
pub const MAX_DOC_SIZE: u64 = 256 * 1024;

/// Context lines around hunks when excerpting large files.
pub const LARGE_FILE_CONTEXT_LINES: usize = 50;

/// Maximum compiled regex DFA size for gitleaks rules (50 MB).
pub const REGEX_SIZE_LIMIT: usize = 50 * 1024 * 1024;

/// Agent name for threat scanner findings.
pub const THREAT_SCANNER_AGENT: &str = "threat-scanner";
