CREATE TABLE IF NOT EXISTS licenses (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  license_key VARCHAR(64) NOT NULL UNIQUE,
  seats_total INTEGER NOT NULL,
  seats_used INTEGER NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  buyer_email VARCHAR(255),
  paypal_order_id VARCHAR(255),
  created_at DATETIME NOT NULL
);

CREATE TABLE IF NOT EXISTS activations (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  license_id INTEGER NOT NULL,
  seat_id INTEGER NOT NULL,
  hwid VARCHAR(191) NOT NULL,
  device_name VARCHAR(255),
  license_file TEXT NOT NULL,
  created_at DATETIME NOT NULL,
  revoked_at DATETIME,
  UNIQUE(license_id, hwid),
  FOREIGN KEY(license_id) REFERENCES licenses(id)
);

CREATE TABLE IF NOT EXISTS orders (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  paypal_order_id VARCHAR(191) NOT NULL UNIQUE,
  license_id INTEGER NOT NULL,
  status VARCHAR(32) NOT NULL,
  seats INTEGER NOT NULL,
  amount VARCHAR(32) NOT NULL,
  currency VARCHAR(8) NOT NULL,
  raw_json TEXT,
  created_at DATETIME NOT NULL,
  FOREIGN KEY(license_id) REFERENCES licenses(id)
);
