-- 用户状态
CREATE TYPE user_status AS ENUM ('normal', 'inactivate', 'banned', 'deleted');
COMMENT ON TYPE user_status IS '用户状态枚举类型，包括正常、未激活、封禁和删除';

-- 账户类型
CREATE TYPE account_type AS ENUM ('wechat', 'dingtalk');
COMMENT ON TYPE account_type IS '账户类型枚举类型，包括微信和钉钉';

-- 用户基础表
CREATE TABLE user_base (
  id SERIAL PRIMARY KEY,
  username TEXT NOT NULL UNIQUE,
  email TEXT UNIQUE,
  phone TEXT UNIQUE,
  avatar TEXT,
  bio TEXT,
  nickname TEXT,
  password_hash TEXT NOT NULL,
  status user_status NOT NULL DEFAULT 'normal',
  created_at BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
  updated_at BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW())
);

COMMENT ON COLUMN user_base.id IS '用户ID';
COMMENT ON COLUMN user_base.username IS '用户姓名';
COMMENT ON COLUMN user_base.email IS '用户邮箱';
COMMENT ON COLUMN user_base.phone IS '用户手机号';
COMMENT ON COLUMN user_base.avatar IS '用户头像';
COMMENT ON COLUMN user_base.bio IS '用户简介';
COMMENT ON COLUMN user_base.nickname IS '用户昵称';
COMMENT ON COLUMN user_base.password_hash IS '用户密码';
COMMENT ON COLUMN user_base.status IS '用户状态';
COMMENT ON COLUMN user_base.created_at IS '创建时间';
COMMENT ON COLUMN user_base.updated_at IS '更新时间';

-- 用户账户表
CREATE TABLE user_account (
  user_id INTEGER NOT NULL,
  type account_type NOT NULL,
  value TEXT NOT NULL,
  created_at BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
  updated_at BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
  UNIQUE (user_id, type)
);

COMMENT ON COLUMN user_account.user_id IS '用户ID';
COMMENT ON COLUMN user_account.type IS '账户类型';
COMMENT ON COLUMN user_account.value IS '设置值';
COMMENT ON COLUMN user_account.created_at IS '创建时间';
COMMENT ON COLUMN user_account.updated_at IS '更新时间';

-- 用户设置表
CREATE TABLE user_setting (
  user_id INTEGER NOT NULL,
  key TEXT NOT NULL,
  value TEXT NOT NULL,
  created_at BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
  updated_at BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
  UNIQUE (user_id, key)
);

COMMENT ON COLUMN user_setting.user_id IS '用户ID';
COMMENT ON COLUMN user_setting.key IS '设置项';
COMMENT ON COLUMN user_setting.value IS '设置值';
COMMENT ON COLUMN user_setting.created_at IS '创建时间';
COMMENT ON COLUMN user_setting.updated_at IS '更新时间';