import { CloudObject } from "src/lib/ncloud";

// 法律文书分类测试数据
const documentCategories = [
  {
    name: "民事诉讼文书",
    description: "与民事纠纷相关的法律文书",
    displayOrder: 1,
    icon: "civil-icon.png",
    isFeatured: true,
    tags: ["民事", "诉讼", "纠纷"]
  },
  {
    name: "刑事诉讼文书",
    description: "与刑事案件相关的法律文书",
    displayOrder: 2,
    icon: "criminal-icon.png",
    isFeatured: true,
    tags: ["刑事", "犯罪", "公诉"]
  },
  {
    name: "行政诉讼文书",
    description: "与行政争议相关的法律文书",
    displayOrder: 3,
    icon: "admin-icon.png",
    isFeatured: false,
    tags: ["行政", "政府", "复议"]
  },
  {
    name: "合同协议文书",
    description: "各类合同协议范本",
    displayOrder: 4,
    icon: "contract-icon.png",
    isFeatured: true,
    tags: ["合同", "协议", "商业"]
  }
];

// 法律文书测试数据
const legalDocuments = [
  {
    title: "民事起诉状范本",
    docType: "起诉状",
    content: "原告：XXX，性别，出生日期，民族，职业，住址...",
    description: "标准民事起诉状模板，适用于一般民事纠纷",
    applicableScope: "合同纠纷、侵权纠纷等民事案件",
    legalBasis: "《中华人民共和国民事诉讼法》第一百二十条",
    keywords: ["民事", "起诉状", "模板"],
    version: 1.2,
    effectiveDate: new Date("2023-01-01"),
    expiryDate: new Date("2025-12-31"),
    isActive: true,
    importance: 5,
    relatedLaws: ["民事诉讼法", "民法典"],
    sampleCases: ["(2023)京01民初123号"],
    notes: "使用时需根据具体案情修改"
  },
  {
    title: "刑事辩护词",
    docType: "辩护词",
    content: "辩护人依法接受委托，现就本案发表如下辩护意见...",
    description: "刑事案件辩护律师使用的标准辩护词",
    applicableScope: "一审刑事案件",
    legalBasis: "《中华人民共和国刑事诉讼法》第三十五条",
    keywords: ["刑事", "辩护", "律师"],
    version: 1.1,
    effectiveDate: new Date("2023-03-15"),
    isActive: true,
    importance: 4,
    relatedLaws: ["刑事诉讼法", "刑法"],
    sampleCases: ["(2023)沪刑初456号"],
    notes: "需结合案件证据材料具体撰写"
  },
  {
    title: "劳动合同范本",
    docType: "合同",
    content: "甲方(用人单位)：XXX公司，地址：...",
    description: "标准劳动合同模板",
    applicableScope: "企业与员工签订劳动合同",
    legalBasis: "《中华人民共和国劳动合同法》第十七条",
    keywords: ["劳动", "合同", "用工"],
    version: 2.0,
    effectiveDate: new Date("2022-06-01"),
    isActive: true,
    importance: 5,
    relatedLaws: ["劳动合同法", "劳动法"],
    sampleCases: ["参考人社局发布的标准文本"],
    notes: "可根据企业实际情况调整补充条款"
  }
];

