// ============================================================================
// TICKET-PMAT-5021: Hook Integration with Gate Executor
// ============================================================================

/// Generate pre-commit hook with quality gate executor (fast mode)
///
/// # TICKET-PMAT-5021
///
/// # Complexity
/// - Time: O(1)
/// - Cyclomatic: 2
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn generate_pre_commit_hook_with_gates_fast() -> String {
    r#"#!/bin/bash
# Pre-commit hook with quality gate executor (fast mode)
# Generated by PMAT ScaffoldEngine - TICKET-PMAT-5021
#
# Fast mode: Runs clippy + tests only (<30s)
# To bypass: git commit --no-verify
# For full gates: Run 'pmat quality-gates' manually

set -e

echo "Running pre-commit quality gates (fast mode)..."

# Check if any source files are staged (skip for docs-only commits)
STAGED_SRC=$(git diff --cached --name-only --diff-filter=ACMR -- '*.rs' '*.py' '*.ts' '*.tsx' '*.js' '*.go' '*.c' '*.cpp' '*.lua' 2>/dev/null || true)
if [ -z "$STAGED_SRC" ]; then
    echo "⏭️  No source files staged (docs-only commit) — skipping quality gates"
    exit 0
fi

# Clippy (strict mode)
echo "→ Running clippy..."
cargo clippy --all-targets --all-features -- -D warnings

# Tests
echo "→ Running tests..."
cargo test --all-features

echo "✓ All quality gates passed!"
echo "ℹ️  For full coverage/complexity checks, run: pmat quality-gates"
exit 0
"#
    .to_string()
}

/// Generate .pmat-gates.toml configuration file
///
/// # TICKET-PMAT-5021
///
/// # Complexity
/// - Time: O(1)
/// - Cyclomatic: 1
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn generate_gate_config_toml() -> String {
    r#"# PMAT Quality Gate Configuration
# Generated by PMAT ScaffoldEngine

[gates]
run_clippy = true
clippy_strict = true
run_tests = true
test_timeout = 300
check_coverage = true
min_coverage = 80.0
check_complexity = true
max_complexity = 10
"#
    .to_string()
}

/// Install quality gate configuration file
///
/// # TICKET-PMAT-5021
///
/// # Complexity
/// - Time: O(1)
/// - Cyclomatic: 2
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn install_gate_config(project_dir: &Path) -> Result<()> {
    use std::fs;

    let config_path = project_dir.join(".pmat-gates.toml");
    let toml = generate_gate_config_toml();

    fs::write(&config_path, toml).map_err(ScaffoldError::IoError)?;

    Ok(())
}

/// Install all hooks (pre-commit, post-commit) with quality gates
///
/// # TICKET-PMAT-5021
///
/// # Complexity
/// - Time: O(1)
/// - Cyclomatic: 2
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn install_all_hooks_with_gates(project_dir: &Path) -> Result<()> {
    // Install pre-commit hook with gates (fast mode)
    let pre_commit = generate_pre_commit_hook_with_gates_fast();
    install_pre_commit_hook(project_dir, &pre_commit)?;

    // Install post-commit hook (from PMAT-5013)
    install_post_commit_hook(project_dir)?;

    // Install gate configuration
    install_gate_config(project_dir)?;

    Ok(())
}
