import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  // ============================================
  // 初始化权益定义
  // 总计 2199 USDT = 400 + 300 + 9 + 800 + 500 + 15 + 20 + 35 + 40 + 80
  // ============================================
  const rightDefinitions = [
    // === 系统费用类 (1509 USDT) ===
    {
      rightType: 'COST_FEE',
      usdtPerTree: 400,
      hashpowerPercent: 0,
      payableTo: 'SYSTEM_ACCOUNT',
      ruleDescription: '成本费：每棵树400 USDT，进成本账户(userId=2)',
    },
    {
      rightType: 'OPERATION_FEE',
      usdtPerTree: 300,
      hashpowerPercent: 0,
      payableTo: 'SYSTEM_ACCOUNT',
      ruleDescription: '运营费：每棵树300 USDT，进运营账户(userId=3)',
    },
    {
      rightType: 'HEADQUARTERS_BASE_FEE',
      usdtPerTree: 9,
      hashpowerPercent: 0,
      payableTo: 'HEADQUARTERS',
      ruleDescription: '总部社区基础费：每棵树9 USDT，进总部社区账户(userId=1)',
    },
    {
      rightType: 'RWAD_POOL_INJECTION',
      usdtPerTree: 800,
      hashpowerPercent: 0,
      payableTo: 'SYSTEM_ACCOUNT',
      ruleDescription: 'RWAD底池注入：每棵树800 USDT，注入1号底池(userId=4)',
    },

    // === 用户权益类 (690 USDT + 算力) ===
    {
      rightType: 'SHARE_RIGHT',
      usdtPerTree: 500,
      hashpowerPercent: 0,
      payableTo: 'USER_ACCOUNT',
      ruleDescription: '分享权益：每棵树500 USDT，分配给推荐链',
    },
    {
      rightType: 'PROVINCE_AREA_RIGHT',
      usdtPerTree: 15,
      hashpowerPercent: 1,
      payableTo: 'SYSTEM_ACCOUNT',
      ruleDescription: '省区域权益：每棵树15 USDT + 1%算力，进系统省公司账户',
    },
    {
      rightType: 'PROVINCE_TEAM_RIGHT',
      usdtPerTree: 20,
      hashpowerPercent: 0,
      payableTo: 'USER_ACCOUNT',
      ruleDescription: '省团队权益：每棵树20 USDT，给最近的授权省公司',
    },
    {
      rightType: 'CITY_AREA_RIGHT',
      usdtPerTree: 35,
      hashpowerPercent: 2,
      payableTo: 'SYSTEM_ACCOUNT',
      ruleDescription: '市区域权益：每棵树35 USDT + 2%算力，进系统市公司账户',
    },
    {
      rightType: 'CITY_TEAM_RIGHT',
      usdtPerTree: 40,
      hashpowerPercent: 0,
      payableTo: 'USER_ACCOUNT',
      ruleDescription: '市团队权益：每棵树40 USDT，给最近的授权市公司',
    },
    {
      rightType: 'COMMUNITY_RIGHT',
      usdtPerTree: 80,
      hashpowerPercent: 0,
      payableTo: 'USER_ACCOUNT',
      ruleDescription: '社区权益：每棵树80 USDT，给最近的社区',
    },
  ];

  for (const def of rightDefinitions) {
    await prisma.rightDefinition.upsert({
      where: { rightType: def.rightType },
      update: def,
      create: def,
    });
  }

  console.log('Seed completed: Right definitions initialized');
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
