-- =============================================
-- Business Module Menus Seed Script
-- 学业邑规划 - MiAssessment
-- 
-- This script inserts business module menus
-- into the Admin database menus table
--
-- Database: SQL Server 2022
-- Run after: init_admin_db.sql, seed_business_permissions.sql
-- =============================================

USE [MiAssessment_Admin];
GO

PRINT N'Seeding business module menus...';

-- =============================================
-- Get max menu ID to avoid conflicts
-- =============================================
DECLARE @maxMenuId INT;
SELECT @maxMenuId = ISNULL(MAX([Id]), 0) FROM [dbo].[menus];
PRINT N'Current max menu ID: ' + CAST(@maxMenuId AS NVARCHAR(10));

-- =============================================
-- 1. 数据统计 (Dashboard)
-- =============================================
IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/dashboard')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'数据统计', '/dashboard', 'dashboard/index', 'dashboard', 2, 'dashboard:view', 0, 1, GETDATE());
    PRINT N'Inserted menu: 数据统计';
END

-- =============================================
-- 2. 测评管理 (Assessment)
-- =============================================
DECLARE @assessmentMenuId BIGINT;

IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/assessment')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'测评管理', '/assessment', 'Layout', 'education', 1, NULL, 3, 1, GETDATE());
    SET @assessmentMenuId = SCOPE_IDENTITY();
    PRINT N'Inserted menu: 测评管理';
END
ELSE
BEGIN
    SELECT @assessmentMenuId = [Id] FROM [dbo].[menus] WHERE [Path] = '/assessment';
END

-- 测评类型
IF @assessmentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/assessment/type')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@assessmentMenuId, N'测评类型', '/assessment/type', 'assessment/type/index', 'list', 2, 'assessment:view', 1, 1, GETDATE());
    PRINT N'Inserted menu: 测评类型';
END

-- 题库管理
IF @assessmentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/assessment/question')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@assessmentMenuId, N'题库管理', '/assessment/question', 'assessment/question/index', 'edit-pen', 2, 'assessment:view', 2, 1, GETDATE());
    PRINT N'Inserted menu: 题库管理';
END

-- 报告分类
IF @assessmentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/assessment/category')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@assessmentMenuId, N'报告分类', '/assessment/category', 'assessment/category/index', 'tree', 2, 'assessment:view', 3, 1, GETDATE());
    PRINT N'Inserted menu: 报告分类';
END

-- 报告结论
IF @assessmentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/assessment/conclusion')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@assessmentMenuId, N'报告结论', '/assessment/conclusion', 'assessment/conclusion/index', 'document', 2, 'assessment:view', 4, 1, GETDATE());
    PRINT N'Inserted menu: 报告结论';
END

-- 测评记录
IF @assessmentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/assessment/record')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@assessmentMenuId, N'测评记录', '/assessment/record', 'business/assessment/record/index', 'tickets', 2, 'assessmentRecord:view', 6, 1, GETDATE());
    PRINT N'Inserted menu: 测评记录';
END

-- =============================================
-- 3. 订单管理 (Order)
-- =============================================
DECLARE @orderMenuId BIGINT;

IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/order')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'订单管理', '/order', 'Layout', 'shopping-cart', 1, NULL, 4, 1, GETDATE());
    SET @orderMenuId = SCOPE_IDENTITY();
    PRINT N'Inserted menu: 订单管理';
END
ELSE
BEGIN
    SELECT @orderMenuId = [Id] FROM [dbo].[menus] WHERE [Path] = '/order';
END

-- 订单列表
IF @orderMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/order/list')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@orderMenuId, N'订单列表', '/order/list', 'order/list/index', 'list', 2, 'order:view', 1, 1, GETDATE());
    PRINT N'Inserted menu: 订单列表';
END

-- 退款管理
IF @orderMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/order/refund')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@orderMenuId, N'退款管理', '/order/refund', 'order/refund/index', 'money', 2, 'order:view', 2, 1, GETDATE());
    PRINT N'Inserted menu: 退款管理';
END

-- =============================================
-- 4. 规划师管理 (Planner)
-- =============================================
DECLARE @plannerMenuId BIGINT;

IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/planner')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'规划师管理', '/planner', 'Layout', 'avatar', 1, NULL, 5, 1, GETDATE());
    SET @plannerMenuId = SCOPE_IDENTITY();
    PRINT N'Inserted menu: 规划师管理';
END
ELSE
BEGIN
    SELECT @plannerMenuId = [Id] FROM [dbo].[menus] WHERE [Path] = '/planner';
END

-- 规划师列表
IF @plannerMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/planner/list')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@plannerMenuId, N'规划师列表', '/planner/list', 'planner/list/index', 'user', 2, 'planner:view', 1, 1, GETDATE());
    PRINT N'Inserted menu: 规划师列表';
END

-- 预约记录
IF @plannerMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/planner/booking')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@plannerMenuId, N'预约记录', '/planner/booking', 'planner/booking/index', 'calendar', 2, 'planner:view', 2, 1, GETDATE());
    PRINT N'Inserted menu: 预约记录';
END

-- =============================================
-- 5. 分销管理 (Distribution)
-- =============================================
DECLARE @distributionMenuId BIGINT;

IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/distribution')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'分销管理', '/distribution', 'Layout', 'share', 1, NULL, 6, 1, GETDATE());
    SET @distributionMenuId = SCOPE_IDENTITY();
    PRINT N'Inserted menu: 分销管理';
