#!/usr/bin/env node
const fixPathImportESM = require('node:url').pathToFileURL(__filename)
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));

// src/helpers.ts
var fs = __toESM(require("fs"), 1);
function fileExistsSync(filePath) {
  try {
    fs.accessSync(filePath, fs.constants.F_OK);
    return true;
  } catch (err) {
    return false;
  }
}
var TablesInit = class {
  static async validateTablesSchema(connection) {
    const expectedTables = {
      files: /* @__PURE__ */ new Set([
        "name",
        "original_name",
        "content_type",
        "timestamp",
        "access",
        "uploader_id",
        "meta",
        "sizeBytes"
      ]),
      file_data: /* @__PURE__ */ new Set([
        "file_name",
        "data"
      ])
    };
    for (const [table, expectedColumns] of Object.entries(expectedTables)) {
      const rows = await connection.getAll(`PRAGMA table_info(${table})`);
      const actualColumns = new Set(rows.map((row) => row.name));
      for (const col of expectedColumns) {
        if (!actualColumns.has(col)) {
          throw new Error(`Missing column '${col}' in table '${table}'`);
        }
      }
    }
  }
  static async initTables(connection) {
    await connection.run(`

  CREATE TABLE IF NOT EXISTS files (
  name TEXT PRIMARY KEY CHECK (name <> ''),
  original_name TEXT NOT NULL CHECK (original_name <> ''),
  content_type TEXT NOT NULL CHECK (content_type <> '' AND length(content_type) <= 100),
  timestamp INTEGER NOT NULL,
  access TEXT NOT NULL CHECK (access IN ('public', 'private')),
  uploader_id TEXT,
  meta TEXT,
  sizeBytes INTEGER NOT NULL CHECK (sizeBytes >= 0)
  ) STRICT;
 `);
    await connection.run(`
   
   CREATE TABLE IF NOT EXISTS file_data (
   file_name TEXT PRIMARY KEY,
   data BLOB NOT NULL,
   FOREIGN KEY (file_name) REFERENCES files(name) ON DELETE CASCADE
  ) STRICT; 
   
  `);
  }
};

// bin.ts
var import_rg_sqlite_helper = require("rg-sqlite-helper");
async function checkDB() {
  const dbPath = process.argv[2];
  if (!dbPath)
    throw new Error("no db path at first argument");
  if (!fileExistsSync(dbPath))
    throw new Error(`file not found ${dbPath}`);
  const connection = new import_rg_sqlite_helper.SqlManager(dbPath);
  await TablesInit.initTables(connection);
  await TablesInit.validateTablesSchema(connection);
  console.log(`tables added and validated to db file ${dbPath}`);
  process.exit(0);
}
void checkDB();
