BEGIN;

-- ============================================================================
-- govdoc 模块前端路由 / 菜单初始化草案
-- 目标：
--   1. 注册 /govdoc 模块根路由与常用子路由
--   2. 尽量兼容当前 sys_routes 的父子层级展示方式
--   3. 为默认角色补齐 role_route 菜单可见性
-- 说明：
--   - 采用 route_path / route_name / route_title 这一套当前仓库主流字段风格
--   - 明细页 / 详情页默认 hidden，避免菜单树出现无意义叶子节点
--   - 幂等执行：重复跑会更新标题、父子关系、显示状态
-- ============================================================================

WITH upsert_root AS (
    INSERT INTO sys_routes (
        route_path,
        route_name,
        component,
        parent_id,
        route_title,
        icon,
        sort_order,
        is_hidden,
        is_cache,
        meta,
        status,
        created_at,
        updated_at,
        deleted_at
    )
    VALUES (
        '/govdoc',
        'govdoc',
        'govdoc',
        NULL,
        '内部公文处理',
        'ri-file-paper-2-line',
        80,
        FALSE,
        TRUE,
        '{"group":"govdoc","module":"govdoc"}'::jsonb,
        0,
        NOW(),
        NOW(),
        NULL
    )
    ON CONFLICT (route_path) WHERE deleted_at IS NULL
    DO UPDATE SET
        route_name = EXCLUDED.route_name,
        component = EXCLUDED.component,
        route_title = EXCLUDED.route_title,
        icon = EXCLUDED.icon,
        sort_order = EXCLUDED.sort_order,
        is_hidden = EXCLUDED.is_hidden,
        is_cache = EXCLUDED.is_cache,
        meta = EXCLUDED.meta,
        status = 0,
        updated_at = NOW(),
        deleted_at = NULL
    RETURNING id
),
root_route AS (
    SELECT id FROM upsert_root
    UNION ALL
    SELECT id
    FROM sys_routes
    WHERE route_path = '/govdoc'
      AND deleted_at IS NULL
    LIMIT 1
)
INSERT INTO sys_routes (
    route_path,
    route_name,
    component,
    parent_id,
    route_title,
    icon,
    sort_order,
    is_hidden,
    is_cache,
    meta,
    status,
    created_at,
    updated_at,
    deleted_at
)
VALUES
    (
        '/govdoc/upload',
        'govdoc.upload',
        'govdoc.upload',
        (SELECT id FROM root_route),
        '上传公文',
        'ri-upload-cloud-2-line',
        1,
        FALSE,
        TRUE,
        '{"group":"govdoc","module":"govdoc","page":"upload"}'::jsonb,
        0,
        NOW(),
        NOW(),
        NULL
    ),
    (
        '/govdoc/list',
        'govdoc.list',
        'govdoc.list',
        (SELECT id FROM root_route),
        '公文列表',
        'ri-file-list-3-line',
        2,
        FALSE,
        TRUE,
        '{"group":"govdoc","module":"govdoc","page":"list"}'::jsonb,
        0,
        NOW(),
        NOW(),
        NULL
    ),
    (
        '/govdoc/detail',
        'govdoc.detail',
        'govdoc.detail',
        (SELECT id FROM root_route),
        '公文详情',
        'ri-file-search-line',
        3,
        TRUE,
        TRUE,
        '{"group":"govdoc","module":"govdoc","page":"detail"}'::jsonb,
        0,
        NOW(),
        NOW(),
        NULL
    ),
    (
        '/govdoc/rules',
        'govdoc.rules',
        'govdoc.rules',
        (SELECT id FROM root_route),
        '规则配置',
        'ri-scales-3-line',
        4,
        FALSE,
        TRUE,
        '{"group":"govdoc","module":"govdoc","page":"rules"}'::jsonb,
        0,
        NOW(),
        NOW(),
        NULL
    ),
    (
        '/govdoc/settings',
        'govdoc.settings',
        'govdoc.settings',
        (SELECT id FROM root_route),
        '模块配置',
        'ri-settings-3-line',
        5,
        FALSE,
        TRUE,
        '{"group":"govdoc","module":"govdoc","page":"settings"}'::jsonb,
        0,
        NOW(),
        NOW(),
        NULL
    )
