// 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  phone TEXT UNIQUE,\n  nickname TEXT,\n  avatar_url TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "idx_users_phone ON users(phone)",
  "CREATE TABLE IF NOT EXISTS products (\n  id TEXT PRIMARY KEY,\n  title TEXT NOT NULL,\n  price REAL NOT NULL,\n  stock INTEGER DEFAULT 0,\n  images TEXT,\n  category_id TEXT REFERENCES categories(id),\n  created_at TEXT\n);",
  "idx_products_category ON products(category_id)",
  "CREATE TABLE IF NOT EXISTS orders (\n  id TEXT PRIMARY KEY,\n  user_id TEXT REFERENCES users(id),\n  status TEXT DEFAULT 'pending',\n  total_amount REAL,\n  address_id TEXT REFERENCES addresses(id),\n  created_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS order_items (\n  id TEXT PRIMARY KEY,\n  order_id TEXT REFERENCES orders(id),\n  product_id TEXT REFERENCES products(id),\n  quantity INTEGER NOT NULL,\n  unit_price REAL\n);",
  "CREATE TABLE IF NOT EXISTS logistics (\n  id TEXT PRIMARY KEY,\n  order_id TEXT UNIQUE REFERENCES orders(id),\n  carrier TEXT,\n  tracking_number TEXT,\n  status TEXT,\n  events TEXT DEFAULT '[]',\n  updated_at TEXT\n);"
  ];
  for (const sql of statements) {
    const trimmed = sql.trim();
    if (trimmed) db.run(trimmed);
  }
}
