import { ILeadSource, NormalizedLead, SearchParams, SourceCategory } from "./types";
import { z } from "zod";

export class MockSource implements ILeadSource {
  id = "mock-source";
  name = "Mock Data Source";
  description = "Generates realistic fake leads for UI testing and development.";
  category: SourceCategory = "other";
  
  inputSchema = z.object({
    keyword: z.string().optional(),
    location: z.string().optional(),
    limit: z.number().optional().default(50),
  });

  async run(params: SearchParams): Promise<NormalizedLead[]> {
    const limit = params.limit || 10;
    const leads: NormalizedLead[] = [];
    
    // Simulate API delay
    await new Promise(resolve => setTimeout(resolve, 1500));

    const industries = ["Software", "Plumbing", "Law Firm", "Marketing", "Real Estate"];
    const cities = ["New York", "London", "San Francisco", "Austin", "Berlin"];

    for (let i = 0; i < limit; i++) {
      const industry = params.industry || industries[Math.floor(Math.random() * industries.length)];
      const city = params.location || cities[Math.floor(Math.random() * cities.length)];
      const companyId = Math.floor(Math.random() * 10000);
      
      leads.push({
        businessName: `${city} ${industry} Co. ${companyId}`,
        website: `https://${city.toLowerCase().replace(" ", "")}-${industry.toLowerCase()}-${companyId}.com`,
        email: `contact@${city.toLowerCase().replace(" ", "")}-${industry.toLowerCase()}-${companyId}.com`,
        phone: `+1 (555) ${Math.floor(100 + Math.random() * 900)}-${Math.floor(1000 + Math.random() * 9000)}`,
        address: `${Math.floor(100 + Math.random() * 900)} Business Way`,
        city: city,
        state: "State",
        country: "US",
        category: industry,
        confidenceScore: 0.8 + Math.random() * 0.2,
        rawData: { mock: true, id: companyId }
      });
    }

    return leads;
  }

  isAvailable(): boolean {
    // Enable by default in development/test or if explicitly requested via env
    return process.env.NODE_ENV === 'development' || 
           process.env.NODE_ENV === 'test' || 
           process.env.ENABLE_MOCK_DATA === 'true';
  }
}
