#!/usr/bin/env python3
"""Build Hash Hunter compatible SQLite database for Burkina Faso OSINT"""
import sqlite3
import os
from datetime import datetime

db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "burkina-faso-hashes.db")

# Remove old DB if exists
if os.path.exists(db_path):
    os.remove(db_path)

conn = sqlite3.connect(db_path)
c = conn.cursor()

# Create Hash Hunter compatible schema
c.execute('''CREATE TABLE domains (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    domain TEXT UNIQUE NOT NULL,
    first_seen DATETIME,
    last_scanned DATETIME,
    times_scanned INTEGER DEFAULT 0,
    user_count INTEGER DEFAULT 0,
    status TEXT,
    wp_version TEXT,
    api_enabled INTEGER DEFAULT 1,
    api_total INTEGER DEFAULT 0,
    hash_count INTEGER DEFAULT 0
)''')

c.execute('''CREATE TABLE hashes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    domain TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    username TEXT,
    display_name TEXT,
    hash TEXT NOT NULL,
    hash_type TEXT DEFAULT 'md5',
    avatar_url TEXT,
    profile_url TEXT,
    first_seen DATETIME,
    last_seen DATETIME,
    source TEXT DEFAULT 'api',
    active INTEGER DEFAULT 1,
    UNIQUE(domain, user_id, hash)
)''')

c.execute('''CREATE TABLE hash_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    domain TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    hash TEXT NOT NULL,
    event_type TEXT NOT NULL,
    event_time DATETIME
)''')

c.execute('''CREATE TABLE scan_sessions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    target_file TEXT,
    total_targets INTEGER DEFAULT 0,
    completed_targets INTEGER DEFAULT 0,
    started_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    status TEXT DEFAULT 'completed'
)''')

# Create indexes
c.execute('CREATE INDEX idx_hashes_domain ON hashes(domain)')
c.execute('CREATE INDEX idx_hashes_hash ON hashes(hash)')
c.execute('CREATE INDEX idx_hashes_username ON hashes(username)')

now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')

