// Copyright 2026-present 650 Industries. All rights reserved.

import ExpoModulesTestCore
import Testing

@testable import ExpoModulesCore
@testable import ExpoSQLite

/// End-to-end benchmarks for JavaScript calling the native module. A JavaScript driver function runs
/// the measured loop against `expo.modules.ExpoSQLite`, so each operation covers the full round trip:
/// the engine's call dispatch, argument decoding, the SQLite work and encoding the result back.
///
/// The drivers issue the same sequence of native calls that `SQLiteDatabase` in `src/` issues for the
/// corresponding public method, so a change to the native binding shows up here the way it shows up
/// for an app. Read every result against `JS: empty driver loop`.
extension Benchmarks {
  /// Evaluates the setup script: opens a seeded in-memory database as the global `db` and exposes the
  /// statement class as `NativeStatement`.
  @JavaScriptActor
  private func prepareDatabase(_ runtime: JavaScriptRuntime) throws {
    _ = try runtime.eval(
      """
      var NativeStatement = expo.modules.ExpoSQLite.NativeStatement;
      var db = new expo.modules.ExpoSQLite.NativeDatabase(':memory:', { useNewConnection: true });
      db.initSync();
      """
    )
    // The schema goes in as a call argument instead of a template literal, so it needs no escaping.
    _ = try runtime.eval("(function (schema) { db.execSync(schema); })")
      .asFunction()
      .call(arguments: BenchmarkSchema.setup())
  }

  /// The native calls behind `SQLiteDatabase.getAllSync(source)`.
  @JavaScriptActor
  private func getAllDriver(_ runtime: JavaScriptRuntime, source: String) throws -> JavaScriptFunction {
    return try javaScriptDriver(
      runtime,
      body: """
        var stmt = new NativeStatement();
        db.prepareSync(stmt, '\(source)');
        stmt.runSync(db, {}, {}, true);
        stmt.getColumnNamesSync();
        stmt.getAllSync(db);
        stmt.finalizeSync(db);
        """
    )
  }

  @Test
  func `JS: empty driver loop`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try javaScriptDriver(runtime, body: "")
      try benchmark("JS: empty driver loop", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: exec a trivial statement`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try javaScriptDriver(runtime, body: "db.execSync('SELECT 1');")
      try benchmark("JS: execSync('SELECT 1')", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: construct a statement`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try javaScriptDriver(runtime, body: "new NativeStatement();")
      try benchmark("JS: new NativeStatement()", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: getAll one row`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try getAllDriver(runtime, source: "SELECT \(BenchmarkSchema.selectColumns) FROM t WHERE id = 1")
      try benchmark("JS: getAllSync 1 row (prepare, run, columns, getAll, finalize)", runtime: runtime) {
        iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: getAll 100 rows`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try getAllDriver(runtime, source: "SELECT \(BenchmarkSchema.selectColumns) FROM t WHERE id <= 100")
      try benchmark("JS: getAllSync 100 rows", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: getAll 10k rows`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try getAllDriver(runtime, source: "SELECT \(BenchmarkSchema.selectColumns) FROM t")
      try benchmark("JS: getAllSync 10k rows", runtime: runtime, samples: 5) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: getAll 100 rows with a 1 KiB blob column`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      let run = try getAllDriver(runtime, source: "SELECT id, data FROM blobs")
      try benchmark("JS: getAllSync 100 rows with 1 KiB blob", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  /// `SQLiteStatement.executeSync(...).getAllSync()` on a statement prepared once.
  @Test
  func `JS: reuse a prepared statement`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      _ = try runtime.eval(
        """
        var prepared = new NativeStatement();
        db.prepareSync(prepared, 'SELECT \(BenchmarkSchema.selectColumns) FROM t WHERE id > ? LIMIT 100');
        """
      )
      let run = try javaScriptDriver(
        runtime,
        body: """
          prepared.runSync(db, { 0: i % 9000 }, {}, true);
          prepared.getColumnNamesSync();
          prepared.getAllSync(db);
          """
      )
      try benchmark("JS: prepared statement reuse, 100 rows", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  /// `SQLiteDatabase.runSync(source, [params])` without the prepare and finalize around it, so the
  /// number isolates parameter binding and the run result.
  @Test
  func `JS: insert with array parameters`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      _ = try runtime.eval(
        """
        var insert = new NativeStatement();
        db.prepareSync(insert, 'INSERT INTO t (int_value, real_value, text_value, null_value) VALUES (?, ?, ?, ?)');
        """
      )
      let run = try javaScriptDriver(
        runtime,
        body: "insert.runSync(db, { 0: i, 1: 1.5, 2: 'row', 3: null }, {}, true);"
      )
      try benchmark("JS: runSync insert with 4 array parameters", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  /// `SQLiteDatabase.runSync(source, { $name: value })`: named parameters resolve their index through
  /// `sqlite3_bind_parameter_index` instead of the array position.
  @Test
  func `JS: insert with named parameters`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      _ = try runtime.eval(
        """
        var insert = new NativeStatement();
        db.prepareSync(insert, 'INSERT INTO t (int_value, real_value, text_value, null_value) VALUES ($i, $r, $t, $n)');
        """
      )
      let run = try javaScriptDriver(
        runtime,
        body: "insert.runSync(db, { $i: i, $r: 1.5, $t: 'row', $n: null }, {}, false);"
      )
      try benchmark("JS: runSync insert with 4 named parameters", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  /// `SQLiteDatabase.runSync(source, [blob])`: the blob goes through the separate blob parameters.
  @Test
  func `JS: insert with a blob parameter`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      _ = try runtime.eval(
        """
        var insert = new NativeStatement();
        db.prepareSync(insert, 'INSERT INTO blobs (data) VALUES (?)');
        var blob = new Uint8Array(1024);
        """
      )
      let run = try javaScriptDriver(runtime, body: "insert.runSync(db, {}, { 0: blob }, true);")
      try benchmark("JS: runSync insert with a 1 KiB blob parameter", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  /// `SQLiteStatement.executeSync(...)` iterated row by row: one native call per row.
  @Test
  func `JS: step through 100 rows`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      _ = try runtime.eval(
        """
        var cursor = new NativeStatement();
        db.prepareSync(cursor, 'SELECT \(BenchmarkSchema.selectColumns) FROM t WHERE id <= 100');
        """
      )
      let run = try javaScriptDriver(
        runtime,
        body: """
          cursor.resetSync(db);
          while (cursor.stepSync(db) != null) {}
          """
      )
      try benchmark("JS: stepSync through 100 rows", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }

  @Test
  func `JS: column names of a prepared statement`() async throws {
    try await sqliteBenchmarkCase { runtime in
      try prepareDatabase(runtime)
      _ = try runtime.eval(
        """
        var columns = new NativeStatement();
        db.prepareSync(columns, 'SELECT \(BenchmarkSchema.selectColumns) FROM t');
        """
      )
      let run = try javaScriptDriver(runtime, body: "columns.getColumnNamesSync();")
      try benchmark("JS: getColumnNamesSync (5 columns)", runtime: runtime) { iterations in
        _ = try run.call(arguments: iterations)
      }
    }
  }
}
