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

import ExpoModulesCore

private let MEMORY_DB_NAME = ":memory:"

private let moduleQueue = DispatchQueue(label: "expo.module.sqlite.AsyncQueue", qos: .userInitiated, attributes: .concurrent)

// `@unchecked Sendable`: the `@JS(.concurrent)` members send the module off the JavaScript thread, which
// Swift 6 mode allows only for a `Sendable` module. The mutable state is either guarded by `cacheLock` or
// only read off the JavaScript thread (`hasListeners`).
@ExpoModule("ExpoSQLite", classes: [NativeStatement.self, NativeSession.self])
public final class SQLiteModule: Module, @unchecked Sendable {
  // Store unmanaged (SQLiteModule, Database) pairs for sqlite callbacks,
  // will release the pair when `closeDatabase` is called.
  private var contextPairs = [Unmanaged<AnyObject>]()

  // Closing a cached connection also releases its update-hook context under this lock.
  private static let cacheLock = NSRecursiveLock()
  private var cachedDatabases = [NativeDatabase]()
  private(set) var hasListeners = false

  @JS
  var defaultDatabaseDirectory: String? {
    #if os(tvOS)
    return appContext?.config.cacheDirectory?.appendingPathComponent("SQLite").standardized.path
    #else
    return appContext?.config.documentDirectory?.appendingPathComponent("SQLite").standardized.path
    #endif
  }

  @JS
  var bundledExtensions: [String: [String: String?]] {
    var bundledExtensions: [String: [String: String?]] = [:]
    #if WITH_SQLITE_VEC
    bundledExtensions["sqlite-vec"] = [
      "libPath": Bundle(identifier: "sqlite-vec")?.path(forResource: "vec", ofType: ""),
      "entryPoint": "sqlite3_vec_init"
    ]
    #endif
    return bundledExtensions
  }

  @Event("onDatabaseChange")
  var onDatabaseChange: (DatabaseChangeEvent) -> Void

  public override func didStartListening(event: String) {
    hasListeners = true
  }

  public override func didStopListening(event: String) {
    hasListeners = false
  }

  public override func willDestroy() {
    removeAllCachedDatabases().forEach {
      do {
        try closeDatabase($0)
      } catch {}
    }
  }

  // MARK: - Module functions

  @JS(.concurrent)
  func deleteDatabaseAsync(databasePath: String) async throws {
    try deleteDatabase(databasePath: databasePath)
  }

  @JS
  func deleteDatabaseSync(databasePath: String) throws {
    try deleteDatabase(databasePath: databasePath)
  }

  @JS(.concurrent)
  func importAssetDatabaseAsync(databasePath: String, assetDatabasePath: String, forceOverwrite: Bool) async throws {
    try importAssetDatabase(databasePath: databasePath, assetDatabasePath: assetDatabasePath, forceOverwrite: forceOverwrite)
  }

  @JS(.concurrent)
  func ensureDatabasePathExistsAsync(databasePath: String) async throws {
    _ = try ensureDatabasePathExists(path: databasePath)
  }

  @JS
  func ensureDatabasePathExistsSync(databasePath: String) throws {
    _ = try ensureDatabasePathExists(path: databasePath)
  }

  @JS(.concurrent)
  func backupDatabaseAsync(
    destDatabase: NativeDatabase,
    destDatabaseName: String,
    sourceDatabase: NativeDatabase,
    sourceDatabaseName: String
  ) async throws {
    try backupDatabase(
      destDatabase: destDatabase,
      destDatabaseName: destDatabaseName,
      sourceDatabase: sourceDatabase,
      sourceDatabaseName: sourceDatabaseName
    )
  }

  @JS
  func backupDatabaseSync(
    destDatabase: NativeDatabase,
    destDatabaseName: String,
    sourceDatabase: NativeDatabase,
    sourceDatabaseName: String
  ) throws {
    try backupDatabase(
      destDatabase: destDatabase,
      destDatabaseName: destDatabaseName,
      sourceDatabase: sourceDatabase,
      sourceDatabaseName: sourceDatabaseName
    )
  }