END
ELSE
BEGIN
    SELECT @distributionMenuId = [Id] FROM [dbo].[menus] WHERE [Path] = '/distribution';
END

-- 邀请码管理
IF @distributionMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/distribution/invite-code')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@distributionMenuId, N'邀请码管理', '/distribution/invite-code', 'distribution/invite-code/index', 'ticket', 2, 'distribution:view', 1, 1, GETDATE());
    PRINT N'Inserted menu: 邀请码管理';
END

-- 佣金记录
IF @distributionMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/distribution/commission')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@distributionMenuId, N'佣金记录', '/distribution/commission', 'distribution/commission/index', 'coin', 2, 'distribution:view', 2, 1, GETDATE());
    PRINT N'Inserted menu: 佣金记录';
END

-- 提现管理
IF @distributionMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/distribution/withdrawal')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@distributionMenuId, N'提现管理', '/distribution/withdrawal', 'distribution/withdrawal/index', 'wallet', 2, 'distribution:view', 3, 1, GETDATE());
    PRINT N'Inserted menu: 提现管理';
END

-- =============================================
-- 6. 内容管理 (Content)
-- =============================================
DECLARE @contentMenuId BIGINT;

IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/content')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'内容管理', '/content', 'Layout', 'picture', 1, NULL, 7, 1, GETDATE());
    SET @contentMenuId = SCOPE_IDENTITY();
    PRINT N'Inserted menu: 内容管理';
END
ELSE
BEGIN
    SELECT @contentMenuId = [Id] FROM [dbo].[menus] WHERE [Path] = '/content';
END

-- 轮播图管理
IF @contentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/content/banner')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@contentMenuId, N'轮播图管理', '/content/banner', 'content/banner/index', 'picture', 2, 'content:view', 1, 1, GETDATE());
    PRINT N'Inserted menu: 轮播图管理';
END

-- 宣传图管理
IF @contentMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/content/promotion')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@contentMenuId, N'宣传图管理', '/content/promotion', 'content/promotion/index', 'picture-filled', 2, 'content:view', 2, 1, GETDATE());
    PRINT N'Inserted menu: 宣传图管理';
END

-- =============================================
-- 7. 系统配置 (Config) - 业务配置
-- =============================================
DECLARE @configMenuId BIGINT;

IF NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/config')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (0, N'业务配置', '/config', 'Layout', 'setting', 1, NULL, 8, 1, GETDATE());
    SET @configMenuId = SCOPE_IDENTITY();
    PRINT N'Inserted menu: 业务配置';
END
ELSE
BEGIN
    SELECT @configMenuId = [Id] FROM [dbo].[menus] WHERE [Path] = '/config';
END

-- 基础配置
IF @configMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/config/basic')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@configMenuId, N'基础配置', '/config/basic', 'config/basic/index', 'tools', 2, 'config:view', 1, 1, GETDATE());
    PRINT N'Inserted menu: 基础配置';
END

-- 支付配置
IF @configMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/config/payment')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@configMenuId, N'支付配置', '/config/payment', 'config/payment/index', 'credit-card', 2, 'config:view', 2, 1, GETDATE());
    PRINT N'Inserted menu: 支付配置';
END

-- 分销配置
IF @configMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[menus] WHERE [Path] = '/config/distribution')
BEGIN
    INSERT INTO [dbo].[menus] ([ParentId], [Name], [Path], [Component], [Icon], [MenuType], [Permission], [SortOrder], [Status], [CreatedAt])
    VALUES (@configMenuId, N'分销配置', '/config/distribution', 'config/distribution/index', 'share', 2, 'config:view', 3, 1, GETDATE());
    PRINT N'Inserted menu: 分销配置';
END

-- =============================================
-- 8. Assign new menus to Super Admin Role (RoleId = 1)
-- =============================================
PRINT N'Assigning new menus to super_admin role...';

INSERT INTO [dbo].[role_menus] ([RoleId], [MenuId])
SELECT 1, m.[Id]
FROM [dbo].[menus] m
WHERE NOT EXISTS (
    SELECT 1 FROM [dbo].[role_menus] rm 
    WHERE rm.[RoleId] = 1 AND rm.[MenuId] = m.[Id]
);

PRINT N'New menus assigned to super_admin role';
GO

-- =============================================
-- Verify inserted menus
-- =============================================
PRINT N'';
PRINT N'Business module menus seeded successfully!';
PRINT N'';

SELECT 
    m.[Id],
    m.[ParentId],
    m.[Name],
    m.[Path],
    m.[Component],
    m.[Icon],
    CASE m.[MenuType] 
        WHEN 1 THEN N'目录'
        WHEN 2 THEN N'菜单'
        WHEN 3 THEN N'按钮'
    END AS [MenuType],
    m.[Permission],
    m.[SortOrder],
    m.[Status]
FROM [dbo].[menus] m
WHERE m.[Path] IN (
    '/dashboard',
    '/assessment', '/assessment/type', '/assessment/question', '/assessment/category', '/assessment/conclusion', '/assessment/record',
    '/order', '/order/list', '/order/refund',
    '/planner', '/planner/list', '/planner/booking',
    '/distribution', '/distribution/invite-code', '/distribution/commission', '/distribution/withdrawal',
    '/content', '/content/banner', '/content/promotion',
    '/config', '/config/basic', '/config/payment', '/config/distribution'
)
ORDER BY m.[SortOrder], m.[ParentId], m.[Id];
GO
