/**
 * Seed Test Data for Staff Dashboard
 * Creates sample demos, contacts, users, and site visits for testing
 */

import { PrismaClient } from '../lib/generated/prisma/index.js';

const prisma = new PrismaClient();

async function seedTestData() {
  console.log('\n🌱 Seeding Staff Dashboard Test Data...\n');

  try {
    // Create test customer users
    console.log('Creating test customer users...');
    const customerUsers = [];
    for (let i = 1; i <= 10; i++) {
      const user = await prisma.users.upsert({
        where: { email: `customer${i}@test.com` },
        update: {},
        create: {
          email: `customer${i}@test.com`,
          name: `Test Customer ${i}`,
          role: 'CUSTOMER',
          country: ['Qatar', 'Saudi Arabia', 'UAE', 'Kuwait'][i % 4],
          companyName: `Company ${i}`,
          industry: ['Healthcare', 'Beauty', 'Hotels'][i % 3],
          selectedPackage: ['Basic', 'Professional', 'Enterprise'][i % 3],
          emailVerified: new Date(),
          createdAt: new Date(Date.now() - (i * 24 * 60 * 60 * 1000)), // Stagger by days
        },
      });
      customerUsers.push(user);
    }
    console.log(`✓ Created ${customerUsers.length} test customers`);

    // Create demo bookings
    console.log('\nCreating demo bookings...');
    const demoStatuses = ['pending', 'confirmed', 'cancelled'];
    for (let i = 1; i <= 15; i++) {
      await prisma.demo_bookings.create({
        data: {
          fullName: `Demo Customer ${i}`,
          email: `demo${i}@test.com`,
          telephone: `+974555${String(i).padStart(5, '0')}`,
          appointmentDate: new Date(Date.now() + (i * 24 * 60 * 60 * 1000)), // Future dates
          appointmentTime: `${9 + (i % 8)}:00`,
          duration: ['30 minutes', '60 minutes'][i % 2],
          language: i % 2 === 0 ? 'ar' : 'en',
          status: demoStatuses[i % 3],
          specialRequirements: i % 3 === 0 ? `Special requirement for demo ${i}` : null,
          meetingLink: i % 3 === 1 ? `https://zoom.us/j/test${i}` : null,
          createdAt: new Date(Date.now() - (i * 2 * 60 * 60 * 1000)), // Stagger by hours
        },
      });
    }
    console.log('✓ Created 15 demo bookings');

    // Create contact submissions
    console.log('\nCreating contact submissions...');
    const contactStatuses = ['NEW', 'READ', 'IN_PROGRESS', 'RESPONDED', 'CLOSED'];
    for (let i = 1; i <= 12; i++) {
      await prisma.contacts.create({
        data: {
          name: `Contact Person ${i}`,
          email: `contact${i}@test.com`,
          phone: `+974666${String(i).padStart(5, '0')}`,
          company: `Test Company ${i}`,
          message: `This is a test contact message ${i}. We are interested in learning more about your services.`,
          language: i % 2 === 0 ? 'ar' : 'en',
          status: contactStatuses[i % 5],
          createdAt: new Date(Date.now() - (i * 3 * 60 * 60 * 1000)), // Stagger by 3 hours
        },
      });
    }
    console.log('✓ Created 12 contact submissions');

    // Create site visits
    console.log('\nCreating site visit records...');
    const pages = ['/ar', '/en', '/ar/demo', '/en/demo', '/ar/pricing', '/en/pricing', '/ar/features'];
    const countries = ['Qatar', 'Saudi Arabia', 'UAE', 'Kuwait', 'Bahrain', 'Oman'];

    for (let i = 1; i <= 100; i++) {
      await prisma.siteVisit.create({
        data: {
          visitorId: `visitor_${i % 30}`, // Simulate 30 unique visitors
          sessionId: `session_${i % 50}`, // Multiple visits per session
          page: pages[i % pages.length],
          country: countries[i % countries.length],
          language: i % 2 === 0 ? 'ar' : 'en',
          userAgent: 'Mozilla/5.0 (Test)',
          referrer: i % 5 === 0 ? 'https://google.com' : null,
          createdAt: new Date(Date.now() - (i * 30 * 60 * 1000)), // Stagger by 30 minutes
        },
      });
    }
    console.log('✓ Created 100 site visit records');

    // Create staff alerts
    console.log('\nCreating staff alerts...');
    const alertTypes = ['NEW_DEMO', 'NEW_CONTACT', 'NEW_USER', 'PAYMENT_FAILED'];
    const severities = ['INFO', 'WARNING', 'ERROR'];

    for (let i = 1; i <= 8; i++) {
      await prisma.staff_alerts.create({
        data: {
          type: alertTypes[i % alertTypes.length],
          title: `Test Alert ${i}`,
          message: `This is test alert message ${i}`,
          severity: severities[i % severities.length],
          isRead: i % 3 === 0,
          createdAt: new Date(Date.now() - (i * 60 * 60 * 1000)), // Stagger by hours
        },
      });
    }
    console.log('✓ Created 8 staff alerts');

    console.log('\n✅ Test Data Seeding Complete!\n');
    console.log('📊 Summary:');
    console.log(`   - ${customerUsers.length} customer users`);
    console.log(`   - 15 demo bookings (5 pending, 5 confirmed, 5 cancelled)`);
    console.log(`   - 12 contact submissions (various statuses)`);
    console.log(`   - 100 site visits (30 unique visitors)`);
    console.log(`   - 8 staff alerts (some read, some unread)`);
    console.log('\n🚀 Staff dashboard is ready for testing!\n');
  } catch (error) {
    console.error('❌ Seeding failed:', error.message);
    console.error(error);
  } finally {
    await prisma.$disconnect();
  }
}

seedTestData();