  public func definition() -> ModuleDefinition {
    // MARK: - NativeDatabase

    // swiftlint:disable:next closure_body_length
    Class(NativeDatabase.self) {
      Constructor { (databasePath: String, options: OpenDatabaseOptions, serializedData: Data?) -> NativeDatabase in
        var db: OpaquePointer?

        if let serializedData = serializedData {
          db = try deserializeDatabase(serializedData)
        } else {
          // Try to find opened database for fast refresh
          if let cachedDb = findCachedDatabase(where: { $0.databasePath == databasePath && $0.openOptions == options && !options.useNewConnection }) {
            cachedDb.addRef()
            return cachedDb
          }

          let path = try ensureDatabasePathExists(path: databasePath)
          if exsqlite3_open(path.toFilePath(), &db) != SQLITE_OK {
            throw DatabaseException()
          }
        }

        let database = NativeDatabase(db, databasePath: databasePath, openOptions: options)
        addCachedDatabase(database)
        return database
      }

      AsyncFunction("initAsync") { (database: NativeDatabase) in
        try initDb(database: database)
      }.runOnQueue(moduleQueue)
      Function("initSync") { (database: NativeDatabase) in
        try initDb(database: database)
      }

      AsyncFunction("closeAsync") { (database: NativeDatabase) in
        try closeDatabaseIfNeeded(database)
      }.runOnQueue(moduleQueue)
      // Interrupt must reach SQLite immediately, without waiting for the running query's queue.
      Function("interruptSync") { (database: NativeDatabase) in
        // Do not block the JS thread or touch a connection being closed on another thread.
        guard database.closeLock.try() else {
          throw DatabaseClosingException()
        }
        defer { database.closeLock.unlock() }
        try maybeThrowForClosedDatabase(database)
        exsqlite3_interrupt(database.pointer)
      }
      Function("closeSync") { (database: NativeDatabase) in
        try closeDatabaseIfNeeded(database)
      }
    }
  }

  private func ensureDatabasePathExists(path: String) throws -> URL {
    if path == MEMORY_DB_NAME {
      guard let url = URL(string: path) else {
        throw DatabaseException()
      }
      return url
    }
    guard let fileSystem = appContext?.fileSystem else {
      throw Exceptions.FileSystemModuleNotFound()
    }

    guard let pathUrl = DatabaseFileUtils.fileURL(fromDatabasePath: path) else {
      throw DatabaseInvalidPathException(path)
    }
    let filePath = pathUrl.toFilePath()
    fileSystem.ensureDirExists(withPath: pathUrl.deletingLastPathComponent().toFilePath())

    // Installs made on iOS 17+ before this resolution opened a percent-encoded file name.
    if let legacyPath = DatabaseFileUtils.legacyEncodedPath(forDatabasePath: path, resolvedPath: filePath) {
      try DatabaseFileUtils.migrateLegacyDatabaseFiles(fromPath: legacyPath, toPath: filePath)
    }

    return pathUrl
  }

  private func deserializeDatabase(_ serializedData: Data) throws -> OpaquePointer? {
    var db: OpaquePointer?
    if exsqlite3_open(MEMORY_DB_NAME, &db) != SQLITE_OK {
      throw DatabaseException()
    }
    let size = sqlite3_int64(serializedData.count)
    guard let buffer = exsqlite3_malloc64(sqlite3_uint64(size)) else {
      throw SQLiteErrorException("Unable to allocate memory for \(size) bytes")
    }
    try serializedData.withUnsafeBytes {
      guard let baseAddress = $0.baseAddress else {
        exsqlite3_free(buffer)
        throw SQLiteErrorException("Unable to get allocated memory base address")
      }
      memcpy(buffer, baseAddress, Int(size))
    }
    let flags = UInt32(SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE)
    let ret = exsqlite3_deserialize(db, "main", buffer.assumingMemoryBound(to: UInt8.self), size, size, flags)
    if ret != SQLITE_OK {
      throw SQLiteErrorException(sqliteErrorMessage(for: db))
    }
    return db
  }

  private func initDb(database: NativeDatabase) throws {
    try maybeThrowForClosedDatabase(database)
    if database.openOptions.enableChangeListener {
      addUpdateHook(database)
    }
  }

  private func convertSqlLiteErrorToString(_ db: NativeDatabase) -> String {
    return db.lastErrorMessage()
  }

  func closeDatabase(_ db: NativeDatabase) throws {
    Self.cacheLock.lock()
    defer { Self.cacheLock.unlock() }
    db.closeLock.lock()
    defer { db.closeLock.unlock() }
    db.statementLifecycleLock.lock()
    defer { db.statementLifecycleLock.unlock() }
    try db.ensureOpen()
    try maybeFinalizeAllStatements(db)

    let ret = exsqlite3_close(db.pointer)
    // SQLITE_BUSY leaves the connection open, including its update hook.
    if ret != SQLITE_OK {
      throw SQLiteErrorException(convertSqlLiteErrorToString(db))
    }
    db.isClosed = true

    Self.cacheLock.withLock {
      if let index = contextPairs.firstIndex(where: {
        guard let pair = $0.takeUnretainedValue() as? (SQLiteModule, NativeDatabase) else {
          return false
        }
        if pair.1.sharedObjectId != db.sharedObjectId {
          return false
        }
        $0.release()
        return true
      }) {
        contextPairs.remove(at: index)
      }
    }
  }

