#!/usr/bin/env bash
# Bug Bounty Hunting — Dependency Install Script
# Run: chmod +x setup.sh && ./setup.sh

set -euo pipefail

echo "=== Bug Bounty Tool Setup ==="

# --- Homebrew check ---
if ! command -v brew &>/dev/null; then
  echo "ERROR: Homebrew not found. Install from https://brew.sh"
  exit 1
fi

# --- Python/pip check ---
if ! command -v python3 &>/dev/null; then
  echo "Installing python3..."
  brew install python3
fi

# --- Semgrep ---
if ! command -v semgrep &>/dev/null; then
  echo "Installing Semgrep..."
  pip3 install semgrep
else
  echo "Semgrep already installed: $(semgrep --version)"
fi

# --- TruffleHog ---
if ! command -v trufflehog &>/dev/null; then
  echo "Installing TruffleHog..."
  brew install trufflehog
else
  echo "TruffleHog already installed: $(trufflehog --version 2>&1 | head -1)"
fi

# --- Nuclei ---
if ! command -v nuclei &>/dev/null; then
  echo "Installing Nuclei..."
  brew install nuclei
else
  echo "Nuclei already installed: $(nuclei --version 2>&1 | head -1)"
fi

# --- jq ---
if ! command -v jq &>/dev/null; then
  echo "Installing jq..."
  brew install jq
else
  echo "jq already installed: $(jq --version)"
fi

# --- SQLite (should be pre-installed on macOS, but check) ---
if ! command -v sqlite3 &>/dev/null; then
  echo "Installing sqlite3..."
  brew install sqlite
else
  echo "sqlite3 already installed: $(sqlite3 --version | head -1)"
fi

# --- Initialize findings database ---
DB_PATH="$HOME/Code/bug-bounty/data/findings.db"
if [ ! -f "$DB_PATH" ]; then
  echo "Creating findings database..."
  sqlite3 "$DB_PATH" <<'SQL'
CREATE TABLE IF NOT EXISTS findings (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now')),

  -- Target info
  program TEXT NOT NULL,
  platform TEXT NOT NULL,
  repo_url TEXT NOT NULL,
  repo_name TEXT NOT NULL,

  -- Finding details
  title TEXT NOT NULL,
  vuln_type TEXT NOT NULL,
  cwe_id TEXT,
  severity TEXT CHECK(severity IN ('critical','high','medium','low','info')),
  confidence TEXT CHECK(confidence IN ('high','medium','low','false_positive')),
  file_path TEXT,
  line_number INTEGER,
  tool TEXT,
  rule_id TEXT,

  -- Analysis
  description TEXT,
  attack_vector TEXT,
  impact TEXT,

  -- Status tracking
  status TEXT DEFAULT 'triaged' CHECK(status IN (
    'raw',           -- just found by scanner
    'triaged',       -- assessed by scanner agent
    'investigating', -- investigator agent is deep-diving
    'draft',         -- report drafted
    'submitted',     -- report submitted to platform
    'accepted',      -- bounty program accepted
    'rejected',      -- bounty program rejected
    'duplicate',     -- duplicate of existing report
    'paid'           -- bounty received
  )),
  report_path TEXT,
  submission_url TEXT,
  payout_amount REAL,
  payout_currency TEXT DEFAULT 'USD',

  -- Metadata
  scan_briefing_path TEXT,
  commit_hash TEXT,
  notes TEXT
);

CREATE INDEX idx_findings_status ON findings(status);
CREATE INDEX idx_findings_program ON findings(program);
CREATE INDEX idx_findings_severity ON findings(severity);
CREATE INDEX idx_findings_confidence ON findings(confidence);

CREATE TABLE IF NOT EXISTS scan_log (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  scanned_at TEXT DEFAULT (datetime('now')),
  repo_url TEXT NOT NULL,
  repo_name TEXT NOT NULL,
  program TEXT NOT NULL,
  tool TEXT NOT NULL,
  total_raw_findings INTEGER DEFAULT 0,
  real_findings INTEGER DEFAULT 0,
  false_positives INTEGER DEFAULT 0,
  briefing_path TEXT,
  duration_seconds INTEGER,
  notes TEXT
);

CREATE TABLE IF NOT EXISTS spending_log (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  logged_at TEXT DEFAULT (datetime('now')),
  session_id TEXT,
  description TEXT,
  estimated_cost REAL,
  currency TEXT DEFAULT 'USD'
);
SQL
  echo "Database created at $DB_PATH"
else
  echo "Findings database already exists at $DB_PATH"
fi

# --- Directory structure verify ---
for dir in briefings/archive reports data/repos .claude/agents; do
  mkdir -p "$HOME/Code/bug-bounty/$dir"
done

echo ""
echo "=== Setup Complete ==="
echo "Tools: semgrep, trufflehog, nuclei, jq, sqlite3"
echo "Database: $DB_PATH"
echo ""
echo "Next steps:"
echo "  1. Create accounts on huntr.com and hackerone.com"
echo "  2. Review config.yaml and adjust settings"
echo "  3. Run the orchestrator: cd ~/Code/bug-bounty && claude"
