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

// 鸟类测试数据
const testBirds = [
    {
        commonName: "红腹锦鸡",
        scientificName: "Chrysolophus pictus",
        description: "又名金鸡，中型鸡类，体长59-110厘米。尾特长，约38-42厘米。雄鸟羽色华丽，头具金黄色丝状羽冠，上体除上背浓绿色外，其余为金黄色，后颈被有橙棕色而缀有黑边的扇状羽，形成披肩状。下体深红色，尾羽黑褐色，满缀以桂黄色斑点。",
        conservationStatus: "无危",
        avgWeight: 0.8,
        avgLength: 90,
        wingspan: 65,
        lifespan: 10,
        diet: "杂食性，主要以植物的叶、芽、花、果实和种子为食，也吃小麦、大豆、玉米、四季豆等农作物。此外也吃甲虫、蠕虫、双翅目和鳞翅目昆虫等动物性食物。",
        habitat: "常栖息于海拔500-2500米的阔叶林、针阔叶混交林和林缘疏林灌丛地带",
        migrationPattern: "留鸟",
        breedingSeason: "4-6月",
        clutchSize: 8,
        incubationPeriod: 22,
        vocalization: "繁殖期间雄鸟常发出'cha、cha'的叫声和'cha-cha-cha'的连续叫声",
        identificationTips: "雄鸟羽色艳丽，具有金色羽冠和红色腹部；雌鸟通体棕黄，密布黑褐色虫蠹状斑",
        behaviorNotes: "白天多活动，尤以早晨和下午活动较多，中午多在隐蔽处休息，晚上栖息于树冠隐蔽处",
        distributionRange: "中国中部和西部山区",
        similarSpecies: ["白腹锦鸡"],
        isEndangered: false,
        isExtinct: false,
        featured: true
    },
    {
        commonName: "丹顶鹤",
        scientificName: "Grus japonensis",
        description: "大型涉禽，体长120-160厘米。颈、脚较长，通体大多白色，头顶鲜红色，喉和颈黑色，耳至头枕白色，脚黑色，站立时颈、尾部飞羽和脚黑色，头顶红色，其余全为白色。",
        conservationStatus: "濒危",
        avgWeight: 7.5,
        avgLength: 150,
        wingspan: 220,
        lifespan: 30,
        diet: "杂食性，主要以鱼、虾、水生昆虫、软体动物、蝌蚪、沙蚕、蛤蜊、钉螺以及水生植物的茎、叶、块根、球茎和果实为食。",
        habitat: "栖息于开阔平原、沼泽、湖泊、草地、海边滩涂、芦苇、沼泽以及河岸沼泽地带",
        migrationPattern: "部分迁徙，部分留鸟",
        breedingSeason: "4-6月",
        clutchSize: 2,
        incubationPeriod: 31,
        vocalization: "高亢宏亮的鸣叫声",
        identificationTips: "头顶红色，身体白色，颈部黑色",
        behaviorNotes: "常成对或成家族群和小群活动。迁徙季节和冬季，常由数个或数十个家族群结成较大的群体。",
        distributionRange: "东亚地区",
        similarSpecies: ["白鹤", "灰鹤"],
        isEndangered: true,
        isExtinct: false,
        featured: true
    },
    // 更多鸟类数据...
];

// 分类学测试数据
const testTaxonomies = [
    {
        kingdom: "动物界",
        phylum: "脊索动物门",
        class: "鸟纲",
        order: "鸡形目",
        family: "雉科",
        genus: "锦鸡属",
        species: "红腹锦鸡",
        scientificName: "Chrysolophus pictus",
        description: "锦鸡属鸟类特征描述...",
        characteristics: ["鲜艳羽毛", "性二态性明显", "陆禽"],
        evolutionaryHistory: "起源于亚洲东部山区"
    },
    {
        kingdom: "动物界",
        phylum: "脊索动物门",
        class: "鸟纲",
        order: "鹤形目",
        family: "鹤科",
        genus: "鹤属",
        species: "丹顶鹤",
        scientificName: "Grus japonensis",
        description: "鹤属鸟类特征描述...",
        characteristics: ["长腿", "长颈", "大型涉禽"],
        evolutionaryHistory: "古老物种，化石记录可追溯到中新世"
    },
    // 更多分类数据...
];

// 专业知识测试数据
const testExpertise = [
    {
        title: "如何观察红腹锦鸡",
        content: "观察红腹锦鸡的最佳时间是清晨和傍晚...详细观察技巧...",
        author: "张观鸟",
        experienceLevel: "中级",
        applicationScenarios: ["野外考察", "生态摄影", "生物多样性调查"],
        bestPractices: "穿着迷彩服，保持安静，使用望远镜远距离观察",
        equipmentRecommendations: ["8-10倍双筒望远镜", "400mm以上长焦镜头", "迷彩伪装"],
        seasonalityTips: "春季繁殖期雄鸟羽毛最艳丽，行为活跃",
        locationTips: "四川、甘肃、陕西等地的山区阔叶林",
        isVerified: true,
        references: ["《中国鸟类野外手册》", "《中国雉类》"]
    },
    // 更多专业知识数据...
];


// 导入鸟类数据
async function importBirds(birds: any[]) {
    const results = [];
    for (const birdData of birds) {
        const bird = new CloudObject('Bird');
        bird.set(birdData);
        await bird.save();
        results.push(bird);
        console.log(`导入鸟类: ${bird.get('commonName')} (ID: ${bird.id})`);
    }
    return results;
}

// 导入分类数据
async function importTaxonomies(taxonomies: any[]) {
    const results = [];
    for (const taxData of taxonomies) {
        const tax = new CloudObject('Taxonomy');
        tax.set(taxData);
        await tax.save();
        results.push(tax);
        console.log(`导入分类: ${tax.get('species')} (ID: ${tax.id})`);
    }
    return results;
}

// 导入专业知识数据
async function importExpertise(expertiseList: any[], relatedBirds: any[]) {
    const results = [];
    for (const expData of expertiseList) {
        const exp = new CloudObject('BirdExpertise');

        // 关联鸟类
        if (expData.relatedBirds) {
            expData.relatedBirds = expData.relatedBirds.map((birdName: string) => {
                const bird = relatedBirds.find(b => b.get('commonName') === birdName);
                return bird ? bird.toPointer() : null;
            }).filter((b: any) => b !== null);
        }

        exp.set(expData);
        await exp.save();
        results.push(exp);
        console.log(`导入专业知识: ${exp.get('title')} (ID: ${exp.id})`);
    }
    return results;
}

// 完整导入流程
export async function importAllData() {
    try {
        console.log('开始导入分类数据...');
        const taxonomies = await importTaxonomies(testTaxonomies);

        console.log('\n开始导入鸟类数据...');
        const birds = await importBirds(testBirds);

        // 建立鸟类和分类的关联
        for (const bird of birds) {
            const scientificName = bird.get('scientificName');
            const taxonomy = taxonomies.find(t => t.get('scientificName') === scientificName);
            if (taxonomy) {
                bird.set({ taxonomy: taxonomy.toPointer() });
                await bird.save();
                console.log(`关联 ${bird.get('commonName')} 与分类 ${taxonomy.get('species')}`);
            }
        }

        console.log('\n开始导入专业知识数据...');
        await importExpertise(testExpertise, birds);

        console.log('\n所有数据导入完成！');
    } catch (error) {
        console.error('导入过程中出错:', error);
    }
}
