import { DataSource } from 'typeorm';
import { CommunityGoal } from '../community/community-goal.entity';

export async function seedCommunityGoals(dataSource: DataSource) {
  const repo = dataSource.getRepository(CommunityGoal);

  // Current month boundaries
  const now = new Date();
  const periodStart = new Date(now.getFullYear(), now.getMonth(), 1);
  const periodEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0);

  const GOALS = [
    {
      name: 'Chasse aux Monstres',
      description: 'La communauté doit éliminer 10 000 monstres ce mois-ci !',
      criteriaType: 'total_monsters_killed',
      targetValue: 10000,
      rewardType: 'xp_boost',
      rewardMultiplier: 1.20,
      rewardDurationHours: 72,
      periodStart,
      periodEnd,
    },
    {
      name: 'Trésor Communautaire',
      description: 'Accumuler 1 000 000 pièces d\'or collectivement !',
      criteriaType: 'total_gold_earned',
      targetValue: 1000000,
      rewardType: 'loot_boost',
      rewardMultiplier: 1.15,
      rewardDurationHours: 48,
      periodStart,
      periodEnd,
    },
    {
      name: 'Fièvre de la Forge',
      description: 'Réaliser 500 améliorations de forge collectivement !',
      criteriaType: 'total_forge_upgrades',
      targetValue: 500,
      rewardType: 'xp_boost',
      rewardMultiplier: 1.10,
      rewardDurationHours: 48,
      periodStart,
      periodEnd,
    },
  ];

  for (const data of GOALS) {
    const existing = await repo.findOne({
      where: { criteriaType: data.criteriaType, periodStart: data.periodStart },
    });
    if (!existing) {
      await repo.save(repo.create(data));
    }
  }

  console.log(`✅ ${GOALS.length} community goals seedés`);
}
