-- 套餐与订阅
CREATE TABLE plans (
  id            INTEGER PRIMARY KEY AUTOINCREMENT,
  code          TEXT NOT NULL UNIQUE CHECK (code IN ('free','pro','team')),
  max_devices   INTEGER NOT NULL,
  daily_minutes INTEGER NULL,
  ad_gate       INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE subscriptions (
  id          INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id     INTEGER NOT NULL,
  plan_id     INTEGER NOT NULL,
  expires_at  DATETIME NOT NULL,
  source      TEXT NOT NULL CHECK (source IN ('trial','code')),
  created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id),
  FOREIGN KEY (plan_id) REFERENCES plans(id)
);
CREATE INDEX idx_subs_user_exp ON subscriptions (user_id, expires_at);
