import type { Lead, Campaign, Ad, LeadSource, Priority } from '@/types/entities';

// Sequential ID generator
let _idCounter = 1000;
export function nextId(): string {
    return `mock-${_idCounter++}`;
}

// Random element picker
export function randomFrom<T>(arr: T[]): T {
    return arr[Math.floor(Math.random() * arr.length)];
}

// ISO date string for N days ago
export function daysAgo(n: number): string {
    const d = new Date();
    d.setDate(d.getDate() - n);
    return d.toISOString();
}

// INR currency helper
export function inr(amount: number): { amountMicros: number; currencyCode: string } {
    return { amountMicros: amount * 1_000_000, currencyCode: 'INR' };
}

// Indian name pools
export const indianFirstNames = [
    'Aarav', 'Aditya', 'Akash', 'Amit', 'Ananya', 'Anjali', 'Ankit', 'Anushka',
    'Arjun', 'Aryan', 'Deepa', 'Deepika', 'Divya', 'Geeta', 'Girish', 'Harsha',
    'Ishaan', 'Kavitha', 'Kiran', 'Lakshmi', 'Madhuri', 'Meena', 'Mohan', 'Nandini',
    'Nikhil', 'Pooja', 'Priya', 'Rahul', 'Raj', 'Rajesh', 'Ramesh', 'Rekha',
    'Rohit', 'Sarita', 'Seema', 'Shilpa', 'Shyam', 'Sneha', 'Sunita', 'Suresh',
    'Usha', 'Varun', 'Vijay', 'Vikram', 'Vipin', 'Vishal', 'Yamini', 'Yogesh',
    'Zara', 'Preethi',
];

export const indianLastNames = [
    'Agarwal', 'Bhat', 'Chandra', 'Desai', 'Ghosh', 'Gupta', 'Iyer', 'Jain',
    'Kamath', 'Kumar', 'Mehta', 'Murthy', 'Nair', 'Patel', 'Rao', 'Reddy',
    'Sharma', 'Singh', 'Srinivas', 'Verma',
];

const leadSources: LeadSource[] = [
    'FACEBOOK_AD', 'FACEBOOK_AD', 'FACEBOOK_AD',
    'GOOGLE_AD', 'GOOGLE_AD', 'GOOGLE_AD',
    'INSTAGRAM', 'INSTAGRAM',
    'WHATSAPP', 'WHATSAPP',
    'GOOGLE_MY_BUSINESS',
    'WEBSITE',
];

const services = [
    'IVF Consultation', 'Fertility Assessment', 'Egg Freezing', 'Embryo Transfer',
    'Prenatal Care', 'Gynecology Checkup', 'Cervical Screening', 'Mammography',
    'General Health Checkup', 'Diabetes Consultation', 'Cardiology Checkup',
    'Orthopaedic Consultation', 'Senior Health Package', 'Nutrition Counselling',
];

const priorities: Priority[] = ['LOW', 'NORMAL', 'NORMAL', 'NORMAL', 'HIGH', 'URGENT'];

export function createMockLead(overrides?: Partial<Lead>): Lead {
    const firstName = randomFrom(indianFirstNames);
    const lastName = randomFrom(indianLastNames);
    const emailLocal = `${firstName.toLowerCase()}.${lastName.charAt(0).toLowerCase()}`;
    const phoneNumber = `+91${Math.floor(7000000000 + Math.random() * 2999999999)}`;
    const spamScore = Math.floor(Math.random() * 100);
    const source = randomFrom(leadSources);

    return {
        id: nextId(),
        createdAt: daysAgo(Math.floor(Math.random() * 14)),
        updatedAt: daysAgo(Math.floor(Math.random() * 2)),
        leadSource: source,
        leadStatus: 'NEW',
        priority: randomFrom(priorities),
        contactName: { firstName, lastName },
        contactPhone: [{ number: phoneNumber, callingCode: '+91' }],
        contactEmail: [{ address: `${emailLocal}@gmail.com` }],
        interestedService: randomFrom(services),
        assignedAgent: null,
        utmSource: null,
        utmMedium: null,
        utmCampaign: null,
        utmContent: null,
        utmTerm: null,
        landingPageUrl: null,
        referrerUrl: null,
        leadScore: Math.floor(Math.random() * 100),
        spamScore,
        isSpam: spamScore >= 60,
        isDuplicate: false,
        duplicateOfLeadId: null,
        firstContactedAt: null,
        lastContactedAt: null,
        contactAttempts: 0,
        convertedAt: null,
        aiSummary: null,
        aiSuggestedAction: null,
        patientId: null,
        campaignId: null,
        adId: null,
        ...overrides,
    };
}

export function createMockCampaign(overrides?: Partial<Campaign>): Campaign {
    return {
        id: nextId(),
        createdAt: daysAgo(30),
        updatedAt: daysAgo(1),
        campaignName: null,
        campaignType: 'FACEBOOK_AD',
        campaignStatus: 'ACTIVE',
        platform: 'FACEBOOK',
        startDate: daysAgo(30),
        endDate: null,
        budget: inr(100000),
        amountSpent: inr(50000),
        impressionCount: 50000,
        clickCount: 1200,
        targetCount: 10000,
        contactedCount: 80,
        convertedCount: 10,
        leadCount: 100,
        externalCampaignId: null,
        platformUrl: null,
        ...overrides,
    };
}

export function createMockAd(campaignId: string, overrides?: Partial<Ad>): Ad {
    return {
        id: nextId(),
        createdAt: daysAgo(28),
        updatedAt: daysAgo(2),
        adName: null,
        externalAdId: null,
        adStatus: 'ACTIVE',
        adFormat: 'IMAGE',
        headline: null,
        adDescription: null,
        destinationUrl: 'https://www.globalhospital.com',
        previewUrl: null,
        impressions: 10000,
        clicks: 250,
        conversions: 15,
        spend: inr(15000),
        campaignId,
        ...overrides,
    };
}
