PRAGMA foreign_keys=ON;

CREATE TABLE
  users (
    user_id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_name TEXT NOT NULL UNIQUE,
    user_source TEXT NOT NULL, -- web | api | telegram | whatsapp | system
    user_api_key TEXT UNIQUE,
    user_api_secret TEXT,
    user_status INTEGER NOT NULL DEFAULT 1, -- 1 = active | 0 = disabled
    user_flag TEXT, -- free text: admin, system, bot, etc
    user_created_at INTEGER NOT NULL -- timestamp ms
  );

CREATE TABLE
  lead (
    lead_id INTEGER PRIMARY KEY AUTOINCREMENT,
    lead_source INTEGER,
    lead_name TEXT NOT NULL,
    lead_phone TEXT,
    lead_email TEXT NOT NULL,
    lead_company TEXT NOT NULL,
    lead_sector TEXT NOT NULL,
    lead_employees INTEGER NOT NULL,
    lead_revenue TEXT NOT NULL,
    lead_description TEXT,
    lead_created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (lead_source) REFERENCES users (user_id) ON DELETE SET NULL
  );