import Foundation
import SQLite3

/// Read-only SQLite inspector for diagnosing SwiftData store state.
/// Does NOT modify the file — just reads `sqlite_master` and a couple of counts.
enum StoreInspector {
    static func dump(at url: URL, label: String) {
        let path = url.path
        guard FileManager.default.fileExists(atPath: path) else {
            print("[StoreInspector:\(label)] file does not exist at \(path)")
            return
        }

        var db: OpaquePointer?
        defer { if let db = db { sqlite3_close(db) } }

        let openResult = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, nil)
        guard openResult == SQLITE_OK else {
            print("[StoreInspector:\(label)] open failed code=\(openResult)")
            return
        }

        // List tables
        let tables = queryStrings(db: db, sql: "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
        let hasZVERB = tables.contains("ZVERB")
        let hasZVERBFORM = tables.contains("ZVERBFORM")
        let hasZTENSEGUIDE = tables.contains("ZTENSEGUIDE")
        let hasZVOCABCARD = tables.contains("ZVOCABCARD")
        let hasZTEXTBOOKCHAPTER = tables.contains("ZTEXTBOOKCHAPTER")

        var summary = "[StoreInspector:\(label)] \(tables.count) tables"
        summary += " | ZVERB=\(hasZVERB ? queryInt(db: db, sql: "SELECT COUNT(*) FROM ZVERB") : -1)"
        summary += " ZVERBFORM=\(hasZVERBFORM ? queryInt(db: db, sql: "SELECT COUNT(*) FROM ZVERBFORM") : -1)"
        summary += " ZTENSEGUIDE=\(hasZTENSEGUIDE ? queryInt(db: db, sql: "SELECT COUNT(*) FROM ZTENSEGUIDE") : -1)"
        summary += " ZVOCABCARD=\(hasZVOCABCARD ? queryInt(db: db, sql: "SELECT COUNT(*) FROM ZVOCABCARD") : -1)"
        summary += " ZTEXTBOOKCHAPTER=\(hasZTEXTBOOKCHAPTER ? queryInt(db: db, sql: "SELECT COUNT(*) FROM ZTEXTBOOKCHAPTER") : -1)"
        print(summary)

        // Log all Z-tables (SwiftData entity tables start with Z, minus Core Data system tables)
        let zTables = tables.filter { $0.hasPrefix("Z") && !$0.hasPrefix("Z_") }
        if !zTables.isEmpty {
            print("[StoreInspector:\(label)] entity tables: \(zTables.joined(separator: ", "))")
        }
    }

    private static func queryStrings(db: OpaquePointer?, sql: String) -> [String] {
        var stmt: OpaquePointer?
        defer { if let stmt = stmt { sqlite3_finalize(stmt) } }
        guard sqlite3_prepare_v2(db, sql, -1, &stmt, nil) == SQLITE_OK else { return [] }
        var results: [String] = []
        while sqlite3_step(stmt) == SQLITE_ROW {
            if let cstr = sqlite3_column_text(stmt, 0) {
                results.append(String(cString: cstr))
            }
        }
        return results
    }

    private static func queryInt(db: OpaquePointer?, sql: String) -> Int {
        var stmt: OpaquePointer?
        defer { if let stmt = stmt { sqlite3_finalize(stmt) } }
        guard sqlite3_prepare_v2(db, sql, -1, &stmt, nil) == SQLITE_OK else { return -1 }
        guard sqlite3_step(stmt) == SQLITE_ROW else { return -1 }
        return Int(sqlite3_column_int(stmt, 0))
    }
}
