// 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 contracts (\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  party_a TEXT,\n  party_b TEXT,\n  amount REAL,\n  signed_at TEXT,\n  expires_at TEXT,\n  status TEXT DEFAULT 'draft',\n  file_url TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS approvals (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT NOT NULL REFERENCES users(id),\n  entity_type TEXT NOT NULL,\n  entity_id TEXT,\n  applicant_id TEXT NOT NULL REFERENCES users(id),\n  status TEXT DEFAULT 'pending',\n  form_data TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS customers (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT NOT NULL REFERENCES users(id),\n  name TEXT NOT NULL,\n  email TEXT,\n  phone TEXT,\n  company TEXT,\n  source TEXT,\n  tags TEXT DEFAULT '[]',\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS reminders (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT NOT NULL REFERENCES users(id),\n  entity_type TEXT NOT NULL,\n  entity_id TEXT,\n  remind_at TEXT NOT NULL,\n  message TEXT,\n  sent INTEGER DEFAULT 'false',\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS boards (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  name TEXT NOT NULL,\n  description TEXT,\n  color TEXT,\n  created_at TEXT,\n  updated_at TEXT\n);",
  "CREATE TABLE IF NOT EXISTS tasks (\n  id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n  user_id TEXT REFERENCES users(id),\n  board_id TEXT REFERENCES boards(id),\n  title TEXT NOT NULL,\n  description TEXT,\n  status TEXT,\n  priority TEXT,\n  due_date TEXT,\n  assignee_id TEXT REFERENCES assignees(id),\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);"
  ];
  for (const sql of statements) {
    const trimmed = sql.trim();
    if (trimmed) db.run(trimmed);
  }
}
