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

// EN: Initialize Prisma client for seeding
// VI: Khởi tạo Prisma client cho seeding
const prisma = new PrismaClient();

async function main() {
  logger.info('Starting database seeding / Bắt đầu seeding database');

  // EN: Seed initial features
  // VI: Seed các tính năng ban đầu
  const features = [
    {
      name: 'health-checks',
      title: 'Health Checks',
      description: 'EN: System health monitoring endpoints / VI: Endpoints giám sát sức khỏe hệ thống',
      enabled: true,
      version: '1.0.0',
      tags: ['system', 'monitoring'],
      config: {
        endpoints: ['/health', '/health/ready', '/health/live'],
        interval: 30000, // 30 seconds
      },
    },
    {
      name: 'metrics',
      title: 'Prometheus Metrics',
      description: 'EN: Application metrics collection / VI: Thu thập metrics ứng dụng',
      enabled: true,
      version: '1.0.0',
      tags: ['observability', 'monitoring'],
      config: {
        endpoint: '/metrics',
        format: 'prometheus',
        defaultMetrics: true,
      },
    },
    {
      name: 'rate-limiting',
      title: 'Rate Limiting',
      description: 'EN: API rate limiting protection / VI: Bảo vệ giới hạn tốc độ API',
      enabled: true,
      version: '1.0.0',
      tags: ['security', 'performance'],
      config: {
        windowMs: 900000, // 15 minutes
        max: 100, // limit each IP to 100 requests per windowMs
        standardHeaders: true,
        legacyHeaders: false,
      },
    },
    {
      name: 'cors',
      title: 'CORS Protection',
      description: 'EN: Cross-Origin Resource Sharing configuration / VI: Cấu hình Cross-Origin Resource Sharing',
      enabled: true,
      version: '1.0.0',
      tags: ['security', 'api'],
      config: {
        origins: ['http://localhost:3000'],
        credentials: true,
        methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
      },
    },
    {
      name: 'tracing',
      title: 'Distributed Tracing',
      description: 'EN: Jaeger/OpenTelemetry distributed tracing / VI: Distributed tracing với Jaeger/OpenTelemetry',
      enabled: false,
      version: '1.0.0',
      tags: ['observability', 'tracing'],
      config: {
        serviceName: 'microservice-template',
        jaegerEndpoint: 'http://localhost:14268/api/traces',
        samplingRate: 1.0,
      },
    },
  ];

  // EN: Create features in database
  // VI: Tạo features trong database
  for (const feature of features) {
    await prisma.feature.upsert({
      where: { name: feature.name },
      update: {
        title: feature.title,
        description: feature.description,
        enabled: feature.enabled,
        version: feature.version,
        tags: feature.tags,
        config: feature.config,
        updatedAt: new Date(),
      },
      create: feature,
    });

    logger.info(`Seeded feature: ${feature.name} / Đã seed feature: ${feature.name}`);
  }

  logger.info('Database seeding completed / Hoàn thành seeding database');
}

main()
  .catch((e) => {
    logger.error('Error during database seeding / Lỗi trong quá trình seeding database', { error: e });
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });