CREATE EXTENSION IF NOT EXISTS pg_trgm;        -- 三元组模糊搜索
CREATE EXTENSION IF NOT EXISTS btree_gin;      -- 复合 GIN 索引
CREATE EXTENSION IF NOT EXISTS pgcrypto;       -- 加密函数

CREATE TABLE users
(
    id                  BIGINT PRIMARY KEY,
    username            VARCHAR(50) UNIQUE NOT NULL,
    password_hash       VARCHAR(255)       NOT NULL,
    phone               VARCHAR(20) UNIQUE,
    email               VARCHAR(100) UNIQUE,
    nickname            VARCHAR(100)       NOT NULL,
    avatar              TEXT,
    bio                 TEXT,
    "current_role"      VARCHAR(20)        NOT NULL DEFAULT 'consumer',
    verified_roles      TEXT[] DEFAULT ARRAY ['consumer']::TEXT[],
    -- 结构: {"player": "pending", "owner": "rejected", "consumer": "approved"}
    verification_status JSONB DEFAULT '{"consumer": "approved"}'::JSONB,
    is_admin            BOOLEAN                     DEFAULT FALSE,
    created_at          TIMESTAMPTZ        NOT NULL DEFAULT NOW(),
    updated_at          TIMESTAMPTZ        NOT NULL DEFAULT NOW(),
    deleted_at          TIMESTAMPTZ,

    CONSTRAINT chk_current_role CHECK ("current_role" IN ('consumer', 'player', 'owner', 'admin'))
);

CREATE INDEX idx_users_phone ON users (phone) WHERE deleted_at IS NULL;
CREATE INDEX idx_users_username_trgm ON users USING gin (username gin_trgm_ops) WHERE deleted_at IS NULL;
