import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { PlanRepository } from '../repositories/plan.repository';

const SEED_PLANS = [
  {
    name: 'free',
    displayName: 'Free',
    monthlyPriceCentsUsd: 0,
    monthlyPriceFenCny: 0,
    includedTokens: 100_000,
    overageRateCentsPerMTokenUsd: 0,
    overageRateFenPerMTokenCny: 0,
    maxServers: 5,
    maxUsers: 3,
    maxStandingOrders: 10,
    hardLimitPercent: 100,
    trialDays: 0,
    isActive: true,
  },
  {
    name: 'pro',
    displayName: 'Pro',
    monthlyPriceCentsUsd: 4999, // $49.99
    monthlyPriceFenCny: 34900, // ¥349.00
    includedTokens: 1_000_000,
    overageRateCentsPerMTokenUsd: 800, // $8.00 per MTok
    overageRateFenPerMTokenCny: 5800, // ¥58.00 per MTok
    maxServers: 50,
    maxUsers: 20,
    maxStandingOrders: 100,
    hardLimitPercent: 150,
    trialDays: 0,
    isActive: true,
  },
  {
    name: 'enterprise',
    displayName: 'Enterprise',
    monthlyPriceCentsUsd: 19999, // $199.99
    monthlyPriceFenCny: 139900, // ¥1399.00
    includedTokens: 10_000_000,
    overageRateCentsPerMTokenUsd: 500, // $5.00 per MTok
    overageRateFenPerMTokenCny: 3600, // ¥36.00 per MTok
    maxServers: -1,
    maxUsers: -1,
    maxStandingOrders: -1,
    hardLimitPercent: 0, // no hard limit
    trialDays: 0,
    isActive: true,
  },
];

@Injectable()
export class PlanSeedService implements OnModuleInit {
  private readonly logger = new Logger(PlanSeedService.name);

  constructor(private readonly planRepo: PlanRepository) {}

  async onModuleInit() {
    try {
      await this.planRepo.upsertSeedPlans(SEED_PLANS);
      this.logger.log('Plan seed completed');
    } catch (err) {
      this.logger.error(`Plan seed failed: ${err.message}`);
    }
  }
}
