-- ============================================================================
-- govdoc 模块入口初始化脚本
-- 用途：
--   1. 为“内部公文 / govdoc”模块补齐首页入口配置
--   2. 使用幂等写法，重复执行不会产生重复数据
--   3. 兼容历史库中 entry_modules 时间字段命名差异
--
-- 说明：
--   - 当前模块入口统一收口到 /govdoc/list，与模块路由 seed 保持一致。
--   - 若后续前端改成其他入口页，请同步更新本脚本中的 v_target_path。
--   - 页面访问权限与左侧菜单路由仍依赖 sys_routes / role_route，
--     这部分建议由配套脚本 seed_govdoc_routes.sql 单独维护。
-- ============================================================================

BEGIN;

DO $$
DECLARE
    v_name          text := '内部公文';
    v_description   text := '内部公文处理与格式审查入口';
    v_target_path   text := '/govdoc/list';
    v_icon_path     text := 'documents/mz/static/img/entry_module_3.png';
    v_sort_order    integer := 30;
    v_areas         jsonb := '[
        {"area":"梅州","enabled":true,"sort_order":1},
        {"area":"云浮","enabled":true,"sort_order":2},
        {"area":"揭阳","enabled":true,"sort_order":3},
        {"area":"潮州","enabled":true,"sort_order":4},
        {"area":"省局","enabled":true,"sort_order":5}
    ]'::jsonb;
    has_created_at  boolean;
    has_updated_at  boolean;
    has_create_time boolean;
    has_update_time boolean;
    has_deleted_at  boolean;
    sql_stmt        text;
BEGIN
    SELECT EXISTS (
               SELECT 1
               FROM information_schema.columns
               WHERE table_schema = 'public'
                 AND table_name = 'leaudit_entry_modules'
                 AND column_name = 'created_at'
           ),
           EXISTS (
               SELECT 1
               FROM information_schema.columns
               WHERE table_schema = 'public'
                 AND table_name = 'leaudit_entry_modules'
                 AND column_name = 'updated_at'
           ),
           EXISTS (
               SELECT 1
               FROM information_schema.columns
               WHERE table_schema = 'public'
                 AND table_name = 'leaudit_entry_modules'
                 AND column_name = 'create_time'
           ),
           EXISTS (
               SELECT 1
               FROM information_schema.columns
               WHERE table_schema = 'public'
                 AND table_name = 'leaudit_entry_modules'
                 AND column_name = 'update_time'
           ),
           EXISTS (
               SELECT 1
               FROM information_schema.columns
               WHERE table_schema = 'public'
                 AND table_name = 'leaudit_entry_modules'
                 AND column_name = 'deleted_at'
           )
      INTO has_created_at, has_updated_at, has_create_time, has_update_time, has_deleted_at;

    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.tables
        WHERE table_schema = 'public'
          AND table_name = 'leaudit_entry_modules'
    ) THEN
        RAISE EXCEPTION '表 public.leaudit_entry_modules 不存在，请先执行入口模块建表脚本';
    END IF;

    sql_stmt := 'INSERT INTO public.leaudit_entry_modules (' ||
                'name, description, path, icon_path, areas, sort_order, is_enabled';

    IF has_created_at THEN
        sql_stmt := sql_stmt || ', created_at';
    ELSIF has_create_time THEN
        sql_stmt := sql_stmt || ', create_time';
    END IF;

    IF has_updated_at THEN
        sql_stmt := sql_stmt || ', updated_at';
    ELSIF has_update_time THEN
        sql_stmt := sql_stmt || ', update_time';
    END IF;

    IF has_deleted_at THEN
        sql_stmt := sql_stmt || ', deleted_at';
    END IF;

    sql_stmt := sql_stmt || ') VALUES (' ||
                '$1, $2, $3, $4, $5, $6, TRUE';

    IF has_created_at OR has_create_time THEN
        sql_stmt := sql_stmt || ', NOW()';
    END IF;

    IF has_updated_at OR has_update_time THEN
        sql_stmt := sql_stmt || ', NOW()';
    END IF;

    IF has_deleted_at THEN
        sql_stmt := sql_stmt || ', NULL';
    END IF;

    sql_stmt := sql_stmt || ') ON CONFLICT (name) DO UPDATE SET ' ||
                'description = EXCLUDED.description, ' ||
                'path = EXCLUDED.path, ' ||
                'icon_path = EXCLUDED.icon_path, ' ||
                'areas = EXCLUDED.areas, ' ||
                'sort_order = EXCLUDED.sort_order, ' ||
                'is_enabled = EXCLUDED.is_enabled';

    IF has_updated_at THEN
        sql_stmt := sql_stmt || ', updated_at = NOW()';
    ELSIF has_update_time THEN
        sql_stmt := sql_stmt || ', update_time = NOW()';
    END IF;

    IF has_deleted_at THEN
        sql_stmt := sql_stmt || ', deleted_at = NULL';
    END IF;

    EXECUTE sql_stmt
        USING v_name, v_description, v_target_path, v_icon_path, v_areas, v_sort_order;
END $$;

COMMIT;
