// 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 products (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT NOT NULL REFERENCES users(id),\n  title TEXT NOT NULL,\n  description TEXT,\n  price REAL,\n  stock INTEGER DEFAULT '0',\n  images TEXT DEFAULT '[]',\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS orders (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT NOT NULL REFERENCES users(id),\n  status TEXT DEFAULT 'pending',\n  total_amount REAL,\n  address_id TEXT REFERENCES addresses(id),\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 suppliers (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  name TEXT NOT NULL,\n  contact TEXT,\n  phone TEXT,\n  email TEXT,\n  address 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);"
  ];
  for (const sql of statements) {
    const trimmed = sql.trim();
    if (trimmed) db.run(trimmed);
  }
}
