// ==================== 审核管理 Mock 数据 ====================

// 审核状态
const auditStatuses = [
    { value: 'pending', label: '待审核' },
    { value: 'approved', label: '已通过' },
    { value: 'rejected', label: '已拒绝' },
];

// 模拟审核数据
const mockAuditData = [
    {
        id: 'audit_001',
        appId: 'app_006',
        appName: '安全生产管理系统',
        appDescription: '安全生产规范、应急预案、事故案例分析',
        creatorId: 'user_006',
        creator: '孙八',
        department: '安全管理部',
        submitTime: '2026-01-16 15:45:00',
        auditStatus: 'pending',
        auditor: null,
        auditTime: null,
        auditOpinion: null,
        knowledgeBaseCount: 4,
        documentCount: 28,
        sliceCount: 156,
    },
    {
        id: 'audit_002',
        appId: 'app_009',
        appName: '合规管理助手',
        appDescription: '企业合规政策、流程指引、风险预警',
        creatorId: 'user_009',
        creator: '郑十一',
        department: '法务合规部',
        submitTime: '2026-01-19 11:20:00',
        auditStatus: 'rejected',
        auditor: '管理员',
        auditTime: '2026-01-20 09:30:00',
        auditOpinion: '知识库内容不够完善，建议补充相关法规文件',
        knowledgeBaseCount: 2,
        documentCount: 8,
        sliceCount: 42,
    },
    {
        id: 'audit_003',
        appId: 'app_011',
        appName: '质量管理助手',
        appDescription: 'ISO 质量管理体系、质检流程、问题整改跟踪',
        creatorId: 'user_011',
        creator: '钱十三',
        department: '质量管理部',
        submitTime: '2026-01-18 10:00:00',
        auditStatus: 'approved',
        auditor: '管理员',
        auditTime: '2026-01-19 14:20:00',
        auditOpinion: '符合上线标准，予以通过',
        knowledgeBaseCount: 3,
        documentCount: 15,
        sliceCount: 89,
    },
    {
        id: 'audit_004',
        appId: 'app_012',
        appName: '采购管理助手',
        appDescription: '供应商管理、采购流程、合同管理',
        creatorId: 'user_012',
        creator: '赵十四',
        department: '采购部',
        submitTime: '2026-01-20 11:30:00',
        auditStatus: 'pending',
        auditor: null,
        auditTime: null,
        auditOpinion: null,
        knowledgeBaseCount: 2,
        documentCount: 12,
        sliceCount: 67,
    },
    {
        id: 'audit_005',
        appId: 'app_013',
        appName: '档案管理助手',
        appDescription: '电子档案管理、档案借阅、档案分类',
        creatorId: 'user_013',
        creator: '孙十五',
        department: '综合办公室',
        submitTime: '2026-01-15 09:00:00',
        auditStatus: 'approved',
        auditor: '管理员',
        auditTime: '2026-01-16 10:15:00',
        auditOpinion: '符合上线标准',
        knowledgeBaseCount: 4,
        documentCount: 32,
        sliceCount: 198,
    },
    {
        id: 'audit_006',
        appId: 'app_014',
        appName: '知识产权助手',
        appDescription: '专利管理、商标管理、知识产权申请流程',
        creatorId: 'user_014',
        creator: '李十六',
        department: '知识产权部',
        submitTime: '2026-01-21 14:00:00',
        auditStatus: 'pending',
        auditor: null,
        auditTime: null,
        auditOpinion: null,
        knowledgeBaseCount: 3,
        documentCount: 18,
        sliceCount: 95,
    },
];

// 模拟审核历史记录
const mockAuditHistoryData = [
    {
        id: 'history_001',
        auditId: 'audit_002',
        appId: 'app_009',
        appName: '合规管理助手',
        action: 'submit',
        actionName: '提交审核',
        operator: '郑十一',
        operatorRole: 'creator',
        time: '2026-01-19 11:20:00',
        remark: '提交应用审核',
    },
    {
        id: 'history_002',
        auditId: 'audit_002',
        appId: 'app_009',
        appName: '合规管理助手',
        action: 'assigned',
        actionName: '分配审核员',
        operator: '系统',
        operatorRole: 'system',
        time: '2026-01-19 11:25:00',
        remark: '自动分配给审核员：管理员',
    },
    {
        id: 'history_003',
        auditId: 'audit_002',
        appId: 'app_009',
        appName: '合规管理助手',
        action: 'reviewing',
        actionName: '开始审核',
        operator: '管理员',
        operatorRole: 'auditor',
        time: '2026-01-20 09:00:00',
        remark: '审核员开始审核',
    },
    {
        id: 'history_004',
        auditId: 'audit_002',
        appId: 'app_009',
        appName: '合规管理助手',
        action: 'rejected',
        actionName: '拒绝',
        operator: '管理员',
        operatorRole: 'auditor',
        time: '2026-01-20 09:30:00',
        remark: '知识库内容不够完善，建议补充相关法规文件',
    },
    {
        id: 'history_005',
        auditId: 'audit_003',
        appId: 'app_011',
        appName: '质量管理助手',
        action: 'submit',
        actionName: '提交审核',
        operator: '钱十三',
        operatorRole: 'creator',
        time: '2026-01-18 10:00:00',
        remark: '提交应用审核',
    },
    {
        id: 'history_006',
        auditId: 'audit_003',
        appId: 'app_011',
        appName: '质量管理助手',
        action: 'approved',
        actionName: '通过',
        operator: '管理员',
        operatorRole: 'auditor',
        time: '2026-01-19 14:20:00',
        remark: '符合上线标准，予以通过',
    },
];