  private func deleteDatabase(databasePath: String) throws {
    if findCachedDatabase(where: { $0.databasePath == databasePath }) != nil {
      throw DeleteDatabaseException(databasePath)
    }

    if databasePath == MEMORY_DB_NAME {
      return
    }
    let path = try ensureDatabasePathExists(path: databasePath).toFilePath()
    try DatabaseFileUtils.deleteDatabaseFiles(atPath: path)
  }

  private func importAssetDatabase(databasePath: String, assetDatabasePath: String, forceOverwrite: Bool) throws {
    let path = try ensureDatabasePathExists(path: databasePath)
    let fileManager = FileManager.default
    if fileManager.fileExists(atPath: path.toFilePath()) && !forceOverwrite {
      return
    }
    guard let assetPath = Utilities.urlFrom(string: assetDatabasePath)?.path,
      fileManager.fileExists(atPath: assetPath) else {
      throw DatabaseNotFoundException(assetDatabasePath)
    }
    DatabaseFileUtils.removeDatabaseFiles(atPath: path.toFilePath())
    try fileManager.copyItem(atPath: assetPath, toPath: path.toFilePath())
  }

  private func backupDatabase(destDatabase: NativeDatabase, destDatabaseName: String, sourceDatabase: NativeDatabase, sourceDatabaseName: String) throws {
    try maybeThrowForClosedDatabase(destDatabase)
    try maybeThrowForClosedDatabase(sourceDatabase)
    guard let backup = exsqlite3_backup_init(destDatabase.pointer, destDatabaseName, sourceDatabase.pointer, sourceDatabaseName) else {
      throw SQLiteErrorException(convertSqlLiteErrorToString(destDatabase))
    }
    exsqlite3_backup_step(backup, -1)
    if exsqlite3_backup_finish(backup) != SQLITE_OK {
      throw SQLiteErrorException(convertSqlLiteErrorToString(destDatabase))
    }
  }

  private func addUpdateHook(_ database: NativeDatabase) {
    let contextPair = Unmanaged.passRetained(((self, database) as AnyObject))
    Self.cacheLock.withLock {
      contextPairs.append(contextPair)
    }
    // swiftlint:disable:next multiline_arguments
    exsqlite3_update_hook(database.pointer, { obj, action, databaseName, tableName, rowId in
      guard let obj,
        let tableName,
        let pair = Unmanaged<AnyObject>.fromOpaque(obj).takeUnretainedValue() as? (SQLiteModule, NativeDatabase) else {
        return
      }
      let selfInstance = pair.0
      let database = pair.1
      let databaseFilePath = exsqlite3_db_filename(database.pointer, databaseName)
      if selfInstance.hasListeners, let databaseName, let databaseFilePath {
        selfInstance.onDatabaseChange(DatabaseChangeEvent(
          databaseName: String(cString: UnsafePointer(databaseName)),
          databaseFilePath: String(cString: UnsafePointer(databaseFilePath)),
          tableName: String(cString: UnsafePointer(tableName)),
          rowId: Double(rowId),
          typeId: SQLAction.fromCode(value: action)
        ))
      }
    },
    contextPair.toOpaque())
  }

  private func maybeThrowForClosedDatabase(_ database: NativeDatabase) throws {
    try database.ensureOpen()
  }

  // MARK: - cachedDatabases managements

  private func addCachedDatabase(_ database: NativeDatabase) {
    Self.cacheLock.withLock {
      cachedDatabases.append(database)
    }
  }

  private func closeDatabaseIfNeeded(_ database: NativeDatabase) throws {
    try Self.cacheLock.withLock {
      try maybeThrowForClosedDatabase(database)
      if let index = cachedDatabases.firstIndex(of: database) {
        let db = cachedDatabases[index]
        if db.release() == 0 {
          do {
            try closeDatabase(db)
          } catch {
            // Keep the connection cached and owned so callers can clean up and retry.
            db.addRef()
            throw error
          }
          cachedDatabases.remove(at: index)
        }
      }
    }
  }

  private func findCachedDatabase(where predicate: (NativeDatabase) -> Bool) -> NativeDatabase? {
    return Self.cacheLock.withLock {
      if let database = cachedDatabases.first(where: predicate) {
        return database
      }
      return nil
    }
  }

  private func removeAllCachedDatabases() -> [NativeDatabase] {
    return Self.cacheLock.withLock {
      let databases = cachedDatabases
      cachedDatabases.removeAll()
      return databases
    }
  }

  // MARK: - statements managements
  private func maybeFinalizeAllStatements(_ database: NativeDatabase) throws {
    guard database.openOptions.finalizeUnusedStatementsBeforeClosing else {
      return
    }
    // Finalize through the wrappers so even a failed close leaves them invalidated.
    // Do not destroy SQLite-internal statements owned by concurrent exec/backup operations.
    for statement in database.statements {
      do {
        try statement.finalize(database: database)
      } catch {
        ExpoModulesCore.log.warn("Finalizing a statement during close failed: \(error)")
      }
    }
  }
}