# === ALL COLLECTED DATA ===
data = {
    'arcep.bf': [
        (7, 'atraore', 'atraore', 'fafee8299b6f09f5db44ffda68c55ceb', 'md5'),
        (10, 'lucien-manzabaarcep-bf', 'Lucien Manzaba', '81f2a93a084a6c9bea12edaa03597a3d', 'md5'),
        (8, 'stella-ouedraogoarcep-bf', 'Stella Ouedraogo', '1029beaeed571f5ab1279d5e994b9a7a', 'md5'),
        (4, 'webmaster', 'webmanager', 'b449ee4708b1bc146c4f935e9c272096', 'md5'),
        (9, 'y-koussoube', 'Yacouba KOUSSOUBE', '17df11a12cbef982a8a22b269d7bb8e5', 'md5'),
    ],
    'sig.bf': [
        (18, 'dwdouedraogo', 'Adams OUEDRAOGO', '7f6d1c8c6bbacc6b83ad037f32e57051', 'md5'),
        (12, 'dwtoure', 'Aly TOURE', 'bf55105041346aa45caeeb74e6569948', 'md5'),
        (14, 'dwbeli', "Beli N'DO", '1ac5deb5001928d400535900e81d8f2a', 'md5'),
        (1, 'admin', 'Sig-Burkina', 'd7a536c2a29e117bd047ba55db21068f', 'md5'),
        (19, 'wendkuni', 'Wendkuni Eric Demouemba', '0b128e26b43d1c4fe686763048ff7daa', 'md5'),
    ],
    'sonapost.bf': [
        (3, 'atraore', 'atraore', 'aee928b13a5c5a219018448aed49b14c27e59454af0f48a75dd85f8e5945036f', 'sha256'),
        (5, 'doatchade', 'doatchade', '13aa6ce79dfa8eacbafe80a7b0424d24d58da9d4a8020173f1d3b51f7d2d9f41', 'sha256'),
        (2, 'fsankara', 'fsankara', 'e5fdd86d56860617dfeae011c79b7c6c251857e6d6531716ec5a07f3b63d3f06', 'sha256'),
        (7, 'madina', 'Madina', '603add125f0ca142884b9a76f106a63da6bf1adb53bada79ec0af6cb27c814a0', 'sha256'),
        (4, 'webmaster', 'Webmaster', '81a6474ee8d6762009bbe55921d044b6d7df987744b8f211afe0e9b7f403688f', 'sha256'),
    ],
    'diasporaburkina.bf': [
        (2, 'ad_zep', 'ad_zep', 'eff82de57a85be4381645a477984c860', 'md5'),
        (1, 'diasp_ad', 'diasp_ad', 'c5f5b2cc5c4093ceddfbf1eea0b6d72e', 'md5'),
        (3, 'studyuser_2343246756', 'studyuser_2343246756', '34c1ec8b126e30bbffca8afd93affbcf', 'md5'),
        (4, 'studyuser_4260180281', 'studyuser_4260180281', '35f88d8fcdc71f415d4cb790eac77634', 'md5'),
    ],
    'anptic.gov.bf': [
        (2, 'dcrp', 'Aicha Ilboudo', 'd4b91045da4209dc9c434dcda01cc3f068540ac29e1091f112d95c383a9680dd', 'sha256'),
        (3, 'axelle', 'Axelle OUEDRAOGO', '90bc5c1b3b0086f0c1940e243d0c1f38ebe4a9fbd3a470323801210d19a7758d', 'sha256'),
        (1, 'webmaster', 'webmaster', 'f200c49f2bcd7f7e5a4d5d98633f01237034a3b8dde73e689bae1c3e740c5e96', 'sha256'),
    ],
    'carfo.bf': [
        (2, 'arnaud', 'Arnaud', '4e3a089ba0216277d6496bd2600093b0ac075091e2af8c7c02e0f9d5cc233193', 'sha256'),
        (1, 'carfo_wordpress_admin', 'carfo_wordpress_admin', '76313d2c7a00be3ab3127eca5bd74990deed22d8556c3041492e49f9246f2bda', 'sha256'),
        (3, 'kader', 'ILBOUDO KADER', '56a0fd5769905248e2f3f8259c6741c05de5adda8e26a24b8e8abe4cbfcf8c63', 'sha256'),
        (9, 'kdaouda', 'kdaouda', 'db92cff8787ce3b5c3056747f41b003627ab09e6d3c6d059e357d2e021409182', 'sha256'),
        (5, 'badno', 'Noel BADO', '454a7663bf9ee05e38a2e41e23eb8927dcb956b69ddd32adcccb88043ef72ac9', 'sha256'),
        (6, 'ksalome', 'Salome KABORE', 'e79626dd0cd5527eee32c2691db47066c3587506e8c34738a4ef1e5f4dfe3aaa', 'sha256'),
        (4, 'walid', 'Walid OUEDRAOGO', '8151901e3b97da051b012bf9aa1a93bf329bb37dcbef67518f2efe1dac2f57e2', 'sha256'),
    ],
    'onea.bf': [
        (1, 'admin', 'Departement Communication', '90203162d9dc93e1afdf13e21e41cf422d248b031eb4e4ad83a73d2dded5b77b', 'sha256'),
        (4, 'nebisma', 'nebisma', '27e43ddb78e55c186160c6e660ee5f4fbf0fa38a6b6f4859d29387d005d695d2', 'sha256'),
        (5, 'rachid', 'Rachid', '986af49cf16dbc507aaa9f305ac121d5adeb4e2e666f92d134a44bf0fd723cc9', 'sha256'),
    ],
}

# Sites probed but no WP API
no_wp = ['lonab.bf', 'bumigeb.bf', 'fasonet.bf', 'sif.bf', 'econcours.bf', 'sofitex.bf', 'cnss.bf']
blocked = ['presidencedufaso.bf']

# Insert domains and hashes
total_hashes = 0
for domain, users in data.items():
    c.execute('''INSERT INTO domains (domain, first_seen, last_scanned, times_scanned, user_count, status, api_enabled, api_total, hash_count)
                 VALUES (?, ?, ?, 1, ?, 'found', 1, ?, ?)''',
              (domain, now, now, len(users), len(users), len(users)))

    for uid, username, display_name, hash_val, hash_type in users:
        avatar_url = f"https://secure.gravatar.com/avatar/{hash_val}"
        profile_url = f"https://{domain}/author/{username}/"
        c.execute('''INSERT INTO hashes (domain, user_id, username, display_name, hash, hash_type, avatar_url, profile_url, first_seen, last_seen, source)
                     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'api')''',
                  (domain, uid, username, display_name, hash_val, hash_type, avatar_url, profile_url, now, now))

        c.execute('''INSERT INTO hash_history (domain, user_id, hash, event_type, event_time)
                     VALUES (?, ?, ?, 'discovered', ?)''', (domain, uid, hash_val, now))
        total_hashes += 1

