
import fs from 'fs';
import path from 'path';

const OUTPUT_FILE = path.join(__dirname, '../../data/patients.json');

const firstNames = ['James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer', 'Michael', 'Linda', 'William', 'Elizabeth', 'David', 'Barbara', 'Richard', 'Susan', 'Joseph', 'Jessica', 'Thomas', 'Sarah', 'Charles', 'Karen', 'Christopher', 'Nancy', 'Daniel', 'Lisa', 'Matthew', 'Betty', 'Anthony', 'Margaret', 'Mark', 'Sandra'];
const lastNames = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez', 'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson', 'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin', 'Lee', 'Perez', 'Thompson', 'White', 'Harris', 'Sanchez', 'Clark', 'Ramirez', 'Lewis', 'Robinson'];
const domains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'example.com'];
const streets = ['Main St', 'Oak Ave', 'Pine Rd', 'Maple Ln', 'Cedar Blvd', 'Elm St', 'Washington St', 'Lakeview Dr'];
const cities = ['Springfield', 'Metropolis', 'Gotham', 'Star City', 'Central City', 'Coast City', 'Smallville', 'Riverdale'];

function getRandomElement(arr: string[]) {
    return arr[Math.floor(Math.random() * arr.length)];
}

function getRandomDate(start: Date, end: Date) {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())).toISOString().split('T')[0];
}

function generatePatients(count: number) {
    const patients = [];
    const existingEmails = new Set();

    // Keep existing data if file exists (optional, but requested dump so maybe overwrite is better? 
    // Let's overwrite to ensure clean state or append if we read first. 
    // User asked "more than 100", let's just generate 150 fresh ones).

    for (let i = 0; i < count; i++) {
        const firstName = getRandomElement(firstNames);
        const lastName = getRandomElement(lastNames);
        let email = `${firstName.toLowerCase()}.${lastName.toLowerCase()}@${getRandomElement(domains)}`;

        let counter = 1;
        while (existingEmails.has(email)) {
            email = `${firstName.toLowerCase()}.${lastName.toLowerCase()}${counter}@${getRandomElement(domains)}`;
            counter++;
        }
        existingEmails.add(email);

        patients.push({
            firstName,
            lastName,
            email,
            phone: `555-${Math.floor(1000 + Math.random() * 9000)}`,
            gender: Math.random() > 0.5 ? 'Male' : 'Female',
            dateOfBirth: getRandomDate(new Date(1950, 0, 1), new Date(2005, 0, 1)),
            address: `${Math.floor(1 + Math.random() * 999)} ${getRandomElement(streets)}, ${getRandomElement(cities)}`,
            medicalHistory: Math.random() > 0.7 ? 'No significant history' : (Math.random() > 0.5 ? 'Hypertension' : 'Asthma')
        });
    }

    return patients;
}

const patients = generatePatients(1500);

fs.writeFileSync(OUTPUT_FILE, JSON.stringify(patients, null, 2));
console.log(`Generated ${patients.length} patients to ${OUTPUT_FILE}`);
