-- 切换到目标数据库
\c postgres;

CREATE OR REPLACE FUNCTION update_user_modified_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql VOLATILE;

DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'user') THEN
        CREATE TABLE "user" (  -- user是关键字，用双引号包裹
            id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
            deleted BOOLEAN NOT NULL DEFAULT FALSE,
            created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
        );
        CREATE TRIGGER update_user_updated_at
        BEFORE UPDATE ON "user"
        FOR EACH ROW
        EXECUTE FUNCTION update_user_modified_column();
        
        RAISE NOTICE 'created user table and trigger';
    ELSE
        RAISE NOTICE 'user table already exists';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'account') THEN
        CREATE TABLE account (
            id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
            user_id UUID NOT NULL,
            account VARCHAR NOT NULL,
            deleted BOOLEAN NOT NULL DEFAULT FALSE,
            created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
        );
        CREATE TRIGGER update_account_updated_at
        BEFORE UPDATE ON "account"
        FOR EACH ROW
        EXECUTE FUNCTION update_account_modified_column();
        
        RAISE NOTICE 'created account table and trigger';
    ELSE
        RAISE NOTICE 'account table already exists';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'password') THEN
        CREATE TABLE password (
            id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
            user_id UUID NOT NULL,
            password VARCHAR NOT NULL,
            deleted BOOLEAN NOT NULL DEFAULT FALSE,
            created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
        );
        CREATE TRIGGER update_password_updated_at
        BEFORE UPDATE ON "password"
        FOR EACH ROW
        EXECUTE FUNCTION update_password_modified_column();
        
        RAISE NOTICE 'created password table and trigger';
    ELSE
        RAISE NOTICE 'password table already exists';
    END IF;
END $$;

DO $$
DECLARE
    view_exists BOOLEAN;
BEGIN
    -- 检查视图是否已存在
    SELECT EXISTS (
        SELECT 1 FROM information_schema.views 
        WHERE table_name = 'user_info_view'
    ) INTO view_exists;

    -- 创建或更新视图
    CREATE OR REPLACE VIEW user_info_view AS
    SELECT 
        u.id AS user_id,
        ua.account AS account,
        up.password AS password,
        u.deleted AS deleted
    FROM 
        "user" u
    JOIN 
        account ua ON u.id = ua.user_id
    JOIN 
        password up ON u.id = up.user_id
    WHERE
        u.deleted = FALSE;

    -- 根据视图是否已存在输出不同提示
    IF view_exists THEN
        RAISE NOTICE '视图 user_info_view 已更新';
    ELSE
        RAISE NOTICE '视图 user_info_view 已创建';
    END IF;
EXCEPTION
    WHEN OTHERS THEN
        RAISE NOTICE '处理视图时发生错误: %', SQLERRM;
END $$;
