#!/bin/bash
set -e

SYNC_DIR="${SYNC_DIR:-$HOME/.hermes-sync}"
HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}"
HOSTNAME=$(hostname)
cd "$SYNC_DIR"

echo "[$(date '+%H:%M:%S')] Sync from $HOSTNAME..."

# ── Step 0: Clean local state_merged.db to avoid git pull conflicts ──────
# The merged db is produced by this script, not tracked, and can conflict
# with remote when pulling. Remove it before fetch+merge.
rm -f "$SYNC_DIR/state_merged.db"

# ── Step 1: Export local state.db (via temp dir to avoid lock) ────────────
python3 << PYEOF
import sqlite3, os, shutil, tempfile

local_db = os.path.join(os.path.expanduser('~/.hermes'), 'state.db')
export_db = os.path.join(os.path.expanduser('~/.hermes-sync'), f"state_{os.environ.get('HOSTNAME') or __import__('socket').gethostname()}.db")
tmpdir = tempfile.mkdtemp(prefix='hs_exp_')

try:
    tmp_db = os.path.join(tmpdir, 'db')
    shutil.copy2(local_db, tmp_db)

    test = sqlite3.connect(tmp_db)
    r = test.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]
    m = test.execute('SELECT COUNT(*) FROM messages').fetchone()[0]
    test.close()

    shutil.copy2(tmp_db, export_db)
    print(f'Exported: {r}s/{m}m')
finally:
    shutil.rmtree(tmpdir, ignore_errors=True)
PYEOF

# ── Step 2: Git stage ────────────────────────────────────────────────────
git add -A
HAS_LOCAL=false
if ! git diff --cached --quiet || ! git diff --quiet; then
    HAS_LOCAL=true
fi

# ── Step 3: Fetch + merge ─────────────────────────────────────────────────
git fetch origin main

if git rev-parse HEAD >/dev/null 2>&1 && \
   git rev-parse origin/main >/dev/null 2>&1 && \
   ! git merge-base --is-ancestor HEAD origin/main 2>/dev/null; then
    echo "Merging remote..."
    if [ "$HAS_LOCAL" = true ]; then
        git stash push -m "local $(date)" 2>/dev/null || true
        if ! git merge origin/main --no-edit 2>/dev/null; then
            git checkout --ours sync.sh memories/MEMORY.md 2>/dev/null || true
            git add -A
            git commit -m "Auto-resolve $(date)" 2>/dev/null || true
        fi
        if git stash list | grep -q "local "; then
            git stash pop 2>/dev/null || true
            git rebase origin/main 2>/dev/null || {
                git rebase --abort 2>/dev/null || true
                git merge origin/main --no-edit 2>/dev/null || true
            }
        fi
    else
        git merge origin/main --no-edit 2>/dev/null || \
        git merge --ff-only origin/main 2>/dev/null || \
        git reset --hard origin/main
    fi
fi

# ── Step 4: Merge all state_*.db → state_merged.db ──────────────────────
python3 << 'PYEOF'
import sqlite3, os, glob, shutil, tempfile

sync_dir = os.path.expanduser('~/.hermes-sync')
merged_path = os.path.join(sync_dir, 'state_merged.db')
db_files = sorted(glob.glob(os.path.join(sync_dir, 'state_*.db')))
db_files = [f for f in db_files if not f.endswith('_merged.db')]
print(f'Merging {len(db_files)} DBs')

tmpdir = tempfile.mkdtemp(prefix='hs_merge_')
tmp_merged = os.path.join(tmpdir, 'merged.db')

