-- 用户主表
CREATE TABLE IF NOT EXISTS user_main (
    id UUID PRIMARY KEY,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    createdate TIMESTAMP WITH TIME ZONE NOT NULL,
    modifydate TIMESTAMP WITH TIME ZONE NOT NULL
);

-- 用户登录账号表
CREATE TABLE IF NOT EXISTS user_login_account (
    id UUID PRIMARY KEY,
    user_id UUID NOT NULL,
    account VARCHAR(100) NOT NULL,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    createdate TIMESTAMP WITH TIME ZONE NOT NULL,
    modifydate TIMESTAMP WITH TIME ZONE NOT NULL,
    CONSTRAINT fk_user_login_account_user_main FOREIGN KEY (user_id) REFERENCES user_main(id)
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_user_login_account_active 
    ON user_login_account(account) 
    WHERE deleted = FALSE;

-- 用户密码表
CREATE TABLE IF NOT EXISTS user_login_password (
    id UUID PRIMARY KEY,
    user_id UUID NOT NULL,
    password VARCHAR(255) NOT NULL,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    createdate TIMESTAMP WITH TIME ZONE NOT NULL,
    modifydate TIMESTAMP WITH TIME ZONE NOT NULL,
    CONSTRAINT fk_user_login_password_user_main FOREIGN KEY (user_id) REFERENCES user_main(id)
);

CREATE INDEX IF NOT EXISTS idx_user_login_password_user_id 
    ON user_login_password(user_id);

-- 用户登录邮箱表
CREATE TABLE IF NOT EXISTS user_login_email (
    id UUID PRIMARY KEY,
    user_id UUID NOT NULL,
    email VARCHAR(255) NOT NULL,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    createdate TIMESTAMP WITH TIME ZONE NOT NULL,
    modifydate TIMESTAMP WITH TIME ZONE NOT NULL,
    CONSTRAINT fk_user_login_email_user_main FOREIGN KEY (user_id) REFERENCES user_main(id)
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_user_login_email_active 
    ON user_login_email(email) 
    WHERE deleted = FALSE;
