import { Lead, LeadStatus } from "@/types"
import { users } from "./users"

const companyNames = [
  "Brightwave Studios", "Nexus Digital", "Pinnacle Web Solutions", "Vertex Media Group",
  "Apex Interactive", "Meridian Design Co", "Titan Software", "Crafted Web Agency",
  "Driftwood Creative", "Summit Digital Labs", "Horizon Interactive", "Pulse Marketing",
  "Ironclad Systems", "Northstar Digital", "Vanguard Web Services", "Cascade Solutions",
  "Atlas Creative Group", "Precision Web Co", "Momentum Digital", "Catalyst Agency",
  "Ridgewood Tech", "Silverline Interactive", "Harbor Media Group", "Providence Web",
  "Sentinel Systems", "Legacy Digital Agency", "Frontier Creative", "Summit Point Tech",
  "Barrel House Digital", "Anchor Web Services", "Trinity Media", "Skyline Interactive",
  "Cornerstone Digital", "Horizon Web Craft", "Noble Systems", "Crescent Creative",
  "Blue Ridge Digital", "Pathfinder Web Co", "Ravenswood Agency", "Sterling Media Group",
  "Cedar Creek Interactive", "Iron Gate Digital", "Fox Run Agency", "Oakwood Systems",
  "Clover Creative", "Birchwood Digital", "Stone Ridge Media", "Riverbend Tech",
  "Fairway Web Services", "Heritage Digital Group", "Maple Leaf Interactive", "Granite Systems",
  "Portside Creative", "Westwind Digital", "Crestview Agency", "Sunburst Media",
  "Broadwood Systems", "Highland Interactive", "Clearwater Web Co", "Emerald Digital",
]

const firstNames = ["James", "Mary", "Robert", "Patricia", "John", "Jennifer", "Michael", "Linda", "David", "Elizabeth", "William", "Barbara", "Richard", "Susan", "Joseph", "Jessica", "Thomas", "Sarah", "Christopher", "Karen", "Daniel", "Lisa", "Matthew", "Nancy", "Anthony", "Betty", "Mark", "Margaret", "Donald", "Sandra", "Steven", "Ashley", "Andrew", "Kimberly", "Paul", "Emily", "Joshua", "Donna", "Kenneth", "Michelle", "Kevin", "Carol", "Brian", "Amanda", "George", "Melissa", "Timothy", "Deborah", "Ronald", "Stephanie"]

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", "Walker", "Young", "Allen", "King", "Wright", "Scott", "Torres", "Nguyen", "Hill", "Flores", "Green", "Adams", "Nelson", "Baker", "Hall", "Rivera", "Campbell", "Mitchell", "Carter", "Roberts"]

const sources = ["Website", "Referral", "LinkedIn", "Cold Call", "Email", "Google", "Social Media", "Other"]
const statuses: LeadStatus[] = ["open", "contacted", "pending", "closed", "ignored"]

function randomItem<T>(arr: T[]): T {
  return arr[Math.floor(Math.random() * arr.length)]
}

function randomDate(startMonthsAgo: number): string {
  const now = new Date()
  const start = new Date(now.getFullYear(), now.getMonth() - startMonthsAgo, 1)
  const randomTime = start.getTime() + Math.random() * (now.getTime() - start.getTime())
  return new Date(randomTime).toISOString()
}

const phonePrefixes = ["555", "123", "456", "789", "321", "654", "987", "234", "876", "432"]

function randomPhone(): string {
  const prefix = randomItem(phonePrefixes)
  const suffix = Math.floor(Math.random() * 10000000).toString().padStart(7, "0")
  return `(${prefix}) ${suffix.slice(0, 3)}-${suffix.slice(3)}`
}

export const leads: Lead[] = Array.from({ length: 120 }, (_, i) => {
  const firstName = randomItem(firstNames)
  const lastName = randomItem(lastNames)
  const status = randomItem(statuses)
  const assignedUser = Math.random() > 0.15 ? randomItem(users.filter((u) => u.active)) : null
  const createdAt = randomDate(6)

  return {
    id: `lead-${String(i + 1).padStart(3, "0")}`,
    companyName: companyNames[i % companyNames.length],
    contactName: `${firstName} ${lastName}`,
    email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@${companyNames[i % companyNames.length].toLowerCase().replace(/\s+/g, "")}.com`,
    phone: randomPhone(),
    source: randomItem(sources),
    description: `Looking for a complete website redesign and modern digital presence. Interested in responsive design, SEO optimization, and a custom CMS solution.`,
    status,
    assignedUserId: assignedUser?.id ?? null,
    assignedUser,
    createdAt,
    updatedAt: createdAt,
  }
}).sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())

export function getLeadById(id: string): Lead | undefined {
  return leads.find((l) => l.id === id)
}

export function getLeadsByStatus(status: LeadStatus): Lead[] {
  return leads.filter((l) => l.status === status)
}

export function searchLeads(query: string): Lead[] {
  const q = query.toLowerCase()
  return leads.filter(
    (l) =>
      l.companyName.toLowerCase().includes(q) ||
      l.contactName.toLowerCase().includes(q) ||
      l.email.toLowerCase().includes(q) ||
      l.phone.includes(q)
  )
}
