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

const prisma = new PrismaClient();

async function main() {
  // 初始化报表定义
  const reportDefinitions = [
    {
      reportType: 'LEADERBOARD_REPORT',
      reportName: '龙虎榜数据报表',
      reportCode: 'RPT_LEADERBOARD',
      description: '龙虎榜日榜/周榜/月榜排名数据统计',
      parameters: {
        dimensions: ['TIME', 'USER'],
        defaultPeriod: 'DAILY',
      },
      outputFormats: ['EXCEL', 'CSV'],
      isActive: true,
    },
    {
      reportType: 'PLANTING_REPORT',
      reportName: '榴莲树认种报表',
      reportCode: 'RPT_PLANTING',
      description: '榴莲树认种日/周/月/季度/年度报表',
      parameters: {
        dimensions: ['TIME', 'REGION'],
        defaultPeriod: 'DAILY',
      },
      scheduleCron: '0 1 * * *',
      scheduleEnabled: true,
      outputFormats: ['EXCEL', 'CSV', 'PDF'],
      isActive: true,
    },
    {
      reportType: 'REGIONAL_PLANTING_REPORT',
      reportName: '区域认种报表',
      reportCode: 'RPT_REGIONAL_PLANTING',
      description: '按省/市统计的认种报表',
      parameters: {
        dimensions: ['REGION', 'TIME'],
        defaultPeriod: 'DAILY',
      },
      scheduleCron: '0 2 * * *',
      scheduleEnabled: true,
      outputFormats: ['EXCEL', 'CSV'],
      isActive: true,
    },
    {
      reportType: 'AUTHORIZED_COMPANY_TOP_REPORT',
      reportName: '授权公司第1名统计',
      reportCode: 'RPT_COMPANY_TOP',
      description: '各授权省公司和市公司的第1名及完成数据',
      parameters: {
        dimensions: ['REGION', 'USER'],
        includeProvince: true,
        includeCity: true,
      },
      outputFormats: ['EXCEL', 'CSV'],
      isActive: true,
    },
    {
      reportType: 'COMMUNITY_REPORT',
      reportName: '社区数据统计',
      reportCode: 'RPT_COMMUNITY',
      description: '社区认种总量、日/周/月新增、上下级社区统计',
      parameters: {
        dimensions: ['COMMUNITY', 'TIME'],
        supportFuzzySearch: true,
      },
      outputFormats: ['EXCEL', 'CSV'],
      isActive: true,
    },
    {
      reportType: 'SYSTEM_ACCOUNT_MONTHLY_REPORT',
      reportName: '系统账户月度报表',
      reportCode: 'RPT_SYSTEM_ACCOUNT_MONTHLY',
      description: '系统省/市公司账户每月各项数据统计',
      parameters: {
        dimensions: ['ACCOUNT', 'TIME'],
        metrics: [
          'monthlyHashpower',
          'cumulativeHashpower',
          'monthlyMining',
          'cumulativeMining',
          'monthlyCommission',
          'cumulativeCommission',
          'monthlyPlantingBonus',
          'cumulativePlantingBonus',
        ],
      },
      scheduleCron: '0 0 1 * *',
      scheduleEnabled: true,
      outputFormats: ['EXCEL', 'CSV'],
      isActive: true,
    },
    {
      reportType: 'SYSTEM_ACCOUNT_INCOME_REPORT',
      reportName: '系统账户收益来源报表',
      reportCode: 'RPT_SYSTEM_ACCOUNT_INCOME',
      description: '系统省/市公司账户收益来源细分统计及时间轴',
      parameters: {
        dimensions: ['ACCOUNT', 'TIME', 'SOURCE'],
        supportTimeFilter: true,
        supportKeywordSearch: true,
      },
      outputFormats: ['EXCEL', 'CSV'],
      isActive: true,
    },
  ];

  for (const def of reportDefinitions) {
    await prisma.reportDefinition.upsert({
      where: { reportCode: def.reportCode },
      update: def,
      create: def,
    });
  }

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

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