#!/bin/bash

# Parse connection details from ZXDB_URL in .env
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ENV_FILE="$SCRIPT_DIR/../.env"

if [ ! -f "$ENV_FILE" ]; then
  echo "Error: .env file not found at $ENV_FILE" >&2
  exit 1
fi

ZXDB_URL=$(grep '^ZXDB_URL=' "$ENV_FILE" | cut -d= -f2-)
if [ -z "$ZXDB_URL" ]; then
  echo "Error: ZXDB_URL not set in .env" >&2
  exit 1
fi

# Unescape backslash-escaped characters (e.g. \$ -> $)
ZXDB_URL=$(echo "$ZXDB_URL" | sed 's/\\\(.\)/\1/g')

# Extract user, password, host, port, database from mysql://user:pass@host:port/db
DB_USER=$(echo "$ZXDB_URL" | sed -n 's|^mysql://\([^:]*\):.*|\1|p')
DB_PASS=$(echo "$ZXDB_URL" | sed -n 's|^mysql://[^:]*:\([^@]*\)@.*|\1|p')
DB_HOST=$(echo "$ZXDB_URL" | sed -n 's|^mysql://[^@]*@\([^:]*\):.*|\1|p')
DB_PORT=$(echo "$ZXDB_URL" | sed -n 's|^mysql://[^@]*@[^:]*:\([0-9]*\)/.*|\1|p')
DB_NAME=$(echo "$ZXDB_URL" | sed -n 's|^mysql://[^/]*/\(.*\)|\1|p')

MYSQL_ARGS="-u${DB_USER} -p${DB_PASS} -h${DB_HOST} -P${DB_PORT}"

echo "DROP DATABASE IF EXISTS \`${DB_NAME}\`; CREATE DATABASE \`${DB_NAME}\`;" | mysql $MYSQL_ARGS
mysql $MYSQL_ARGS < ZXDB/ZXDB_mysql.sql
{
  echo "SET @OLD_SQL_MODE := @@SESSION.sql_mode;"
  echo "SET SESSION sql_mode := REPLACE(@@SESSION.sql_mode, 'ONLY_FULL_GROUP_BY', '');"
  cat ZXDB/scripts/ZXDB_help_search.sql
  echo "SET SESSION sql_mode := @OLD_SQL_MODE;"
#  echo "CREATE ROLE IF NOT EXISTS 'zxdb_readonly';"
#  echo "GRANT SELECT, SHOW VIEW ON \`zxdb\`.* TO 'zxdb_readonly';"
} | mysql --force $MYSQL_ARGS "$DB_NAME"
# ---- Reimport software_hashes from JSON snapshot if available ----
HASHES_SNAPSHOT="$SCRIPT_DIR/../data/zxdb/software_hashes.json"
if [ -f "$HASHES_SNAPSHOT" ]; then
  echo "Reimporting software_hashes from $HASHES_SNAPSHOT ..."
  node -e "
    const fs = require('fs');
    const mysql = require('mysql2/promise');
    (async () => {
      const snap = JSON.parse(fs.readFileSync('$HASHES_SNAPSHOT', 'utf8'));
      if (!snap.rows || snap.rows.length === 0) {
        console.log('  No rows in snapshot, skipping.');
        return;
      }
      const pool = mysql.createPool({ uri: '$ZXDB_URL', connectionLimit: 1 });
      await pool.query(\`
        CREATE TABLE IF NOT EXISTS software_hashes (
          download_id INT NOT NULL PRIMARY KEY,
          md5 VARCHAR(32) NOT NULL,
          crc32 VARCHAR(8) NOT NULL,
          size_bytes BIGINT NOT NULL,
          inner_path VARCHAR(500) NOT NULL,
          updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
          INDEX idx_sh_md5 (md5),
          INDEX idx_sh_crc32 (crc32)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
      \`);
      await pool.query('TRUNCATE TABLE software_hashes');
      // Batch insert in chunks of 500
      const chunk = 500;
      for (let i = 0; i < snap.rows.length; i += chunk) {
        const batch = snap.rows.slice(i, i + chunk);
        const values = batch.map(r => [r.download_id, r.md5, r.crc32, r.size_bytes, r.inner_path, r.updated_at]);
        await pool.query(
          'INSERT INTO software_hashes (download_id, md5, crc32, size_bytes, inner_path, updated_at) VALUES ?',
          [values]
        );
      }
      console.log('  Imported ' + snap.rows.length + ' rows into software_hashes.');
      await pool.end();
    })().catch(e => { console.error('  Error reimporting software_hashes:', e.message); process.exit(0); });
  "
else
  echo "No software_hashes snapshot found at $HASHES_SNAPSHOT — skipping reimport."
fi

mysqldump --no-data -uroot -p -h${DB_HOST} -P${DB_PORT} "$DB_NAME" > ZXDB/ZXDB_mysql_STRUCTURE_ONLY.sql