// 法律知识测试数据
const legalKnowledge = [
  {
    title: "如何正确撰写民事起诉状",
    content: "撰写民事起诉状需要注意以下要点...",
    summary: "民事起诉状撰写指南",
    applicableScenarios: ["准备起诉", "法律咨询", "自我维权"],
    popularity: 8,
    source: "最高人民法院司法案例研究院",
    publishDate: new Date("2023-05-10"),
    author: "张律师",
    keywords: ["起诉状", "撰写", "指南"],
    isVerified: true
  },
  {
    title: "刑事案件辩护策略",
    content: "刑事辩护中常用的策略包括...",
    summary: "刑事辩护方法与技巧",
    applicableScenarios: ["刑事辩护", "律师培训"],
    popularity: 7,
    source: "中国律师协会",
    publishDate: new Date("2023-02-20"),
    author: "李律师",
    keywords: ["刑事", "辩护", "策略"],
    isVerified: true
  },
  {
    title: "劳动合同签订注意事项",
    content: "签订劳动合同时需注意以下事项...",
    summary: "劳动合同签订指南",
    applicableScenarios: ["入职签订", "HR工作", "劳动维权"],
    popularity: 9,
    source: "人力资源和社会保障部",
    publishDate: new Date("2023-04-15"),
    author: "王顾问",
    keywords: ["劳动", "合同", "签订"],
    isVerified: true
  }
];




// 导入函数实现
export class DataImporter {
  /**
   * 批量导入分类数据
   */
  static async importCategories() {
    const results = [];
    for (const categoryData of documentCategories) {
      const category = new CloudObject("DocumentCategory");
      category.set(categoryData);
      await category.save();
      results.push(category);
      console.log(`导入分类: ${categoryData.name}`, category.id);
    }
    return results;
  }

  /**
   * 批量导入法律文书数据
   * @param categoryMap 分类映射表 {分类名称: 分类objectId}
   */
  static async importLegalDocuments(categoryMap: Record<string, string>) {
    const results = [];
    for (const docData of legalDocuments) {
      const doc = new CloudObject("LegalDocument");
      
      // 设置分类指针
      if (docData.docType) {
        let categoryName = "";
        if (docData.docType.includes("民事")) categoryName = "民事诉讼文书";
        else if (docData.docType.includes("刑事")) categoryName = "刑事诉讼文书";
        else if (docData.docType.includes("合同")) categoryName = "合同协议文书";
        
        if (categoryName && categoryMap[categoryName]) {
          doc.set({
            ...docData,
            category: {
              __type: "Pointer",
              className: "DocumentCategory",
              objectId: categoryMap[categoryName]
            }
          });
        }
      }
      
      await doc.save();
      results.push(doc);
      console.log(`导入文书: ${docData.title}`, doc.id);
    }
    return results;
  }

  /**
   * 批量导入法律知识数据
   * @param categoryMap 分类映射表 {分类名称: 分类objectId}
   */
  static async importLegalKnowledge(categoryMap: Record<string, string>) {
    const results = [];
    for (const knowledgeData of legalKnowledge) {
      const knowledge = new CloudObject("LegalKnowledge");
      
      // 设置分类指针
      let categoryName = "";
      if (knowledgeData.title.includes("民事")) categoryName = "民事诉讼文书";
      else if (knowledgeData.title.includes("刑事")) categoryName = "刑事诉讼文书";
      else if (knowledgeData.title.includes("劳动")) categoryName = "合同协议文书";
      
      if (categoryName && categoryMap[categoryName]) {
        knowledge.set({
          ...knowledgeData,
          category: {
            __type: "Pointer",
            className: "DocumentCategory",
            objectId: categoryMap[categoryName]
          }
        });
      }
      
      await knowledge.save();
      results.push(knowledge);
      console.log(`导入知识: ${knowledgeData.title}`, knowledge.id);
    }
    return results;
  }

  /**
   * 执行完整导入流程
   */
  static async executeFullImport() {
    try {
      // 1. 先导入分类
      const categories = await this.importCategories();
      const categoryMap: Record<string, string> = {};
      categories.forEach(cat => {
        const name = cat.get("name");
        if (name && cat.id) {
          categoryMap[name] = cat.id;
        }
      });

      // 2. 导入文书和知识(带分类关联)
      await this.importLegalDocuments(categoryMap);
      await this.importLegalKnowledge(categoryMap);

      console.log("所有数据导入完成");
      return true;
    } catch (error) {
      console.error("数据导入失败:", error);
      return false;
    }
  }
}

// 使用示例
// DataImporter.executeFullImport();

