-- 002_admin_login_logs.sql —— 后台登录记录。

CREATE TABLE IF NOT EXISTS admin_login_logs (
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT,
  username TEXT NOT NULL DEFAULT '',
  role TEXT NOT NULL DEFAULT '',
  ip TEXT NOT NULL DEFAULT '',
  location TEXT NOT NULL DEFAULT '',
  user_agent TEXT NOT NULL DEFAULT '',
  success BOOLEAN NOT NULL DEFAULT TRUE,
  failure_reason TEXT NOT NULL DEFAULT '',
  created_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_admin_login_logs_created_at
  ON admin_login_logs(created_at DESC);

CREATE INDEX IF NOT EXISTS idx_admin_login_logs_username
  ON admin_login_logs(username);

CREATE INDEX IF NOT EXISTS idx_admin_login_logs_user_id
  ON admin_login_logs(user_id);

COMMENT ON TABLE admin_login_logs IS '后台账号登录记录';
COMMENT ON COLUMN admin_login_logs.location IS '登录地点（可选，依赖 IP 推断或代理头）';
COMMENT ON COLUMN admin_login_logs.user_agent IS '客户端设备 / User-Agent';