import { MockRequest } from './index';

/**
 * 获取审核列表
 * POST /deepseek/api/app/audit/list
 */
export const mockFetchAuditList = async (request: MockRequest) => {
    const { pageNum = 1, pageSize = 10, status, keyword, department } = request.data || {};

    console.log('[Mock] 获取审核列表', { pageNum, pageSize, status, keyword, department });

    // 筛选数据
    let filteredData = [...mockAuditData];

    if (status && status !== 'all') {
        filteredData = filteredData.filter(item => item.auditStatus === status);
    }

    if (department) {
        filteredData = filteredData.filter(item => item.department === department);
    }

    if (keyword) {
        filteredData = filteredData.filter(item =>
            item.appName.toLowerCase().includes(keyword.toLowerCase()) ||
            item.creator.toLowerCase().includes(keyword.toLowerCase())
        );
    }

    // 分页
    const total = filteredData.length;
    const startIndex = (pageNum - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const list = filteredData.slice(startIndex, endIndex);

    return {
        code: 200,
        message: 'success',
        data: {
            list,
            total,
            pageNum,
            pageSize,
        },
    };
};

/**
 * 获取审核详情
 * GET /deepseek/api/app/audit/detail/:id
 */
export const mockFetchAuditDetail = async (request: MockRequest) => {
    const auditId = request.params?.id;

    console.log('[Mock] 获取审核详情', { auditId });

    const audit = mockAuditData.find(a => a.id === auditId);

    if (!audit) {
        return {
            code: 404,
            message: '审核记录不存在',
            data: null,
        };
    }

    return {
        code: 200,
        message: 'success',
        data: audit,
    };
};

/**
 * 审核操作（通过/拒绝）
 * PUT /deepseek/api/app/audit/auditAction
 */
export const mockAuditAction = async (request: MockRequest) => {
    const { auditId, action, opinion } = request.data || {};

    console.log('[Mock] 审核操作', { auditId, action, opinion });

    const auditIndex = mockAuditData.findIndex(a => a.id === auditId);

    if (auditIndex === -1) {
        return {
            code: 404,
            message: '审核记录不存在',
            data: null,
        };
    }

    const audit = mockAuditData[auditIndex];

    // 更新审核状态
    mockAuditData[auditIndex] = {
        ...audit,
        auditStatus: action === 'approve' ? 'approved' : 'rejected',
        auditor: '当前审核员',
        auditTime: new Date().toLocaleString('zh-CN'),
        auditOpinion: opinion || null,
    };

    // 同时更新应用状态（模拟）
    const appIndex = mockAuditData.findIndex(a => a.appId === audit.appId);
    if (appIndex !== -1) {
        // 这里只是模拟，实际应用列表在 applicationApi.ts 中
        console.log(`[Mock] 应用 ${audit.appId} 状态已更新为 ${action === 'approve' ? 'online' : 'rejected'}`);
    }

    return {
        code: 200,
        message: action === 'approve' ? '审核通过' : '已拒绝',
        data: {
            auditId,
            action,
            auditTime: new Date().toLocaleString('zh-CN'),
        },
    };
};

/**
 * 获取审核历史列表
 * POST /deepseek/api/app/auditHistory/list
 */
export const mockFetchAuditHistoryList = async (request: MockRequest) => {
    const { auditId, pageNum = 1, pageSize = 20 } = request.data || {};

    console.log('[Mock] 获取审核历史列表', { auditId, pageNum, pageSize });

    // 筛选数据
    let filteredData = [...mockAuditHistoryData];

    if (auditId) {
        filteredData = filteredData.filter(item => item.auditId === auditId);
    }

    // 按时间排序
    filteredData.sort((a, b) => new Date(b.time).getTime() - new Date(a.time).getTime());

    // 分页
    const total = filteredData.length;
    const startIndex = (pageNum - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const list = filteredData.slice(startIndex, endIndex);

    return {
        code: 200,
        message: 'success',
        data: {
            list,
            total,
            pageNum,
            pageSize,
        },
    };
};

/**
 * 获取审核统计
 * GET /deepseek/api/app/audit/stats
 */
export const mockFetchAuditStats = async () => {
    console.log('[Mock] 获取审核统计');

    const total = mockAuditData.length;
    const pending = mockAuditData.filter(a => a.auditStatus === 'pending').length;
    const approved = mockAuditData.filter(a => a.auditStatus === 'approved').length;
    const rejected = mockAuditData.filter(a => a.auditStatus === 'rejected').length;

    return {
        code: 200,
        message: 'success',
        data: {
            total,
            pending,
            approved,
            rejected,
            pendingRate: total > 0 ? ((pending / total) * 100).toFixed(1) : '0',
            approvedRate: total > 0 ? ((approved / total) * 100).toFixed(1) : '0',
            rejectedRate: total > 0 ? ((rejected / total) * 100).toFixed(1) : '0',
        },
    };
};

// ==================== 导出所有 Mock 处理函数 ====================

export const auditMockHandlers = {
    'POST /deepseek/api/app/audit/list': mockFetchAuditList,
    'GET /deepseek/api/app/audit/detail/:id': mockFetchAuditDetail,
    'PUT /deepseek/api/app/audit/auditAction': mockAuditAction,
    'POST /deepseek/api/app/auditHistory/list': mockFetchAuditHistoryList,
    'GET /deepseek/api/app/audit/stats': mockFetchAuditStats,
};
