// Auto-generated SQLite schema
import type { Database } from "sql.js";

export function createTables(db: Database): void {
  const statements = [
  "CREATE TABLE IF NOT EXISTS users (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  username TEXT NOT NULL UNIQUE,\n  password_hash TEXT NOT NULL,\n  nickname TEXT,\n  role TEXT DEFAULT 'user',\n  phone TEXT,\n  avatar_url TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS items (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  title TEXT NOT NULL,\n  description TEXT,\n  status TEXT,\n  data TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS stock_in (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  product_name TEXT NOT NULL,\n  quantity INTEGER NOT NULL,\n  supplier TEXT,\n  received_by TEXT,\n  received_at TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS stock_out (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  product_name TEXT NOT NULL,\n  quantity INTEGER NOT NULL,\n  recipient TEXT,\n  approved_by TEXT,\n  shipped_at TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS stocktaking (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  warehouse TEXT,\n  product_name TEXT,\n  expected_qty INTEGER,\n  actual_qty INTEGER,\n  diff INTEGER,\n  stocktaken_at TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS stock_alerts (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  product_name TEXT NOT NULL,\n  current_qty INTEGER,\n  threshold INTEGER,\n  alerted_at TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS warehouse (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  product_name TEXT NOT NULL,\n  quantity INTEGER NOT NULL,\n  supplier TEXT,\n  received_by TEXT,\n  received_at TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);"
  ];
  for (const sql of statements) {
    const trimmed = sql.trim();
    if (trimmed) db.run(trimmed);
  }
}
