import Parse from 'parse';

// 定义玄幻漫画分类计划数据
async function importFantasyComicPlan() {
  try {
    await Parse.Cloud.run('testConnection');
    console.log('Parse server connected!');
    // 初始化Parse
    Parse.initialize("YOUR_APP_ID", "YOUR_JS_KEY");
    Parse.serverURL = 'https://your.parse-server.com/parse';

    // 1. 创建用户 (如果不存在)
    const User = Parse.Object.extend("_User");
    const creator = new User();
    creator.set("username", "fantasy_artist");
    creator.set("email", "fantasy@example.com");
    creator.set("password", "fantasy123");
    creator.set("emailVerified", true);
    
    const savedCreator = await creator.save(null, { useMasterKey: true });
    console.log("创作者用户创建成功:", savedCreator.id);

    // 2. 创建玄幻漫画计划
    const ComicPlan = Parse.Object.extend("ComicPlan");
    const fantasyPlan = new ComicPlan();
    
    fantasyPlan.set("planName", "玄幻修仙系列漫画");
    fantasyPlan.set("description", "创作一部关于修仙世界的长篇玄幻漫画，包含修真体系、法宝、灵兽等元素");
    fantasyPlan.set("coverImage", new Parse.File("cover.jpg", { base64: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" }));
    fantasyPlan.set("totalTasks", 6);
    fantasyPlan.set("completedTasks", 0);
    fantasyPlan.set("status", "active");
    fantasyPlan.set("difficulty", "advanced");
    fantasyPlan.set("tags", ["fantasy", "cultivation", "action", "long-term"]);
    fantasyPlan.set("creator", savedCreator);
    fantasyPlan.set("dueDate", new Date(Date.now() + 90 * 24 * 60 * 60 * 1000)); // 90天后
    
    const savedPlan = await fantasyPlan.save();
    console.log("玄幻漫画计划创建成功:", savedPlan.id);

    // 3. 创建玄幻漫画任务
    const ComicTask = Parse.Object.extend("ComicTask");
    
    // 任务1: 世界观设定
    const task1 = new ComicTask();
    task1.set("taskName", "世界观与修真体系设定");
    task1.set("description", "设计修真等级体系(炼气、筑基、金丹等)、各大门派势力分布、主要地图");
    task1.set("step", 1);
    task1.set("type", "storyboard");
    task1.set("isCompleted", false);
    task1.set("priority", 5);
    task1.set("estimatedHours", 20);
    task1.set("plan", savedPlan);
    task1.set("assignee", savedCreator);
    task1.set("startDate", new Date());
    task1.set("endDate", new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)); // 7天后
    await task1.save();
    
    // 任务2: 主角设计
    const task2 = new ComicTask();
    task2.set("taskName", "主角与主要角色设计");
    task2.set("description", "设计主角形象、性格、成长路线，以及3-5个重要配角");
    task2.set("step", 2);
    task2.set("type", "sketch");
    task2.set("referenceImages", [
      new Parse.File("ref1.jpg", { base64: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" }),
      new Parse.File("ref2.jpg", { base64: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" })
    ]);
    task2.set("isCompleted", false);
    task2.set("priority", 4);
    task2.set("estimatedHours", 15);
    task2.set("plan", savedPlan);
    task2.set("assignee", savedCreator);
    task2.set("startDate", new Date(Date.now() + 5 * 24 * 60 * 60 * 1000));
    task2.set("endDate", new Date(Date.now() + 12 * 24 * 60 * 60 * 1000));
    await task2.save();
    
    // 任务3: 第一话分镜
    const task3 = new ComicTask();
    task3.set("taskName", "第一话分镜设计");
    task3.set("description", "完成第一话(主角觉醒灵根，加入门派)的完整分镜");
    task3.set("step", 3);
    task3.set("type", "storyboard");
    task3.set("isCompleted", false);
    task3.set("priority", 5);
    task3.set("estimatedHours", 25);
    task3.set("plan", savedPlan);
    task3.set("assignee", savedCreator);
    task3.set("startDate", new Date(Date.now() + 10 * 24 * 60 * 60 * 1000));
    task3.set("endDate", new Date(Date.now() + 20 * 24 * 60 * 60 * 1000));
    await task3.save();
    
    // 任务4: 法宝与灵兽设计
    const task4 = new ComicTask();
    task4.set("taskName", "法宝与灵兽设计");
    task4.set("description", "设计主角的初始法宝和伴生灵兽，以及3-5种常见法宝");
    task4.set("step", 4);
    task4.set("type", "sketch");
    task4.set("isCompleted", false);
    task4.set("priority", 3);
    task4.set("estimatedHours", 12);
    task4.set("plan", savedPlan);
    task4.set("assignee", savedCreator);
    task4.set("startDate", new Date(Date.now() + 15 * 24 * 60 * 60 * 1000));
    task4.set("endDate", new Date(Date.now() + 22 * 24 * 60 * 60 * 1000));
    await task4.save();
    
    // 任务5: 第一话线稿
    const task5 = new ComicTask();
    task5.set("taskName", "第一话线稿绘制");
    task5.set("description", "根据分镜完成第一话的完整线稿");
    task5.set("step", 5);
    task5.set("type", "lineart");
    task5.set("isCompleted", false);
    task5.set("priority", 4);
    task5.set("estimatedHours", 30);
    task5.set("plan", savedPlan);
    task5.set("assignee", savedCreator);
    task5.set("startDate", new Date(Date.now() + 20 * 24 * 60 * 60 * 1000));
    task5.set("endDate", new Date(Date.now() + 35 * 24 * 60 * 60 * 1000));
    await task5.save();
    
    // 任务6: 第一话上色
    const task6 = new ComicTask();
    task6.set("taskName", "第一话上色与特效");
    task6.set("description", "完成第一话的上色工作，添加修真特效(灵气、法宝光效等)");
    task6.set("step", 6);
    task6.set("type", "coloring");
    task6.set("isCompleted", false);
    task6.set("priority", 4);
    task6.set("estimatedHours", 25);
    task6.set("plan", savedPlan);
    task6.set("assignee", savedCreator);
    task6.set("startDate", new Date(Date.now() + 35 * 24 * 60 * 60 * 1000));
    task6.set("endDate", new Date(Date.now() + 50 * 24 * 60 * 60 * 1000));
    await task6.save();
    
    console.log("6个玄幻漫画任务已成功创建");
    
    // 更新计划的任务总数
    savedPlan.set("totalTasks", 6);
    await savedPlan.save();
    
    console.log("玄幻漫画分类计划导入完成!");
    
  } catch (error) {
    console.error('Failed to connect to Parse server:', error);
  }
}

// 执行导入
importFantasyComicPlan();