ON CONFLICT (route_path) WHERE deleted_at IS NULL
DO UPDATE SET
    route_name = EXCLUDED.route_name,
    component = EXCLUDED.component,
    parent_id = EXCLUDED.parent_id,
    route_title = EXCLUDED.route_title,
    icon = EXCLUDED.icon,
    sort_order = EXCLUDED.sort_order,
    is_hidden = EXCLUDED.is_hidden,
    is_cache = EXCLUDED.is_cache,
    meta = EXCLUDED.meta,
    status = 0,
    updated_at = NOW(),
    deleted_at = NULL;

-- 修正旧环境中可能已存在但未正确挂到 /govdoc 下的子路由。
UPDATE sys_routes AS child
SET
    parent_id = root.id,
    route_title = CASE
        WHEN child.route_path = '/govdoc/upload' THEN '上传公文'
        WHEN child.route_path = '/govdoc/list' THEN '公文列表'
        WHEN child.route_path = '/govdoc/detail' THEN '公文详情'
        WHEN child.route_path = '/govdoc/rules' THEN '规则配置'
        WHEN child.route_path = '/govdoc/settings' THEN '模块配置'
        ELSE child.route_title
    END,
    is_hidden = CASE
        WHEN child.route_path = '/govdoc/detail' THEN TRUE
        ELSE child.is_hidden
    END,
    updated_at = NOW()
FROM sys_routes root
WHERE child.deleted_at IS NULL
  AND root.deleted_at IS NULL
  AND root.route_path = '/govdoc'
  AND child.route_path IN (
      '/govdoc/upload',
      '/govdoc/list',
      '/govdoc/detail',
      '/govdoc/rules',
      '/govdoc/settings'
  );

-- 根路由标题兜底，避免历史环境残留旧文案。
UPDATE sys_routes
SET route_title = '内部公文处理',
    updated_at = NOW()
WHERE deleted_at IS NULL
  AND route_path = '/govdoc';

WITH role_map AS (
    SELECT id, role_key
    FROM roles
    WHERE role_key IN ('super_admin', 'provincial_admin', 'admin', 'common')
),
route_map AS (
    SELECT id, route_path
    FROM sys_routes
    WHERE deleted_at IS NULL
      AND route_path IN (
        '/govdoc',
        '/govdoc/upload',
        '/govdoc/list',
        '/govdoc/detail',
        '/govdoc/rules',
        '/govdoc/settings'
      )
),
seed(role_key, route_path, permission, status) AS (
    VALUES
        ('super_admin', '/govdoc', 'RW', 1),
        ('super_admin', '/govdoc/upload', 'RW', 1),
        ('super_admin', '/govdoc/list', 'RW', 1),
        ('super_admin', '/govdoc/detail', 'RW', 1),
        ('super_admin', '/govdoc/rules', 'RW', 1),
        ('super_admin', '/govdoc/settings', 'RW', 1),

        ('provincial_admin', '/govdoc', 'RW', 1),
        ('provincial_admin', '/govdoc/upload', 'RW', 1),
        ('provincial_admin', '/govdoc/list', 'RW', 1),
        ('provincial_admin', '/govdoc/detail', 'RW', 1),
        ('provincial_admin', '/govdoc/rules', 'RW', 1),
        ('provincial_admin', '/govdoc/settings', 'RW', 1),

        ('admin', '/govdoc', 'RW', 1),
        ('admin', '/govdoc/upload', 'RW', 1),
        ('admin', '/govdoc/list', 'RW', 1),
        ('admin', '/govdoc/detail', 'RW', 1),
        ('admin', '/govdoc/rules', 'R', 1),

        ('common', '/govdoc', 'R', 1),
        ('common', '/govdoc/upload', 'R', 1),
        ('common', '/govdoc/list', 'R', 1),
        ('common', '/govdoc/detail', 'R', 1),
        ('common', '/govdoc/rules', 'R', 1)
)
INSERT INTO role_route (role_id, route_id, permission, status, created_at, updated_at)
SELECT rm.id, tm.id, s.permission, s.status, NOW(), NOW()
FROM seed s
JOIN role_map rm ON rm.role_key = s.role_key
JOIN route_map tm ON tm.route_path = s.route_path
ON CONFLICT (role_id, route_id) DO UPDATE SET
    permission = EXCLUDED.permission,
    status = EXCLUDED.status,
    updated_at = NOW();

COMMIT;