# Insert blocked/no-wp domains
for d in no_wp:
    c.execute('''INSERT INTO domains (domain, first_seen, last_scanned, times_scanned, user_count, status, api_enabled, api_total, hash_count)
                 VALUES (?, ?, ?, 1, 0, 'no_hashes', 0, 0, 0)''', (d, now, now))
for d in blocked:
    c.execute('''INSERT INTO domains (domain, first_seen, last_scanned, times_scanned, user_count, status, api_enabled, api_total, hash_count)
                 VALUES (?, ?, ?, 1, 0, 'error', 0, 0, 0)''', (d, now, now))

# Insert scan session
c.execute('''INSERT INTO scan_sessions (target_file, total_targets, completed_targets, started_at, updated_at, status)
             VALUES ('burkina-faso-websites.txt', ?, ?, ?, ?, 'completed')''',
          (len(data) + len(no_wp) + len(blocked), len(data) + len(no_wp) + len(blocked), now, now))

conn.commit()

# === PRINT STATS ===
print("=" * 65)
print("  BURKINA FASO HASH DATABASE — HASH HUNTER FORMAT")
print("=" * 65)
print(f"  Path: {db_path}")
print(f"  Created: {now}")
print()

c.execute("SELECT COUNT(*) FROM domains WHERE status='found'")
found = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM domains WHERE status='no_hashes'")
no_api = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM domains WHERE status='error'")
err = c.fetchone()[0]
print(f"  Sites scanned:           {found + no_api + err}")
print(f"  WordPress with hashes:   {found}")
print(f"  No WP API:               {no_api}")
print(f"  Blocked/Auth required:   {err}")
print()

c.execute("SELECT COUNT(*) FROM hashes")
total = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM hashes WHERE hash_type='md5'")
md5_count = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM hashes WHERE hash_type='sha256'")
sha_count = c.fetchone()[0]
c.execute("SELECT COUNT(DISTINCT hash) FROM hashes")
unique = c.fetchone()[0]

print(f"  Total hash entries:      {total}")
print(f"    MD5 hashes:            {md5_count}")
print(f"    SHA256 hashes:         {sha_count}")
print(f"  Unique hashes:           {unique}")
print()

print("-" * 65)
print("  DOMAIN BREAKDOWN")
print("-" * 65)
c.execute("SELECT domain, hash_count, status FROM domains ORDER BY hash_count DESC")
for row in c.fetchall():
    bar = "#" * row[1]
    status = f" [{row[2]}]" if row[2] != 'found' else ''
    print(f"  {row[0]:30s} {row[1]:2d} {bar}{status}")
print()

print("-" * 65)
print("  ALL HASHES")
print("-" * 65)
c.execute("SELECT domain, user_id, username, display_name, hash, hash_type FROM hashes ORDER BY domain, user_id")
prev_domain = ''
for row in c.fetchall():
    if row[0] != prev_domain:
        print(f"\n  [{row[0].upper()}]")
        prev_domain = row[0]
    ht = row[5].upper()
    print(f"    ID {row[1]:3d} | {row[2]:30s} | {row[3]:30s} | {ht:6s} | {row[4]}")

print()
print("-" * 65)
print("  EMAIL PATTERN HINTS (for Hash Cracker)")
print("-" * 65)
print("  arcep.bf:          firstname.lastname@arcep.bf (from slug pattern)")
print("  sig.bf:            dw prefix = 'Direction Web' — try first.last@sig.bf")
print("  sonapost.bf:       flast pattern (fsankara = F. Sankara)")
print("  carfo.bf:          first initial + last (kader, kdaouda, ksalome, badno)")
print("  anptic.gov.bf:     role-based (dcrp, axelle, webmaster)")
print("  onea.bf:           role-based (admin, nebisma, rachid)")
print("  diasporaburkina.bf: service accounts (diasp_ad, ad_zep, studyuser)")
print()

conn.close()
print(f"  Database size: {os.path.getsize(db_path):,} bytes")
print("=" * 65)
