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

DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange') THEN
        CREATE TABLE exchange ( 
            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_exchange_updated_at
        BEFORE UPDATE ON "exchange"
        FOR EACH ROW
        EXECUTE FUNCTION update_data_modified_column();
        
        RAISE NOTICE 'created exchange table and trigger';
    ELSE
        RAISE NOTICE 'exchange table already exists';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange_name') THEN
        CREATE TABLE exchange_name (
            id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
            exchange_id UUID NOT NULL,
            name 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_name_updated_at
        BEFORE UPDATE ON "exchange_name"
        FOR EACH ROW
        EXECUTE FUNCTION update_data_modified_column();
        
        RAISE NOTICE 'created exchange_name table and trigger';
    ELSE
        RAISE NOTICE 'exchange_name table already exists';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange_short_name') THEN
        CREATE TABLE exchange_short_name (
            id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
            exchange_id UUID NOT NULL,
            short_name 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_short_name_updated_at
        BEFORE UPDATE ON "exchange_short_name"
        FOR EACH ROW
        EXECUTE FUNCTION update_data_modified_column();
        
        RAISE NOTICE 'created exchange_short_name table and trigger';
    ELSE
        RAISE NOTICE 'exchange_short_name table already exists';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange_code') THEN
        CREATE TABLE exchange_code (
            id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
            exchange_id UUID NOT NULL,
            code 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_code_updated_at
        BEFORE UPDATE ON "exchange_code"
        FOR EACH ROW
        EXECUTE FUNCTION update_data_modified_column();
        
        RAISE NOTICE 'created exchange_code table and trigger';
    ELSE
        RAISE NOTICE 'exchange_code table already exists';
    END IF;
END $$;

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

    -- 若视图存在，先删除（避免字段名冲突）
    IF view_exists THEN
        DROP VIEW exchange_info_view;
        RAISE NOTICE '已删除旧视图 exchange_info_view';
    END IF;

    -- 重新创建视图（无冲突风险）
    CREATE VIEW exchange_info_view AS
    SELECT 
        u.id AS exchange_id,
        n.name AS name,
        sn.short_name AS short_name,
        c.code AS code,
        u.deleted AS deleted
    FROM 
        exchange u
    JOIN 
        exchange_name n ON u.id = n.exchange_id AND n.deleted = FALSE
    JOIN 
        exchange_short_name sn ON u.id = sn.exchange_id AND sn.deleted = FALSE
    JOIN 
        exchange_code c ON u.id = c.exchange_id AND c.deleted = FALSE
    WHERE
        u.deleted = FALSE;

    IF view_exists THEN
        RAISE NOTICE '视图 exchange_info_view 已更新（删除旧视图后重建）';
    ELSE
        RAISE NOTICE '视图 exchange_info_view 已创建';
    END IF;
EXCEPTION
    WHEN OTHERS THEN
        RAISE NOTICE '处理视图时发生错误: %', SQLERRM;
END $$;