try:
    # Create merged DB with proper schema
    conn = sqlite3.connect(tmp_merged)
    conn.execute('PRAGMA journal_mode=DELETE')
    conn.execute('PRAGMA locking_mode=NORMAL')
    conn.execute('PRAGMA synchronous=FULL')

    conn.execute('''
    CREATE TABLE sessions (
        id TEXT PRIMARY KEY, source TEXT NOT NULL, user_id TEXT,
        model TEXT, model_config TEXT, system_prompt TEXT,
        parent_session_id TEXT, started_at REAL, ended_at REAL,
        end_reason TEXT, message_count INTEGER DEFAULT 0,
        tool_call_count INTEGER DEFAULT 0, input_tokens INTEGER DEFAULT 0,
        output_tokens INTEGER DEFAULT 0, cache_read_tokens INTEGER DEFAULT 0,
        cache_write_tokens INTEGER DEFAULT 0, reasoning_tokens INTEGER DEFAULT 0,
        billing_provider TEXT, billing_base_url TEXT, billing_mode TEXT,
        estimated_cost_usd REAL, actual_cost_usd REAL,
        cost_status TEXT, cost_source TEXT, pricing_version TEXT, title TEXT
    )''')

    conn.execute('''
    CREATE TABLE messages (
        id INTEGER PRIMARY KEY,
        session_id TEXT NOT NULL, role TEXT NOT NULL, content TEXT,
        tool_call_id TEXT, tool_calls TEXT, tool_name TEXT,
        timestamp REAL NOT NULL, token_count INTEGER,
        finish_reason TEXT, reasoning TEXT, reasoning_details TEXT,
        codex_reasoning_items TEXT
    )''')

    conn.execute('CREATE INDEX idx_msg_session ON messages(session_id)')
    conn.execute('CREATE INDEX idx_msg_ts ON messages(timestamp)')

    for db_file in db_files:
        name = os.path.basename(db_file)
        # Copy to temp file first (avoids WAL lock issues with open connections)
        tmp_copy = os.path.join(tmpdir, name)
        shutil.copy2(db_file, tmp_copy)
        # Also copy WAL if exists
        if os.path.exists(db_file + '-wal'):
            shutil.copy2(db_file + '-wal', tmp_copy + '-wal')

        src = sqlite3.connect(tmp_copy)

        s_cnt = src.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]
        m_cnt = src.execute('SELECT COUNT(*) FROM messages').fetchone()[0]
        print(f'  {name}: {s_cnt}s/{m_cnt}m')

        sess_rows = src.execute('SELECT * FROM sessions').fetchall()
        sess_cols = len(src.execute('PRAGMA table_info(sessions)').fetchall())
        for row in sess_rows:
            conn.execute(f'INSERT OR REPLACE INTO sessions VALUES ({",".join(["?"]*sess_cols)})', row)

        msg_rows = src.execute('SELECT * FROM messages').fetchall()
        msg_cols = len(src.execute('PRAGMA table_info(messages)').fetchall())
        for row in msg_rows:
            conn.execute(f'INSERT OR IGNORE INTO messages VALUES ({",".join(["?"]*msg_cols)})', row)

        src.close()
        os.remove(tmp_copy)
        if os.path.exists(tmp_copy + '-wal'):
            os.remove(tmp_copy + '-wal')

    conn.commit()
    conn.close()

    if os.path.exists(merged_path):
        os.remove(merged_path)
    shutil.copy2(tmp_merged, merged_path)
    print(f'Merged: {os.path.getsize(merged_path)/1024:.0f} KB')

finally:
    shutil.rmtree(tmpdir, ignore_errors=True)
PYEOF

# ── Step 5: Stage merged DB (it was just created in Step 4) ─────────────
git add state_merged.db 2>/dev/null || true

# ── Step 6: Push ─────────────────────────────────────────────────────────
if [ "$HAS_LOCAL" = true ]; then
    git commit -m "Sync $(date '+%Y-%m-%d %H:%M')" 2>/dev/null || true
    if ! git push origin main 2>&1; then
        echo "Push rejected, pulling..."
        git pull origin main --no-edit 2>/dev/null || true
        git push origin main 2>&1 || echo "Push failed"
    fi
else
    echo "No local changes"
fi

# ── Step 7: Restore merged state to local hermes ─────────────────────────
python3 << 'PYEOF'
import sqlite3, os, shutil, tempfile

hermes_home = os.path.expanduser('~/.hermes')
merged_path = os.path.join(os.path.expanduser('~/.hermes-sync'), 'state_merged.db')
local_db = os.path.join(hermes_home, 'state.db')

if not os.path.exists(merged_path):
    print('No merged DB, skipping restore')
else:
    tmpdir = tempfile.mkdtemp(prefix='hs_rest_')
    try:
        tmp_db = os.path.join(tmpdir, 'db')
        shutil.copy2(merged_path, tmp_db)

        test = sqlite3.connect(tmp_db)
        r = test.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]
        m = test.execute('SELECT COUNT(*) FROM messages').fetchone()[0]
        test.close()

        shutil.copy2(local_db, local_db + '.bak')
        shutil.copy2(tmp_db, local_db)
        os.remove(local_db + '.bak')
        print(f'Restored: {r}s/{m}m')
    finally:
        shutil.rmtree(tmpdir, ignore_errors=True)
PYEOF

# ── Step 8: Sync memories + skills (additive) ────────────────────────────
cp "$SYNC_DIR/memories/MEMORY.md" "$HERMES_HOME/memories/MEMORY.md" 2>/dev/null || true
if [ -d "$SYNC_DIR/skills" ]; then
    mkdir -p "$HERMES_HOME/skills"
    rsync -a --ignore-existing "$SYNC_DIR/skills/" "$HERMES_HOME/skills/" 2>/dev/null || \
        cp -rn "$SYNC_DIR/skills/"* "$HERMES_HOME/skills/" 2>/dev/null || true
fi

echo "[$(date '+%H:%M:%S')] Done"
